1 //-----------------------------------------------------------------------------
2 // boost aligned_storage.hpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
5 //
6 // Copyright (c) 2002-2003
7 // Eric Friedman, Itay Maman
8 //
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
12 
13 #ifndef BOOST_TT_ALIGNED_STORAGE_HPP
14 #define BOOST_TT_ALIGNED_STORAGE_HPP
15 
16 #include <cstddef> // for std::size_t
17 
18 #include <boost/config.hpp>
19 #include <boost/detail/workaround.hpp>
20 #include <boost/type_traits/alignment_of.hpp>
21 #include <boost/type_traits/type_with_alignment.hpp>
22 #include <boost/type_traits/is_pod.hpp>
23 #include <boost/type_traits/conditional.hpp>
24 
25 namespace boost {
26 
27 namespace detail { namespace aligned_storage {
28 
29 BOOST_STATIC_CONSTANT(
30       std::size_t
31     , alignment_of_max_align = ::boost::alignment_of<boost::detail::max_align>::value
32     );
33 
34 //
35 // To be TR1 conforming this must be a POD type:
36 //
37 template <
38       std::size_t size_
39     , std::size_t alignment_
40 >
41 struct aligned_storage_imp
42 {
43     union data_t
44     {
45         char buf[size_];
46 
47         typename ::boost::type_with_alignment<alignment_>::type align_;
48     } data_;
addressboost::detail::aligned_storage::aligned_storage_imp49     void* address() const { return const_cast<aligned_storage_imp*>(this); }
50 };
51 template <std::size_t size>
52 struct aligned_storage_imp<size, std::size_t(-1)>
53 {
54    union data_t
55    {
56       char buf[size];
57       ::boost::detail::max_align align_;
58    } data_;
addressboost::detail::aligned_storage::aligned_storage_imp59    void* address() const { return const_cast<aligned_storage_imp*>(this); }
60 };
61 
62 template< std::size_t alignment_ >
63 struct aligned_storage_imp<0u,alignment_>
64 {
65     /* intentionally empty */
addressboost::detail::aligned_storage::aligned_storage_imp66     void* address() const { return 0; }
67 };
68 
69 }} // namespace detail::aligned_storage
70 
71 template <
72       std::size_t size_
73     , std::size_t alignment_ = std::size_t(-1)
74 >
75 class aligned_storage :
76 #ifndef __BORLANDC__
77    private
78 #else
79    public
80 #endif
81    ::boost::detail::aligned_storage::aligned_storage_imp<size_, alignment_>
82 {
83 
84 public: // constants
85 
86     typedef ::boost::detail::aligned_storage::aligned_storage_imp<size_, alignment_> type;
87 
88     BOOST_STATIC_CONSTANT(
89           std::size_t
90         , size = size_
91         );
92     BOOST_STATIC_CONSTANT(
93           std::size_t
94         , alignment = (
95               alignment_ == std::size_t(-1)
96             ? ::boost::detail::aligned_storage::alignment_of_max_align
97             : alignment_
98             )
99         );
100 
101 private: // noncopyable
102 
103     aligned_storage(const aligned_storage&);
104     aligned_storage& operator=(const aligned_storage&);
105 
106 public: // structors
107 
aligned_storage()108     aligned_storage()
109     {
110     }
111 
~aligned_storage()112     ~aligned_storage()
113     {
114     }
115 
116 public: // accessors
117 
address()118     void* address()
119     {
120         return static_cast<type*>(this)->address();
121     }
122 
address() const123     const void* address() const
124     {
125         return static_cast<const type*>(this)->address();
126     }
127 };
128 
129 //
130 // Make sure that is_pod recognises aligned_storage<>::type
131 // as a POD (Note that aligned_storage<> itself is not a POD):
132 //
133 template <std::size_t size_, std::size_t alignment_>
134 struct is_pod< ::boost::detail::aligned_storage::aligned_storage_imp<size_, alignment_> > : public true_type{};
135 
136 } // namespace boost
137 
138 #endif // BOOST_ALIGNED_STORAGE_HPP
139