aligned_storage.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #ifndef BOOST_ALIGNED_STORAGE_HPP
  13. #define BOOST_ALIGNED_STORAGE_HPP
  14. #include <cstddef> // for std::size_t
  15. #include "boost/config.hpp"
  16. #include "boost/detail/workaround.hpp"
  17. #include "boost/type_traits/alignment_of.hpp"
  18. #include "boost/type_traits/type_with_alignment.hpp"
  19. #include "boost/type_traits/is_pod.hpp"
  20. #include "boost/mpl/eval_if.hpp"
  21. #include "boost/mpl/identity.hpp"
  22. #include "boost/type_traits/detail/bool_trait_def.hpp"
  23. namespace boost {
  24. namespace detail { namespace aligned_storage {
  25. BOOST_STATIC_CONSTANT(
  26. std::size_t
  27. , alignment_of_max_align = ::boost::alignment_of<max_align>::value
  28. );
  29. //
  30. // To be TR1 conforming this must be a POD type:
  31. //
  32. template <
  33. std::size_t size_
  34. , std::size_t alignment_
  35. >
  36. struct aligned_storage_imp
  37. {
  38. union data_t
  39. {
  40. char buf[size_];
  41. typename mpl::eval_if_c<
  42. alignment_ == std::size_t(-1)
  43. , mpl::identity<detail::max_align>
  44. , type_with_alignment<alignment_>
  45. >::type align_;
  46. } data_;
  47. void* address() const { return const_cast<aligned_storage_imp*>(this); }
  48. };
  49. template< std::size_t alignment_ >
  50. struct aligned_storage_imp<0u,alignment_>
  51. {
  52. /* intentionally empty */
  53. void* address() const { return 0; }
  54. };
  55. }} // namespace detail::aligned_storage
  56. template <
  57. std::size_t size_
  58. , std::size_t alignment_ = std::size_t(-1)
  59. >
  60. class aligned_storage :
  61. #ifndef __BORLANDC__
  62. private
  63. #else
  64. public
  65. #endif
  66. detail::aligned_storage::aligned_storage_imp<size_, alignment_>
  67. {
  68. public: // constants
  69. typedef detail::aligned_storage::aligned_storage_imp<size_, alignment_> type;
  70. BOOST_STATIC_CONSTANT(
  71. std::size_t
  72. , size = size_
  73. );
  74. BOOST_STATIC_CONSTANT(
  75. std::size_t
  76. , alignment = (
  77. alignment_ == std::size_t(-1)
  78. ? ::boost::detail::aligned_storage::alignment_of_max_align
  79. : alignment_
  80. )
  81. );
  82. #if defined(__GNUC__) &&\
  83. (__GNUC__ > 3) ||\
  84. (__GNUC__ == 3 && (__GNUC_MINOR__ > 2 ||\
  85. (__GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ >=3)))
  86. private: // noncopyable
  87. aligned_storage(const aligned_storage&);
  88. aligned_storage& operator=(const aligned_storage&);
  89. #else // gcc less than 3.2.3
  90. public: // _should_ be noncopyable, but GCC compiler emits error
  91. aligned_storage(const aligned_storage&);
  92. aligned_storage& operator=(const aligned_storage&);
  93. #endif // gcc < 3.2.3 workaround
  94. public: // structors
  95. aligned_storage()
  96. {
  97. }
  98. ~aligned_storage()
  99. {
  100. }
  101. public: // accessors
  102. void* address()
  103. {
  104. return static_cast<type*>(this)->address();
  105. }
  106. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
  107. const void* address() const
  108. {
  109. return static_cast<const type*>(this)->address();
  110. }
  111. #else // MSVC6
  112. const void* address() const;
  113. #endif // MSVC6 workaround
  114. };
  115. #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
  116. // MSVC6 seems not to like inline functions with const void* returns, so we
  117. // declare the following here:
  118. template <std::size_t S, std::size_t A>
  119. const void* aligned_storage<S,A>::address() const
  120. {
  121. return const_cast< aligned_storage<S,A>* >(this)->address();
  122. }
  123. #endif // MSVC6 workaround
  124. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  125. //
  126. // Make sure that is_pod recognises aligned_storage<>::type
  127. // as a POD (Note that aligned_storage<> itself is not a POD):
  128. //
  129. template <std::size_t size_, std::size_t alignment_>
  130. struct is_pod<boost::detail::aligned_storage::aligned_storage_imp<size_,alignment_> >
  131. BOOST_TT_AUX_BOOL_C_BASE(true)
  132. {
  133. BOOST_TT_AUX_BOOL_TRAIT_VALUE_DECL(true)
  134. };
  135. #endif
  136. } // namespace boost
  137. #include "boost/type_traits/detail/bool_trait_undef.hpp"
  138. #endif // BOOST_ALIGNED_STORAGE_HPP