Debian Codegraph @

src/mpt/base/memory.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_BASE_MEMORY_HPP
4 #define MPT_BASE_MEMORY_HPP
5
6
7
8 #include "mpt/base/integer.hpp"
9 #include "mpt/base/namespace.hpp"
10 #include "mpt/base/macros.hpp"
11 #include "mpt/base/span.hpp"
12
13 #include <type_traits>
14
15 #include <cstddef>
16 #include <cstring>
17
18
19
20 namespace mpt {
21 inline namespace MPT_INLINE_NS {
22
23
24
25 using byte_span = mpt::span<std::byte>;
26 using const_byte_span = mpt::span<const std::byte>;
27
28
29
30 // Tell which types are safe for mpt::byte_cast.
31 // signed char is actually not allowed to alias into an object representation,
32 // which means that, if the actual type is not itself signed char but char or
33 // unsigned char instead, dereferencing the signed char pointer is undefined
34 // behaviour.
35 template <typename T>
36 struct is_byte_castable : public std::false_type { };
37 template <>
38 struct is_byte_castable<char> : public std::true_type { };
39 template <>
40 struct is_byte_castable<unsigned char> : public std::true_type { };
41 template <>
42 struct is_byte_castable<std::byte> : public std::true_type { };
43 template <>
44 struct is_byte_castable<const char> : public std::true_type { };
45 template <>
46 struct is_byte_castable<const unsigned char> : public std::true_type { };
47 template <>
48 struct is_byte_castable<const std::byte> : public std::true_type { };
49
50
51 template <typename T>
52 struct is_byte : public std::false_type { };
53 template <>
54 struct is_byte<std::byte> : public std::true_type { };
55 template <>
56 struct is_byte<const std::byte> : public std::true_type { };
57
58
59 template <typename T>
60 constexpr bool declare_binary_safe(const T &) noexcept {
61 return false;
62 }
63
64 constexpr bool declare_binary_safe(const char &) noexcept {
65 return true;
66 }
67 constexpr bool declare_binary_safe(const uint8 &) noexcept {
68 return true;
69 }
70 constexpr bool declare_binary_safe(const int8 &) noexcept {
71 return true;
72 }
73 constexpr bool declare_binary_safe(const std::byte &) noexcept {
74 return true;
75 }
76
77 // Tell which types are safe to binary write into files.
78 // By default, no types are safe.
79 // When a safe type gets defined,
80 // also specialize this template so that IO functions will work.
81 template <typename T>
82 struct is_binary_safe : public std::conditional<declare_binary_safe(T{}), std::true_type, std::false_type>::type { };
83
84 // Generic Specialization for arrays.
85 template <typename T, std::size_t N>
86 struct is_binary_safe<T[N]> : public is_binary_safe<typename std::remove_const<T>::type> { };
87 template <typename T, std::size_t N>
88 struct is_binary_safe<const T[N]> : public is_binary_safe<typename std::remove_const<T>::type> { };
89 template <typename T, std::size_t N>
90 struct is_binary_safe<std::array<T, N>> : public is_binary_safe<typename std::remove_const<T>::type> { };
91 template <typename T, std::size_t N>
92 struct is_binary_safe<const std::array<T, N>> : public is_binary_safe<typename std::remove_const<T>::type> { };
93
94 template <typename T>
95 struct is_binary_safe<mpt::span<T>> : public is_binary_safe<typename std::remove_const<T>::type> { };
96 template <typename T>
97 struct is_binary_safe<const mpt::span<T>> : public is_binary_safe<typename std::remove_const<T>::type> { };
98
99
100 template <typename T>
101 constexpr bool check_binary_size(std::size_t size) noexcept {
102 return true
103 && (sizeof(T) == size)
104 && (alignof(T) == 1)
105 && std::is_standard_layout<T>::value
106 #if !defined(MPT_LIBCXX_QUIRK_NO_HAS_UNIQUE_OBJECT_REPRESENTATIONS)
107 && std::has_unique_object_representations<T>::value
108 #endif
109 && mpt::is_binary_safe<T>::value;
110 }
111
112
113 template <typename Tdst, typename Tsrc>
114 struct byte_cast_impl {
115 inline Tdst operator()(Tsrc src) const noexcept {
116 static_assert(sizeof(Tsrc) == sizeof(std::byte));
117 static_assert(sizeof(Tdst) == sizeof(std::byte));
118 // not checking is_byte_castable here because we are actually
119 // doing a static_cast and converting the value
120 static_assert(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
121 static_assert(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
122 return static_cast<Tdst>(src);
123 }
124 };
125
126 template <typename Tdst, typename Tsrc>
127 struct byte_cast_impl<mpt::span<Tdst>, mpt::span<Tsrc>> {
128 inline mpt::span<Tdst> operator()(mpt::span<Tsrc> src) const noexcept {
129 static_assert(sizeof(Tsrc) == sizeof(std::byte));
130 static_assert(sizeof(Tdst) == sizeof(std::byte));
131 static_assert(mpt::is_byte_castable<Tsrc>::value);
132 static_assert(mpt::is_byte_castable<Tdst>::value);
133 static_assert(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
134 static_assert(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
135 return mpt::as_span(mpt::byte_cast_impl<Tdst *, Tsrc *>()(src.data()), mpt::byte_cast_impl<Tdst *, Tsrc *>()(src.data() + src.size()));
136 }
137 };
138
139 template <typename Tdst, typename Tsrc>
140 struct byte_cast_impl<Tdst *, Tsrc *> {
141 inline Tdst * operator()(Tsrc * src) const noexcept {
142 static_assert(sizeof(Tsrc) == sizeof(std::byte));
143 static_assert(sizeof(Tdst) == sizeof(std::byte));
144 static_assert(mpt::is_byte_castable<Tsrc>::value);
145 static_assert(mpt::is_byte_castable<Tdst>::value);
146 static_assert(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
147 static_assert(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
148 return reinterpret_cast<Tdst *>(src);
149 }
150 };
151
152 template <typename Tdst, typename Tsrc>
153 struct void_cast_impl;
154
155 template <typename Tdst>
156 struct void_cast_impl<Tdst *, void *> {
157 inline Tdst * operator()(void * src) const noexcept {
158 static_assert(sizeof(Tdst) == sizeof(std::byte));
159 static_assert(mpt::is_byte_castable<Tdst>::value);
160 static_assert(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
161 return reinterpret_cast<Tdst *>(src);
162 }
163 };
164
165 template <typename Tdst>
166 struct void_cast_impl<Tdst *, const void *> {
167 inline Tdst * operator()(const void * src) const noexcept {
168 static_assert(sizeof(Tdst) == sizeof(std::byte));
169 static_assert(mpt::is_byte_castable<Tdst>::value);
170 static_assert(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
171 return reinterpret_cast<Tdst *>(src);
172 }
173 };
174
175 template <typename Tsrc>
176 struct void_cast_impl<void *, Tsrc *> {
177 inline void * operator()(Tsrc * src) const noexcept {
178 static_assert(sizeof(Tsrc) == sizeof(std::byte));
179 static_assert(mpt::is_byte_castable<Tsrc>::value);
180 static_assert(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
181 return reinterpret_cast<void *>(src);
182 }
183 };
184
185 template <typename Tsrc>
186 struct void_cast_impl<const void *, Tsrc *> {
187 inline const void * operator()(Tsrc * src) const noexcept {
188 static_assert(sizeof(Tsrc) == sizeof(std::byte));
189 static_assert(mpt::is_byte_castable<Tsrc>::value);
190 static_assert(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
191 return reinterpret_cast<const void *>(src);
192 }
193 };
194
195 // casts between different byte (char) types or pointers to these types
196 template <typename Tdst, typename Tsrc>
197 inline Tdst byte_cast(Tsrc src) noexcept {
198 return byte_cast_impl<Tdst, Tsrc>()(src);
199 }
200
201 // casts between pointers to void and pointers to byte
202 template <typename Tdst, typename Tsrc>
203 inline Tdst void_cast(Tsrc src) noexcept {
204 return void_cast_impl<Tdst, Tsrc>()(src);
205 }
206
207
208
209 template <typename T>
210 MPT_CONSTEXPRINLINE std::byte as_byte(T src) noexcept {
211 static_assert(std::is_integral<T>::value);
212 return static_cast<std::byte>(static_cast<uint8>(src));
213 }
214
215
216
217 template <typename T>
218 struct as_raw_memory_impl {
219 inline mpt::const_byte_span operator()(const T & v) const {
220 static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
221 return mpt::as_span(reinterpret_cast<const std::byte *>(&v), sizeof(T));
222 }
223 inline mpt::byte_span operator()(T & v) const {
224 static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
225 return mpt::as_span(reinterpret_cast<std::byte *>(&v), sizeof(T));
226 }
227 };
228
229 template <typename T, std::size_t N>
230 struct as_raw_memory_impl<T[N]> {
231 inline mpt::const_byte_span operator()(const T (&v)[N]) const {
232 static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
233 return mpt::as_span(reinterpret_cast<const std::byte *>(v), N * sizeof(T));
234 }
235 inline mpt::byte_span operator()(T (&v)[N]) const {
236 static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
237 return mpt::as_span(reinterpret_cast<std::byte *>(v), N * sizeof(T));
238 }
239 };
240
241 template <typename T, std::size_t N>
242 struct as_raw_memory_impl<const T[N]> {
243 inline mpt::const_byte_span operator()(const T (&v)[N]) const {
244 static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
245 return mpt::as_span(reinterpret_cast<const std::byte *>(v), N * sizeof(T));
246 }
247 };
248
249 template <typename T>
250 struct as_raw_memory_impl<mpt::span<T>> {
251 inline mpt::const_byte_span operator()(const mpt::span<const T> & v) const {
252 static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
253 return mpt::as_span(reinterpret_cast<const std::byte *>(v.data()), v.size() * sizeof(T));
254 }
255 inline mpt::byte_span operator()(const mpt::span<T> & v) const {
256 static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
257 return mpt::as_span(reinterpret_cast<std::byte *>(v.data()), v.size() * sizeof(T));
258 }
259 };
260
261 template <typename T>
262 struct as_raw_memory_impl<mpt::span<const T>> {
263 inline mpt::const_byte_span operator()(const mpt::span<const T> & v) const {
264 static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
265 return mpt::as_span(reinterpret_cast<const std::byte *>(v.data()), v.size() * sizeof(T));
266 }
267 };
268
269 // In order to be able to partially specialize it,
270 // as_raw_memory is implemented via a class template.
271 // Do not overload or specialize as_raw_memory directly.
272 // Using a wrapper (by default just around a cast to const std::byte *),
273 // allows for implementing raw memory access
274 // via on-demand generating a cached serialized representation.
275 template <typename T>
276 inline mpt::const_byte_span as_raw_memory(const T & v) {
277 return mpt::as_raw_memory_impl<T>()(v);
278 }
279 template <typename T>
280 inline mpt::byte_span as_raw_memory(T & v) {
281 return mpt::as_raw_memory_impl<T>()(v);
282 }
283
284
285
286 template <class T>
287 inline void memclear(T & x) {
288 static_assert(std::is_standard_layout<T>::value);
289 static_assert((std::is_trivially_default_constructible<T>::value && std::is_trivially_copyable<T>::value) || mpt::is_binary_safe<T>::value);
290 std::memset(&x, 0, sizeof(T));
291 }
292
293
294
295 } // namespace MPT_INLINE_NS
296 } // namespace mpt
297
298
299
300 #endif // MPT_BASE_MEMORY_HPP