AlignedData.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #ifndef CORE_ALIGNED_DATA_HPP
  2. #define CORE_ALIGNED_DATA_HPP
  3. #define CORE_ASSERT_ALIGNED_DATA(var, type) \
  4. static_assert(sizeof(var) == sizeof(type), "aligned data size missmatch"); \
  5. static_assert(var.getSize() >= var.getAlignment(), "size >= alignment"); \
  6. static_assert(alignof(decltype(var)) == alignof(type), \
  7. "aligned data alignment missmatch");
  8. namespace Core {
  9. template<int SIZE, int ALIGNMENT>
  10. class alignas(ALIGNMENT) AlignedData final {
  11. static_assert(SIZE > 0, "size must be positive");
  12. char buffer[static_cast<unsigned int>(SIZE)] = {};
  13. public:
  14. template<typename T>
  15. constexpr T* as() {
  16. return reinterpret_cast<T*>(this);
  17. }
  18. template<typename T>
  19. constexpr const T* as() const {
  20. return reinterpret_cast<T*>(this);
  21. }
  22. static consteval int getSize() {
  23. return SIZE;
  24. }
  25. static consteval int getAlignment() {
  26. return ALIGNMENT;
  27. }
  28. };
  29. template<typename T>
  30. using AlignedType = AlignedData<sizeof(T), alignof(T)>;
  31. }
  32. #endif