1#pragma once
2
3#include <string.h>
4#include <type_traits>
5
6
7template <typename T>
8inline T unalignedLoad(const void * address)
9{
10 T res {};
11 memcpy(&res, address, sizeof(res));
12 return res;
13}
14
15/// We've had troubles before with wrong store size due to integral promotions
16/// (e.g., unalignedStore(dest, uint16_t + uint16_t) stores an uint32_t).
17/// To prevent this, make the caller specify the stored type explicitly.
18/// To disable deduction of T, wrap the argument type with std::enable_if.
19template <typename T>
20inline void unalignedStore(void * address,
21 const typename std::enable_if<true, T>::type & src)
22{
23 static_assert(std::is_trivially_copyable_v<T>);
24 memcpy(address, &src, sizeof(src));
25}
26