| 1 |
/* |
| 2 |
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 |
* It is copyright by its individual contributors, as recorded in the |
| 4 |
* project's Git history. See COPYING.txt at the top level for license |
| 5 |
* terms and a link to the Git history. |
| 6 |
*/ |
| 7 |
#pragma once |
| 8 |
#include <algorithm> |
| 9 |
#include <bit> |
| 10 |
#include <cassert> |
| 11 |
#include <cstdint> |
| 12 |
#include <functional> |
| 13 |
#include <initializer_list> |
| 14 |
#include <tuple> |
| 15 |
|
| 16 |
#include "dxxsconf.h" |
| 17 |
#include "compiler-range_for.h" |
| 18 |
#include <array> |
| 19 |
#include <memory> |
| 20 |
#include <utility> |
| 21 |
#include "fwd-d_array.h" |
| 22 |
|
| 23 |
namespace serial { |
| 24 |
|
| 25 |
template <std::uint64_t Expected, std::uint64_t Actual> |
| 26 |
concept check_equal_value = (Expected == Actual); |
| 27 |
|
| 28 |
template <typename... Args> |
| 29 |
class message; |
| 30 |
|
| 31 |
template <typename> |
| 32 |
class message_type; |
| 33 |
|
| 34 |
/* Classifiers to identify whether a type is a message<...> */ |
| 35 |
template <typename> |
| 36 |
class is_message : public std::false_type |
| 37 |
{ |
| 38 |
}; |
| 39 |
|
| 40 |
template <typename... Args> |
| 41 |
class is_message<message<Args...>> : public std::true_type |
| 42 |
{ |
| 43 |
}; |
| 44 |
|
| 45 |
namespace detail { |
| 46 |
|
| 47 |
template <std::size_t maximum, std::size_t minimum = maximum> |
| 48 |
struct size_base |
| 49 |
{ |
| 50 |
static constexpr std::integral_constant<std::size_t, maximum> maximum_size{}; |
| 51 |
static constexpr std::integral_constant<std::size_t, minimum> minimum_size{}; |
| 52 |
}; |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
template <typename> |
| 57 |
class is_cxx_array : public std::false_type |
| 58 |
{ |
| 59 |
}; |
| 60 |
|
| 61 |
template <typename T, std::size_t N> |
| 62 |
class is_cxx_array<std::array<T, N>> : public std::true_type |
| 63 |
{ |
| 64 |
public: |
| 65 |
using array_type = std::array<T, N>; |
| 66 |
}; |
| 67 |
|
| 68 |
template <typename T> |
| 69 |
class is_cxx_array<const T> : public is_cxx_array<T> |
| 70 |
{ |
| 71 |
}; |
| 72 |
|
| 73 |
template <typename T, std::size_t N, typename E> |
| 74 |
class is_cxx_array<dcx::enumerated_array<T, N, E>> : public is_cxx_array<std::array<T, N>> |
| 75 |
{ |
| 76 |
}; |
| 77 |
|
| 78 |
template <typename T> |
| 79 |
using is_generic_class = typename std::conditional<is_cxx_array<T>::value, std::false_type, std::is_class<T>>::type; |
| 80 |
|
| 81 |
template <typename Accessor, typename A1> |
| 82 |
requires(std::is_integral<typename std::remove_reference<A1>::type>::value) |
| 83 |
static inline void process_buffer(Accessor &&accessor, A1 &&a1) |
| 84 |
{ |
| 85 |
process_integer(std::forward<Accessor &&>(accessor), a1); |
| 86 |
} |
| 87 |
|
| 88 |
template <typename Accessor, typename A1, typename A1rr = typename std::remove_reference<A1>::type> |
| 89 |
requires(std::is_enum<A1rr>::value) |
| 90 |
static inline void process_buffer(Accessor &, A1 &&); |
| 91 |
|
| 92 |
template <typename Accessor, typename A1, typename A1rr = typename std::remove_reference<A1>::type> |
| 93 |
requires(is_generic_class<A1rr>::value) |
| 94 |
static inline void process_buffer(Accessor &, A1 &&); |
| 95 |
|
| 96 |
template <typename Accessor, typename A1> |
| 97 |
requires(is_cxx_array<A1>::value) |
| 98 |
static void process_buffer(Accessor &&, A1 &); |
| 99 |
|
| 100 |
template <typename Accessor, typename... Args> |
| 101 |
static void process_buffer(Accessor &, const message<Args...> &); |
| 102 |
|
| 103 |
class endian_access |
| 104 |
{ |
| 105 |
public: |
| 106 |
/* |
| 107 |
* Endian access modes: |
| 108 |
* - little_endian: assume buffered data is little endian |
| 109 |
* Copy on little endian host, byte swap on big endian host |
| 110 |
* - big_endian: assume buffered data is big endian |
| 111 |
* Copy on big endian host, byte swap on little endian host |
| 112 |
* - native_endian: assume buffered data is native endian |
| 113 |
* Copy regardless of host byte order |
| 114 |
*/ |
| 115 |
using little_endian_type = std::integral_constant<std::endian, std::endian::little>; |
| 116 |
using big_endian_type = std::integral_constant<std::endian, std::endian::big>; |
| 117 |
using native_endian_type = std::integral_constant<std::endian, std::endian::native>; |
| 118 |
}; |
| 119 |
|
| 120 |
/* Implementation details - avoid namespace pollution */ |
| 121 |
namespace detail { |
| 122 |
|
| 123 |
template <typename T, typename Trr = typename std::remove_reference<T>::type> |
| 124 |
using capture_type = typename std::conditional<std::is_lvalue_reference<T>::value, |
| 125 |
std::reference_wrapper<Trr>, |
| 126 |
std::tuple<Trr> |
| 127 |
>; |
| 128 |
|
| 129 |
template <typename T, typename Trr = typename std::remove_reference<T>::type> |
| 130 |
static inline auto capture_value(Trr &t) |
| 131 |
{ |
| 132 |
return std::ref(t); |
| 133 |
} |
| 134 |
|
| 135 |
template <typename T, typename Trr = typename std::remove_reference<T>::type> |
| 136 |
requires(std::is_rvalue_reference<T>::value) |
| 137 |
static inline auto capture_value(Trr &&t) |
| 138 |
{ |
| 139 |
return std::tuple<Trr>{std::forward<T>(t)}; |
| 140 |
} |
| 141 |
|
| 142 |
template <typename extended_signed_type, typename wrapped_type> |
| 143 |
class sign_extend_type : std::reference_wrapper<wrapped_type> |
| 144 |
{ |
| 145 |
static_assert(sizeof(extended_signed_type) > sizeof(wrapped_type), "cannot sign-extend into a type of equal or smaller size"); |
| 146 |
static_assert(std::is_signed<extended_signed_type>::value, "cannot sign-extend into an unsigned type"); |
| 147 |
using base_type = std::reference_wrapper<wrapped_type>; |
| 148 |
public: |
| 149 |
using base_type::base_type; |
| 150 |
using base_type::get; |
| 151 |
}; |
| 152 |
|
| 153 |
template <typename extended_signed_type, typename wrapped_type> |
| 154 |
message<std::array<uint8_t, sizeof(extended_signed_type)>> udt_to_message(const sign_extend_type<extended_signed_type, wrapped_type> &); |
| 155 |
|
| 156 |
template <std::size_t amount, uint8_t value> |
| 157 |
class pad_type |
| 158 |
{ |
| 159 |
}; |
| 160 |
|
| 161 |
template <std::size_t amount, uint8_t value> |
| 162 |
message<std::array<uint8_t, amount>> udt_to_message(const pad_type<amount, value> &); |
| 163 |
|
| 164 |
/* |
| 165 |
* This can never be instantiated, but will be requested if a UDT |
| 166 |
* specialization is missing. |
| 167 |
*/ |
| 168 |
template <typename T> |
| 169 |
class missing_udt_specialization |
| 170 |
{ |
| 171 |
public: |
| 172 |
missing_udt_specialization() = delete; |
| 173 |
}; |
| 174 |
|
| 175 |
template <typename T> |
| 176 |
void udt_to_message(T &, missing_udt_specialization<T> = missing_udt_specialization<T>()); |
| 177 |
|
| 178 |
static void preprocess_udt(auto && /* accessor */, auto & /* udt */) {} |
| 179 |
|
| 180 |
static void postprocess_udt(auto && /* accessor */, auto & /* udt */) {} |
| 181 |
|
| 182 |
template <typename Accessor, typename UDT> |
| 183 |
static inline void process_udt(Accessor &&accessor, UDT &udt) |
| 184 |
{ |
| 185 |
process_buffer(std::forward<Accessor &&>(accessor), udt_to_message(udt)); |
| 186 |
} |
| 187 |
|
| 188 |
template <typename E> |
| 189 |
void check_enum(auto && /* accessor */, E) {} |
| 190 |
|
| 191 |
template <typename buffer_value_type> |
| 192 |
struct bytebuffer : endian_access |
| 193 |
{ |
| 194 |
using iterator_category = std::random_access_iterator_tag; |
| 195 |
using value_type = buffer_value_type; |
| 196 |
using difference_type = std::ptrdiff_t; |
| 197 |
using pointer = value_type *; |
| 198 |
using reference = value_type &; |
| 199 |
constexpr bytebuffer(const pointer u) : p{u} {} |
| 200 |
operator pointer() const { return p; } |
| 201 |
void operator++() |
| 202 |
{ |
| 203 |
++p; |
| 204 |
} |
| 205 |
void operator--() |
| 206 |
{ |
| 207 |
--p; |
| 208 |
} |
| 209 |
void operator+=(const difference_type d) |
| 210 |
{ |
| 211 |
p += d; |
| 212 |
} |
| 213 |
operator const void *() const = delete; |
| 214 |
protected: |
| 215 |
pointer p; |
| 216 |
}; |
| 217 |
|
| 218 |
#define SERIAL_UDT_ROUND_UP(X,M) (((X) + (M) - 1) & ~((M) - 1)) |
| 219 |
template <std::size_t amount, |
| 220 |
std::size_t SERIAL_UDT_ROUND_MULTIPLIER = sizeof(void *), |
| 221 |
std::size_t SERIAL_UDT_ROUND_UP_AMOUNT = SERIAL_UDT_ROUND_UP(amount, SERIAL_UDT_ROUND_MULTIPLIER), |
| 222 |
std::size_t FULL_SIZE = amount / 64 ? 64 : SERIAL_UDT_ROUND_UP_AMOUNT, |
| 223 |
std::size_t REMAINDER_SIZE = amount % 64> |
| 224 |
union pad_storage |
| 225 |
{ |
| 226 |
static_assert(amount % SERIAL_UDT_ROUND_MULTIPLIER ? SERIAL_UDT_ROUND_UP_AMOUNT > amount && SERIAL_UDT_ROUND_UP_AMOUNT < amount + SERIAL_UDT_ROUND_MULTIPLIER : SERIAL_UDT_ROUND_UP_AMOUNT == amount, "round up error"); |
| 227 |
static_assert(SERIAL_UDT_ROUND_UP_AMOUNT % SERIAL_UDT_ROUND_MULTIPLIER == 0, "round modulus error"); |
| 228 |
static_assert(amount % FULL_SIZE == REMAINDER_SIZE || FULL_SIZE == REMAINDER_SIZE, "padding alignment error"); |
| 229 |
std::array<uint8_t, FULL_SIZE> f; |
| 230 |
std::array<uint8_t, REMAINDER_SIZE> p; |
| 231 |
#undef SERIAL_UDT_ROUND_UP |
| 232 |
}; |
| 233 |
|
| 234 |
template <typename Accessor, std::size_t amount, uint8_t value> |
| 235 |
static inline void process_udt(Accessor &&accessor, const pad_type<amount, value> &) |
| 236 |
{ |
| 237 |
/* If reading from accessor, accessor data is const and buffer is |
| 238 |
* overwritten by read. |
| 239 |
* If writing to accessor, accessor data is non-const, so initialize |
| 240 |
* buffer to be written. |
| 241 |
*/ |
| 242 |
pad_storage<amount> s; |
| 243 |
if constexpr (!std::is_const< |
| 244 |
typename std::remove_pointer< |
| 245 |
/* rvalue reference `Accessor &&` causes `Accessor` to be `T &` |
| 246 |
* for some type T. Use std::remove_reference to get T. Then |
| 247 |
* take the type `pointer` from type T to use as input to |
| 248 |
* std::remove_pointer. |
| 249 |
*/ |
| 250 |
typename std::remove_reference<Accessor>::type |
| 251 |
::pointer |
| 252 |
>::type |
| 253 |
>::value) |
| 254 |
s.f.fill(value); |
| 255 |
for (std::size_t count{amount}; count; count -= s.f.size()) |
| 256 |
{ |
| 257 |
if (count < s.f.size()) |
| 258 |
{ |
| 259 |
assert(count == s.p.size()); |
| 260 |
process_buffer(accessor, s.p); |
| 261 |
break; |
| 262 |
} |
| 263 |
process_buffer(accessor, s.f); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
template <typename T> |
| 268 |
static inline T &extract_value(std::reference_wrapper<T> t) |
| 269 |
{ |
| 270 |
return t; |
| 271 |
} |
| 272 |
|
| 273 |
template <typename T> |
| 274 |
static inline T &extract_value(std::tuple<T> &t) |
| 275 |
{ |
| 276 |
return std::get<0>(t); |
| 277 |
} |
| 278 |
|
| 279 |
template <typename T> |
| 280 |
static inline const T &extract_value(const std::tuple<T> &t) |
| 281 |
{ |
| 282 |
return std::get<0>(t); |
| 283 |
} |
| 284 |
|
| 285 |
template <typename T> |
| 286 |
struct message_dispatch_base |
| 287 |
{ |
| 288 |
using effective_type = T; |
| 289 |
}; |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
template <std::size_t amount, uint8_t value = 0xcc> |
| 294 |
using pad = detail::pad_type<amount, value>; |
| 295 |
|
| 296 |
template <typename extended_signed_type, typename wrapped_type> |
| 297 |
static inline detail::sign_extend_type<extended_signed_type, wrapped_type> sign_extend(wrapped_type &t) |
| 298 |
{ |
| 299 |
return {t}; |
| 300 |
} |
| 301 |
|
| 302 |
#define DEFINE_SERIAL_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) \ |
| 303 |
DEFINE_SERIAL_CONST_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) \ |
| 304 |
DEFINE_SERIAL_MUTABLE_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) \ |
| 305 |
|
| 306 |
#define _DEFINE_SERIAL_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) \ |
| 307 |
template <typename Accessor> \ |
| 308 |
static inline void process_udt(Accessor &&accessor, TYPE &NAME) \ |
| 309 |
{ \ |
| 310 |
using serial::process_buffer; \ |
| 311 |
process_buffer(std::forward<Accessor &&>(accessor), _SERIAL_UDT_UNWRAP_LIST MEMBERLIST); \ |
| 312 |
} \ |
| 313 |
\ |
| 314 |
dxx_compiler_attribute_unused \ |
| 315 |
static inline auto udt_to_message(TYPE &NAME) { \ |
| 316 |
return serial::message MEMBERLIST; \ |
| 317 |
} |
| 318 |
|
| 319 |
#define DEFINE_SERIAL_CONST_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) \ |
| 320 |
_DEFINE_SERIAL_UDT_TO_MESSAGE(const TYPE, NAME, MEMBERLIST) |
| 321 |
#define DEFINE_SERIAL_MUTABLE_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) \ |
| 322 |
_DEFINE_SERIAL_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) |
| 323 |
|
| 324 |
template <std::size_t Expected, typename T> |
| 325 |
concept check_serial_udt_maximum_message_size = check_equal_value<Expected, T::maximum_size.value>; |
| 326 |
|
| 327 |
#define ASSERT_SERIAL_UDT_MESSAGE_SIZE(T, SIZE) \ |
| 328 |
static_assert(serial::check_serial_udt_maximum_message_size<SIZE, serial::class_type<T>>) |
| 329 |
|
| 330 |
template <typename M1, typename T1, typename base_type = std::is_same<typename std::remove_cv<typename std::remove_reference<M1>::type>::type, T1>> |
| 331 |
struct udt_message_compatible_same_type : base_type |
| 332 |
{ |
| 333 |
static_assert(base_type::value, "parameter type mismatch"); |
| 334 |
}; |
| 335 |
|
| 336 |
template <bool, typename M, typename T> |
| 337 |
class assert_udt_message_compatible2; |
| 338 |
|
| 339 |
template <typename M, typename T> |
| 340 |
class assert_udt_message_compatible2<false, M, T> : public std::false_type |
| 341 |
{ |
| 342 |
}; |
| 343 |
|
| 344 |
template <typename... Mn, typename... Tn> |
| 345 |
class assert_udt_message_compatible2<true, message<Mn...>, std::tuple<Tn...>> : |
| 346 |
public std::integral_constant<bool, (udt_message_compatible_same_type<Mn, Tn>::value && ...)> |
| 347 |
{ |
| 348 |
}; |
| 349 |
|
| 350 |
template <typename M, typename T> |
| 351 |
class assert_udt_message_compatible; |
| 352 |
|
| 353 |
template <typename... Mn, typename... Tn> |
| 354 |
class assert_udt_message_compatible<message<Mn...>, std::tuple<Tn...>> : public assert_udt_message_compatible2<sizeof...(Mn) == sizeof...(Tn), message<Mn...>, std::tuple<Tn...>> |
| 355 |
{ |
| 356 |
static_assert(sizeof...(Mn) <= sizeof...(Tn), "too few types in tuple"); |
| 357 |
static_assert(sizeof...(Mn) >= sizeof...(Tn), "too few types in message"); |
| 358 |
}; |
| 359 |
|
| 360 |
template <typename T> |
| 361 |
using class_type = message_type<decltype(udt_to_message(std::declval<T>()))>; |
| 362 |
|
| 363 |
#define _SERIAL_UDT_UNWRAP_LIST(A1,...) A1, ## __VA_ARGS__ |
| 364 |
|
| 365 |
#define ASSERT_SERIAL_UDT_MESSAGE_TYPE(T, TYPELIST) \ |
| 366 |
ASSERT_SERIAL_UDT_MESSAGE_CONST_TYPE(T, TYPELIST); \ |
| 367 |
ASSERT_SERIAL_UDT_MESSAGE_MUTABLE_TYPE(T, TYPELIST); \ |
| 368 |
|
| 369 |
#define _ASSERT_SERIAL_UDT_MESSAGE_TYPE(T, TYPELIST) \ |
| 370 |
static_assert(serial::assert_udt_message_compatible<typename class_type<T>::as_message, std::tuple<_SERIAL_UDT_UNWRAP_LIST TYPELIST>>::value, "udt/message mismatch") |
| 371 |
|
| 372 |
#define ASSERT_SERIAL_UDT_MESSAGE_CONST_TYPE(T, TYPELIST) \ |
| 373 |
_ASSERT_SERIAL_UDT_MESSAGE_TYPE(const T, TYPELIST) |
| 374 |
#define ASSERT_SERIAL_UDT_MESSAGE_MUTABLE_TYPE(T, TYPELIST) \ |
| 375 |
_ASSERT_SERIAL_UDT_MESSAGE_TYPE(T, TYPELIST) |
| 376 |
|
| 377 |
static constexpr uint8_t endian_skip_byteswap(std::endian E) |
| 378 |
{ |
| 379 |
/* If this static_assert fails, then the comparison may return the wrong |
| 380 |
* result. |
| 381 |
*/ |
| 382 |
static_assert(std::endian::little == std::endian::native || std::endian::big == std::endian::native, "host byte order must be little endian or big endian"); |
| 383 |
return E == std::endian::native; |
| 384 |
} |
| 385 |
|
| 386 |
template <typename T, std::size_t N> |
| 387 |
union unaligned_storage |
| 388 |
{ |
| 389 |
T a; |
| 390 |
typename std::conditional<N < 4, |
| 391 |
typename std::conditional<N == 1, uint8_t, uint16_t>, |
| 392 |
typename std::conditional<N == 4, uint32_t, uint64_t>>::type::type i; |
| 393 |
uint8_t u[N]; |
| 394 |
static_assert(check_equal_value<N, sizeof(i)>); |
| 395 |
static_assert(check_equal_value<sizeof(u), sizeof(a)>); |
| 396 |
}; |
| 397 |
|
| 398 |
template <typename T, typename = void> |
| 399 |
class message_dispatch_type; |
| 400 |
|
| 401 |
template <typename T> |
| 402 |
class message_dispatch_type<T, typename std::enable_if<std::is_integral<T>::value or std::is_enum<T>::value, void>::type> : |
| 403 |
public detail::message_dispatch_base<detail::size_base<sizeof(T)>> |
| 404 |
{ |
| 405 |
}; |
| 406 |
|
| 407 |
template <typename T> |
| 408 |
class message_dispatch_type<T, typename std::enable_if<is_cxx_array<T>::value, void>::type> : |
| 409 |
public detail::message_dispatch_base< |
| 410 |
detail::size_base< |
| 411 |
message_type<typename T::value_type>::maximum_size * std::tuple_size<typename is_cxx_array<T>::array_type>::value, |
| 412 |
message_type<typename T::value_type>::minimum_size * std::tuple_size<typename is_cxx_array<T>::array_type>::value |
| 413 |
> |
| 414 |
> |
| 415 |
{ |
| 416 |
}; |
| 417 |
|
| 418 |
template <typename T> |
| 419 |
class message_dispatch_type<T, typename std::enable_if<is_generic_class<T>::value && !is_message<T>::value, void>::type> : |
| 420 |
public detail::message_dispatch_base<class_type<T>> |
| 421 |
{ |
| 422 |
}; |
| 423 |
|
| 424 |
template <typename T> |
| 425 |
class message_type : |
| 426 |
message_dispatch_type<typename std::remove_reference<T>::type>::effective_type |
| 427 |
{ |
| 428 |
using effective_type = typename message_dispatch_type<typename std::remove_reference<T>::type>::effective_type; |
| 429 |
public: |
| 430 |
using effective_type::maximum_size; |
| 431 |
using effective_type::minimum_size; |
| 432 |
}; |
| 433 |
|
| 434 |
template <typename A1> |
| 435 |
class message_dispatch_type<message<A1>, void> : |
| 436 |
public detail::message_dispatch_base<message_type<A1>> |
| 437 |
{ |
| 438 |
public: |
| 439 |
typedef message<A1> as_message; |
| 440 |
}; |
| 441 |
|
| 442 |
template <typename... Args> |
| 443 |
class message_type<message<Args...>> : |
| 444 |
public detail::size_base< |
| 445 |
(0 + ... + message_dispatch_type<message<Args>>::effective_type::maximum_size), |
| 446 |
(0 + ... + message_dispatch_type<message<Args>>::effective_type::minimum_size) |
| 447 |
> |
| 448 |
{ |
| 449 |
public: |
| 450 |
using as_message = message<Args...>; |
| 451 |
}; |
| 452 |
|
| 453 |
template <typename... Args> |
| 454 |
class message |
| 455 |
{ |
| 456 |
static_assert(sizeof...(Args) > 0, "message must have at least one template argument"); |
| 457 |
using tuple_type = std::tuple<typename detail::capture_type<Args &&>::type...>; |
| 458 |
template <typename T1> |
| 459 |
static void check_type() |
| 460 |
{ |
| 461 |
static_assert(message_type<T1>::maximum_size > 0, "empty field in message"); |
| 462 |
} |
| 463 |
tuple_type t; |
| 464 |
public: |
| 465 |
message(Args &&... args) : |
| 466 |
t{detail::capture_value<Args>(std::forward<Args>(args))...} |
| 467 |
{ |
| 468 |
(check_type<Args>(), ...); |
| 469 |
} |
| 470 |
const tuple_type &get_tuple() const |
| 471 |
{ |
| 472 |
return t; |
| 473 |
} |
| 474 |
}; |
| 475 |
|
| 476 |
template <typename... Args> |
| 477 |
message(Args &&... args) -> message<Args && ...>; |
| 478 |
|
| 479 |
template <typename T> |
| 480 |
static constexpr T bswap(const T u) |
| 481 |
{ |
| 482 |
if constexpr (std::is_same<T, int8_t>::value || std::is_same<T, uint8_t>::value) |
| 483 |
/* Swapping a byte-sized value is a no-op. This is permitted here so |
| 484 |
* that callers can swap without checking the size of the value. |
| 485 |
*/ |
| 486 |
return u; |
| 487 |
if constexpr (std::is_same<T, int16_t>::value || std::is_same<T, uint16_t>::value) |
| 488 |
{ |
| 489 |
#ifdef DXX_HAVE_BUILTIN_BSWAP |
| 490 |
return __builtin_bswap16(u); |
| 491 |
#else |
| 492 |
return (static_cast<T>(static_cast<uint8_t>(u)) << 8) | static_cast<T>(static_cast<uint8_t>(u >> 8)); |
| 493 |
#endif |
| 494 |
} |
| 495 |
if constexpr (std::is_same<T, int32_t>::value || std::is_same<T, uint32_t>::value) |
| 496 |
{ |
| 497 |
#ifdef DXX_HAVE_BUILTIN_BSWAP |
| 498 |
return __builtin_bswap32(u); |
| 499 |
#else |
| 500 |
return (static_cast<T>(bswap(static_cast<uint16_t>(u))) << 16) | static_cast<T>(bswap(static_cast<uint16_t>(u >> 16))); |
| 501 |
#endif |
| 502 |
} |
| 503 |
if constexpr (std::is_same<T, int64_t>::value || std::is_same<T, uint64_t>::value) |
| 504 |
{ |
| 505 |
#ifdef DXX_HAVE_BUILTIN_BSWAP |
| 506 |
return __builtin_bswap64(u); |
| 507 |
#else |
| 508 |
return (static_cast<T>(bswap(static_cast<uint32_t>(u))) << 32) | static_cast<T>(bswap(static_cast<uint32_t>(u >> 32))); |
| 509 |
#endif |
| 510 |
} |
| 511 |
/* Unsupported type. Fall off the end of the function and trigger a |
| 512 |
* compile error due to the missing `return` statement. |
| 513 |
*/ |
| 514 |
} |
| 515 |
|
| 516 |
template <typename T, T Expected, T input> |
| 517 |
concept check_bswap = check_equal_value<Expected, bswap(input)>; |
| 518 |
|
| 519 |
static_assert(check_bswap<uint8_t, 1, 1>); |
| 520 |
|
| 521 |
static_assert(check_bswap<uint16_t, 0x1200, 0x12>); |
| 522 |
static_assert(check_bswap<uint16_t, 0x9200, 0x92>); |
| 523 |
static_assert(check_bswap<uint16_t, 0x92, 0x9200>); |
| 524 |
static_assert(check_bswap<uint16_t, 0x201, 0x102>); |
| 525 |
|
| 526 |
static_assert(check_bswap<uint32_t, 0x2010000, 0x102>); |
| 527 |
|
| 528 |
static_assert(check_bswap<uint64_t, 0x201000000000000ull, 0x102>); |
| 529 |
|
| 530 |
namespace reader { |
| 531 |
|
| 532 |
template <std::endian endian_value> |
| 533 |
struct bytebuffer : detail::bytebuffer<const uint8_t> |
| 534 |
{ |
| 535 |
using detail::bytebuffer<const uint8_t>::bytebuffer; |
| 536 |
explicit bytebuffer(const bytebuffer &) = default; |
| 537 |
bytebuffer(bytebuffer &&) = default; |
| 538 |
static constexpr std::integral_constant<std::endian, endian_value> endian{}; |
| 539 |
}; |
| 540 |
using be_bytebuffer = bytebuffer<std::endian::big>; |
| 541 |
using le_bytebuffer = bytebuffer<std::endian::little>; |
| 542 |
using ne_bytebuffer = bytebuffer<std::endian::native>; |
| 543 |
|
| 544 |
template <typename A1, std::size_t BYTES> |
| 545 |
static inline void unaligned_copy(const uint8_t *src, unaligned_storage<A1, BYTES> &dst) |
| 546 |
{ |
| 547 |
if constexpr (BYTES == 1) |
| 548 |
dst.u[0] = {*src}; |
| 549 |
else |
| 550 |
std::copy_n(src, sizeof(dst.u), dst.u); |
| 551 |
} |
| 552 |
|
| 553 |
template <typename Accessor, typename A1> |
| 554 |
static inline void process_integer(Accessor &buffer, A1 &a1) |
| 555 |
{ |
| 556 |
using std::advance; |
| 557 |
unaligned_storage<A1, message_type<A1>::maximum_size> u; |
| 558 |
unaligned_copy(buffer, u); |
| 559 |
if (!endian_skip_byteswap(buffer.endian())) |
| 560 |
u.i = {bswap(u.i)}; |
| 561 |
a1 = u.a; |
| 562 |
advance(buffer, sizeof(u.u)); |
| 563 |
} |
| 564 |
|
| 565 |
template <typename Accessor, typename A, typename T = typename A::value_type> |
| 566 |
/* When the type is an array of byte-size integers (typically, `char` or |
| 567 |
* `uint8_t`), enable this overload for more efficient copying. |
| 568 |
*/ |
| 569 |
requires(sizeof(T) == 1 && std::is_integral<T>::value) |
| 570 |
static inline void process_array(Accessor &accessor, A &a) |
| 571 |
{ |
| 572 |
using std::advance; |
| 573 |
std::copy_n(static_cast<typename Accessor::pointer>(accessor), a.size(), a.data()); |
| 574 |
advance(accessor, a.size()); |
| 575 |
} |
| 576 |
|
| 577 |
template <typename Accessor, typename extended_signed_type, typename wrapped_type> |
| 578 |
static inline void process_udt(Accessor &&accessor, const detail::sign_extend_type<extended_signed_type, wrapped_type> &v) |
| 579 |
{ |
| 580 |
extended_signed_type est; |
| 581 |
process_integer(static_cast<Accessor &&>(accessor), est); |
| 582 |
v.get() = static_cast<wrapped_type>(est); |
| 583 |
} |
| 584 |
|
| 585 |
} |
| 586 |
|
| 587 |
namespace writer { |
| 588 |
|
| 589 |
template <std::endian endian_value> |
| 590 |
struct bytebuffer : detail::bytebuffer<uint8_t> |
| 591 |
{ |
| 592 |
using detail::bytebuffer<uint8_t>::bytebuffer; |
| 593 |
explicit bytebuffer(const bytebuffer &) = default; |
| 594 |
bytebuffer(bytebuffer &&) = default; |
| 595 |
static constexpr std::integral_constant<std::endian, endian_value> endian{}; |
| 596 |
}; |
| 597 |
using be_bytebuffer = bytebuffer<std::endian::big>; |
| 598 |
using le_bytebuffer = bytebuffer<std::endian::little>; |
| 599 |
using ne_bytebuffer = bytebuffer<std::endian::native>; |
| 600 |
|
| 601 |
/* If unaligned_copy is manually inlined into the caller, then gcc |
| 602 |
* inlining of copy_n creates a loop instead of a store. |
| 603 |
*/ |
| 604 |
template <typename A1, std::size_t BYTES> |
| 605 |
static inline void unaligned_copy(const unaligned_storage<A1, BYTES> &src, uint8_t *dst) |
| 606 |
{ |
| 607 |
if constexpr (BYTES == 1) |
| 608 |
*dst = {src.u[0]}; |
| 609 |
else |
| 610 |
std::copy_n(src.u, sizeof(src.u), dst); |
| 611 |
} |
| 612 |
|
| 613 |
template <typename Accessor, typename A1> |
| 614 |
static inline void process_integer(Accessor &buffer, const A1 &a1) |
| 615 |
{ |
| 616 |
using std::advance; |
| 617 |
unaligned_storage<A1, message_type<A1>::maximum_size> u{a1}; |
| 618 |
if (!endian_skip_byteswap(buffer.endian())) |
| 619 |
u.i = {bswap(u.i)}; |
| 620 |
unaligned_copy(u, buffer); |
| 621 |
advance(buffer, sizeof(u.u)); |
| 622 |
} |
| 623 |
|
| 624 |
template <typename Accessor, typename A, typename T = typename A::value_type> |
| 625 |
requires(sizeof(T) == 1 && std::is_integral<T>::value) |
| 626 |
static inline void process_array(Accessor &accessor, const A &a) |
| 627 |
{ |
| 628 |
using std::advance; |
| 629 |
std::copy_n(a.data(), a.size(), static_cast<typename Accessor::pointer>(accessor)); |
| 630 |
advance(accessor, a.size()); |
| 631 |
} |
| 632 |
|
| 633 |
template <typename Accessor, typename extended_signed_type, typename wrapped_type> |
| 634 |
static inline void process_udt(Accessor &&accessor, const detail::sign_extend_type<extended_signed_type, const wrapped_type> &v) |
| 635 |
{ |
| 636 |
const typename std::make_signed<wrapped_type>::type swt = v.get(); |
| 637 |
process_integer(static_cast<Accessor &&>(accessor), extended_signed_type{swt}); |
| 638 |
} |
| 639 |
|
| 640 |
} |
| 641 |
|
| 642 |
template <typename Accessor, typename A1, typename A1rr> |
| 643 |
requires(std::is_enum<A1rr>::value) |
| 644 |
static inline void process_buffer(Accessor &accessor, A1 &&a1) |
| 645 |
{ |
| 646 |
using detail::check_enum; |
| 647 |
process_integer(accessor, a1); |
| 648 |
/* Hook for enum types to check that the given value is legal */ |
| 649 |
check_enum(accessor, a1); |
| 650 |
} |
| 651 |
|
| 652 |
template <typename Accessor, typename A1, typename A1rr> |
| 653 |
requires(is_generic_class<A1rr>::value) |
| 654 |
static inline void process_buffer(Accessor &accessor, A1 &&a1) |
| 655 |
{ |
| 656 |
using detail::preprocess_udt; |
| 657 |
using detail::process_udt; |
| 658 |
using detail::postprocess_udt; |
| 659 |
preprocess_udt(accessor, a1); |
| 660 |
process_udt(accessor, std::forward<A1>(a1)); |
| 661 |
postprocess_udt(accessor, a1); |
| 662 |
} |
| 663 |
|
| 664 |
template <typename Accessor, typename A, typename T = typename A::value_type> |
| 665 |
requires(!(sizeof(T) == 1 && std::is_integral<T>::value)) |
| 666 |
static void process_array(Accessor &accessor, A &a) |
| 667 |
{ |
| 668 |
range_for (auto &i, a) |
| 669 |
process_buffer(accessor, i); |
| 670 |
} |
| 671 |
|
| 672 |
template <typename Accessor, typename A1> |
| 673 |
requires(is_cxx_array<A1>::value) |
| 674 |
static void process_buffer(Accessor &&accessor, A1 &a1) |
| 675 |
{ |
| 676 |
process_array(std::forward<Accessor &&>(accessor), a1); |
| 677 |
} |
| 678 |
|
| 679 |
template <typename Accessor, typename... Args, std::size_t... N> |
| 680 |
static inline void process_message_tuple(Accessor &&accessor, const std::tuple<Args...> &t, std::index_sequence<N...>) |
| 681 |
{ |
| 682 |
(process_buffer(accessor, detail::extract_value(std::get<N>(t))), ...); |
| 683 |
} |
| 684 |
|
| 685 |
template <typename Accessor, typename... Args> |
| 686 |
static void process_buffer(Accessor &&accessor, const message<Args...> &m) |
| 687 |
{ |
| 688 |
process_message_tuple(std::forward<Accessor &&>(accessor), m.get_tuple(), std::make_index_sequence<sizeof...(Args)>()); |
| 689 |
} |
| 690 |
|
| 691 |
/* Require at least two arguments to prevent self-selection */ |
| 692 |
template <typename Accessor, typename... An> |
| 693 |
requires(sizeof...(An) > 1) |
| 694 |
static void process_buffer(Accessor &&accessor, An &&... an) |
| 695 |
{ |
| 696 |
(process_buffer(accessor, std::forward<An>(an)), ...); |
| 697 |
} |
| 698 |
|
| 699 |
} |