-
Notifications
You must be signed in to change notification settings - Fork 86
/
aligned_storage.hpp
137 lines (113 loc) · 3.29 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//-----------------------------------------------------------------------------
// boost aligned_storage.hpp header file
// See http://www.boost.org for updates, documentation, and revision history.
//-----------------------------------------------------------------------------
//
// Copyright (c) 2002-2003
// Eric Friedman, Itay Maman
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_TT_ALIGNED_STORAGE_HPP
#define BOOST_TT_ALIGNED_STORAGE_HPP
#include <cstddef> // for std::size_t
#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/type_traits/alignment_of.hpp>
#include <boost/type_traits/type_with_alignment.hpp>
#include <boost/type_traits/is_pod.hpp>
#include <boost/type_traits/conditional.hpp>
namespace boost {
namespace detail { namespace aligned_storage {
BOOST_STATIC_CONSTANT(
std::size_t
, alignment_of_max_align = ::boost::alignment_of<boost::detail::max_align>::value
);
//
// To be TR1 conforming this must be a POD type:
//
template <
std::size_t size_
, std::size_t alignment_
>
struct aligned_storage_imp
{
union data_t
{
unsigned char buf[size_];
typename ::boost::type_with_alignment<alignment_>::type align_;
} data_;
void* address() const { return const_cast<unsigned char*>(data_.buf); }
};
template <std::size_t size>
struct aligned_storage_imp<size, std::size_t(-1)>
{
union data_t
{
unsigned char buf[size];
::boost::detail::max_align align_;
} data_;
void* address() const { return const_cast<unsigned char*>(data_.buf); }
};
template< std::size_t alignment_ >
struct aligned_storage_imp<0u,alignment_>
{
/* intentionally empty */
void* address() const { return 0; }
};
}} // namespace detail::aligned_storage
template <
std::size_t size_
, std::size_t alignment_ = std::size_t(-1)
>
class aligned_storage :
#ifndef BOOST_BORLANDC
private
#else
public
#endif
::boost::detail::aligned_storage::aligned_storage_imp<size_, alignment_>
{
public: // constants
typedef ::boost::detail::aligned_storage::aligned_storage_imp<size_, alignment_> type;
BOOST_STATIC_CONSTANT(
std::size_t
, size = size_
);
BOOST_STATIC_CONSTANT(
std::size_t
, alignment = (
alignment_ == std::size_t(-1)
? ::boost::detail::aligned_storage::alignment_of_max_align
: alignment_
)
);
private: // noncopyable
aligned_storage(const aligned_storage&);
aligned_storage& operator=(const aligned_storage&);
public: // structors
aligned_storage()
{
}
~aligned_storage()
{
}
public: // accessors
void* address()
{
return static_cast<type*>(this)->address();
}
const void* address() const
{
return static_cast<const type*>(this)->address();
}
};
//
// Make sure that is_pod recognises aligned_storage<>::type
// as a POD (Note that aligned_storage<> itself is not a POD):
//
template <std::size_t size_, std::size_t alignment_>
struct is_pod< ::boost::detail::aligned_storage::aligned_storage_imp<size_, alignment_> > : public true_type{};
} // namespace boost
#endif // BOOST_ALIGNED_STORAGE_HPP