-
Notifications
You must be signed in to change notification settings - Fork 0
/
aligned_storage.hpp
72 lines (61 loc) · 2.65 KB
/
aligned_storage.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* \file aligned_storage.hpp
*
* \brief This header defines a utility type for aligned storage
*
* \author Matthew Rodusek ([email protected])
*/
#ifndef BIT_STL_UTILITIES_ALIGNED_STORAGE_HPP
#define BIT_STL_UTILITIES_ALIGNED_STORAGE_HPP
#include "../traits/composition/conjunction.hpp"
#include "../traits/composition/size_constant.hpp"
#include <cstddef> // std::size_t, std::max_align_t
namespace bit {
namespace stl {
namespace detail {
template<std::size_t Max, std::size_t...Sizes>
struct aligned_storage_max;
template<std::size_t Max, std::size_t Size0, std::size_t...Sizes>
struct aligned_storage_max<Max,Size0,Sizes...>
: aligned_storage_max<(Max > Size0 ? Max : Size0),Sizes...>{};
template<std::size_t Max>
struct aligned_storage_max<Max> : size_constant<Max>{};
} // namespace detail
constexpr auto max_align = alignof(std::max_align_t);
///////////////////////////////////////////////////////////////////////////
/// \brief Provides the nested type type, which is a PODType suitable for
/// use as uninitialized storage for any object whose size is at
/// most Len and whose alignment requirement is a divisor of Align.
///
/// The default value of Align is the most stringent (the largest)
/// alignment requirement for any object whose size is at most Len.
/// If the default value is not used, Align must be the value of
/// alignof(T) for some type T, or the behavior is undefined.
///
/// The behavior is undefined if Len == 0.
///
/// \tparam Len the length of the storage
/// \tparam Align the alignment of the storage
//////////////////////////////////////////////////////////////////////////
template<std::size_t Len, std::size_t Align = max_align>
struct aligned_storage
{
union type
{
char data[Len];
alignas(Align) struct dummy{} dummy;
};
};
/// \brief Type-alias for extracting aligned_storage::type
template<std::size_t Len, std::size_t Align = max_align>
using aligned_storage_t = typename aligned_storage<Len,Align>::type;
/// \brief Type-alias for creating aligned_storage large enough and suitably
/// aligned for all specified Types...
///
/// \tparam Types the types to ensure alignment and size of
template<typename...Types>
using aligned_storage_for = aligned_storage_t<detail::aligned_storage_max<sizeof(Types)...>::value,
detail::aligned_storage_max<alignof(Types)...>::value>;
} // namespace stl
} // namespace bit
#endif /* BIT_STL_UTILITIES_ALIGNED_STORAGE_HPP */