Sane C++ Libraries
C++ Platform Abstraction Libraries
AlignedStorage.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/PrimitiveTypes.h"
5
6namespace SC
7{
8#if !DOXYGEN
9template <typename T, size_t E, size_t R = sizeof(T)>
10void static_assert_size()
11{
12 static_assert(R <= E, "Size mismatch");
13}
14#endif
17
23template <int N, int Alignment = alignof(void*)>
25{
34 template <typename T>
36 {
37 static_assert_size<T, N>();
38 static_assert(alignof(T) <= Alignment, "Increase Alignment of AlignedStorage");
39 return *reinterpret_cast<T*>(bytes);
40 }
41
50 template <typename T>
51 const T& reinterpret_as() const
52 {
53 static_assert_size<T, N>();
54 static_assert(alignof(T) <= Alignment, "Increase Alignment of AlignedStorage");
55 return *reinterpret_cast<const T*>(bytes);
56 }
57
58 private:
59 alignas(Alignment) char bytes[N] = {0};
60};
61
63
64} // namespace SC
A buffer of bytes with given alignment.
Definition: AlignedStorage.h:25
T & reinterpret_as()
Access wanted OS Handle with it's actual type.
Definition: AlignedStorage.h:35
const T & reinterpret_as() const
Access wanted OS Handle with it's actual type.
Definition: AlignedStorage.h:51