array_initializer.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2014-2014
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_INTRUSIVE_DETAIL_ARRAY_INITIALIZER_HPP
  13. #define BOOST_INTRUSIVE_DETAIL_ARRAY_INITIALIZER_HPP
  14. #ifndef BOOST_CONFIG_HPP
  15. # include <boost/config.hpp>
  16. #endif
  17. #if defined(BOOST_HAS_PRAGMA_ONCE)
  18. # pragma once
  19. #endif
  20. #include <boost/config.hpp>
  21. #include <boost/core/no_exceptions_support.hpp>
  22. #include <boost/move/detail/placement_new.hpp>
  23. namespace boost {
  24. namespace intrusive {
  25. namespace detail {
  26. //This is not standard, but should work with all compilers
  27. union max_align
  28. {
  29. char char_;
  30. short short_;
  31. int int_;
  32. long long_;
  33. #ifdef BOOST_HAS_LONG_LONG
  34. ::boost::long_long_type long_long_;
  35. #endif
  36. float float_;
  37. double double_;
  38. long double long_double_;
  39. void * void_ptr_;
  40. };
  41. template<class T, std::size_t N>
  42. class array_initializer
  43. {
  44. public:
  45. template<class CommonInitializer>
  46. array_initializer(const CommonInitializer &init)
  47. {
  48. char *init_buf = (char*)rawbuf;
  49. std::size_t i = 0;
  50. BOOST_TRY{
  51. for(; i != N; ++i){
  52. ::new(init_buf, boost_move_new_t()) T(init);
  53. init_buf += sizeof(T);
  54. }
  55. }
  56. BOOST_CATCH(...){
  57. while(i--){
  58. init_buf -= sizeof(T);
  59. ((T*)init_buf)->~T();
  60. }
  61. BOOST_RETHROW;
  62. }
  63. BOOST_CATCH_END
  64. }
  65. operator T* ()
  66. { return (T*)(rawbuf); }
  67. operator const T*() const
  68. { return (const T*)(rawbuf); }
  69. ~array_initializer()
  70. {
  71. char *init_buf = (char*)rawbuf + N*sizeof(T);
  72. for(std::size_t i = 0; i != N; ++i){
  73. init_buf -= sizeof(T);
  74. ((T*)init_buf)->~T();
  75. }
  76. }
  77. private:
  78. detail::max_align rawbuf[(N*sizeof(T)-1)/sizeof(detail::max_align)+1];
  79. };
  80. } //namespace detail{
  81. } //namespace intrusive{
  82. } //namespace boost{
  83. #endif //BOOST_INTRUSIVE_DETAIL_ARRAY_INITIALIZER_HPP