C/C++ API Reference
Loading...
Searching...
No Matches
cast.h
1// Copyright 2025 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include "pw_bytes/alignment.h"
17#include "pw_span/internal/config.h"
18#include "pw_span/span.h"
19
20namespace pw {
21namespace internal {
22
23template <class ResultT, size_t kSourceExtentBytes>
24using SpanFromBytes = span<ResultT,
25 (kSourceExtentBytes == dynamic_extent
26 ? dynamic_extent
27 : kSourceExtentBytes / sizeof(ResultT))>;
28
29} // namespace internal
30
32
35
55template <class ResultT, size_t kSourceExtentBytes>
58 static_assert(sizeof(ResultT) == 1);
59
60 ResultT* const ptr = reinterpret_cast<ResultT*>(bytes.data());
61 const size_t count = bytes.size() / sizeof(ResultT);
62
63 auto result =
65
66 _PW_SPAN_ASSERT(IsAlignedAs<ResultT>(result.data()));
67 _PW_SPAN_ASSERT(result.size_bytes() == bytes.size_bytes());
68
69 return result;
70}
71
72// TODO: https://pwbug.dev/396493663 - Doxygen thinks this is the same function
73// as the non-const version above, and merges the docs together.
74
95template <class ResultT, size_t kSourceExtentBytes>
98 static_assert(sizeof(ResultT) == 1);
99
100 const ResultT* const ptr = reinterpret_cast<const ResultT*>(bytes.data());
101 const size_t count = bytes.size() / sizeof(ResultT);
102
103 auto result =
105
106 _PW_SPAN_ASSERT(IsAlignedAs<const ResultT>(result.data()));
107 _PW_SPAN_ASSERT(result.size_bytes() == bytes.size_bytes());
108
109 return result;
110}
111
113
114} // namespace pw
Definition: span_impl.h:235
internal::SpanFromBytes< ResultT, kSourceExtentBytes > span_cast(span< std::byte, kSourceExtentBytes > bytes)
Definition: cast.h:56
The Pigweed namespace.
Definition: alignment.h:27