123456789101112131415161718192021222324252627282930313233343536373839404142 |
- #ifndef CORE_ALIGNED_DATA_HPP
- #define CORE_ALIGNED_DATA_HPP
- #define alignmentof(type) __alignof__(type)
- #define ASSERT_ALIGNED_DATA(var, type) \
- static_assert(sizeof(var) == sizeof(type), "aligned data size missmatch"); \
- static_assert(var.getSize() >= var.getAlignment(), "size >= alignment"); \
- static_assert(alignmentof(var) == alignmentof(type), \
- "aligned data alignment missmatch");
- namespace Core {
- template<int SIZE, int ALIGNMENT>
- class alignas(ALIGNMENT) AlignedData {
- static_assert(SIZE > 0, "size must be positive");
- char buffer[static_cast<unsigned int>(SIZE)] = {};
- public:
- template<typename T>
- T* as() {
- return reinterpret_cast<T*>(this);
- }
- template<typename T>
- 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), alignmentof(T)>;
- }
- #endif
|