1234567891011121314151617181920212223242526272829303132333435363738394041 |
- #ifndef CORE_ALIGNED_DATA_HPP
- #define CORE_ALIGNED_DATA_HPP
- #define CORE_ASSERT_ALIGNED_DATA(var, type) \
- static_assert(sizeof(var) == sizeof(type), "aligned data size missmatch"); \
- static_assert(decltype(var)::getSize() >= decltype(var)::getAlignment(), \
- "size >= alignment"); \
- static_assert(alignof(decltype(var)) == alignof(type), \
- "aligned data alignment missmatch");
- namespace Core {
- template<int SIZE, int ALIGNMENT>
- class alignas(ALIGNMENT) AlignedData final {
- static_assert(SIZE > 0, "size must be positive");
- char buffer[static_cast<unsigned int>(SIZE)] = {};
- public:
- template<typename T>
- constexpr T* as() {
- return reinterpret_cast<T*>(this);
- }
- template<typename T>
- constexpr const T* as() const {
- return reinterpret_cast<T*>(this);
- }
- static consteval int getSize() {
- return SIZE;
- }
- static consteval int getAlignment() {
- return ALIGNMENT;
- }
- };
- template<typename T>
- using AlignedType = AlignedData<sizeof(T), alignof(T)>;
- }
- #endif
|