aligned_storage.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_TT_ALIGNED_STORAGE_HPP
  13. #define BOOST_TT_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/type_traits/conditional.hpp>
  21. namespace boost {
  22. namespace detail { namespace aligned_storage {
  23. BOOST_STATIC_CONSTANT(
  24. std::size_t
  25. , alignment_of_max_align = ::boost::alignment_of<boost::detail::max_align>::value
  26. );
  27. //
  28. // To be TR1 conforming this must be a POD type:
  29. //
  30. template <
  31. std::size_t size_
  32. , std::size_t alignment_
  33. >
  34. struct aligned_storage_imp
  35. {
  36. union data_t
  37. {
  38. char buf[size_];
  39. typename ::boost::type_with_alignment<alignment_>::type align_;
  40. } data_;
  41. void* address() const { return const_cast<aligned_storage_imp*>(this); }
  42. };
  43. template <std::size_t size>
  44. struct aligned_storage_imp<size, std::size_t(-1)>
  45. {
  46. union data_t
  47. {
  48. char buf[size];
  49. ::boost::detail::max_align align_;
  50. } data_;
  51. void* address() const { return const_cast<aligned_storage_imp*>(this); }
  52. };
  53. template< std::size_t alignment_ >
  54. struct aligned_storage_imp<0u,alignment_>
  55. {
  56. /* intentionally empty */
  57. void* address() const { return 0; }
  58. };
  59. }} // namespace detail::aligned_storage
  60. template <
  61. std::size_t size_
  62. , std::size_t alignment_ = std::size_t(-1)
  63. >
  64. class aligned_storage :
  65. #ifndef __BORLANDC__
  66. private
  67. #else
  68. public
  69. #endif
  70. ::boost::detail::aligned_storage::aligned_storage_imp<size_, alignment_>
  71. {
  72. public: // constants
  73. typedef ::boost::detail::aligned_storage::aligned_storage_imp<size_, alignment_> type;
  74. BOOST_STATIC_CONSTANT(
  75. std::size_t
  76. , size = size_
  77. );
  78. BOOST_STATIC_CONSTANT(
  79. std::size_t
  80. , alignment = (
  81. alignment_ == std::size_t(-1)
  82. ? ::boost::detail::aligned_storage::alignment_of_max_align
  83. : alignment_
  84. )
  85. );
  86. private: // noncopyable
  87. aligned_storage(const aligned_storage&);
  88. aligned_storage& operator=(const aligned_storage&);
  89. public: // structors
  90. aligned_storage()
  91. {
  92. }
  93. ~aligned_storage()
  94. {
  95. }
  96. public: // accessors
  97. void* address()
  98. {
  99. return static_cast<type*>(this)->address();
  100. }
  101. const void* address() const
  102. {
  103. return static_cast<const type*>(this)->address();
  104. }
  105. };
  106. //
  107. // Make sure that is_pod recognises aligned_storage<>::type
  108. // as a POD (Note that aligned_storage<> itself is not a POD):
  109. //
  110. template <std::size_t size_, std::size_t alignment_>
  111. struct is_pod< ::boost::detail::aligned_storage::aligned_storage_imp<size_, alignment_> > : public true_type{};
  112. } // namespace boost
  113. #endif // BOOST_ALIGNED_STORAGE_HPP