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
13#ifndef BOOST_INTRUSIVE_DETAIL_ARRAY_INITIALIZER_HPP
14#define BOOST_INTRUSIVE_DETAIL_ARRAY_INITIALIZER_HPP
15
16#ifndef BOOST_CONFIG_HPP
17# include <boost/config.hpp>
18#endif
19
20#if defined(BOOST_HAS_PRAGMA_ONCE)
21# pragma once
22#endif
23
24#include <boost/config.hpp>
25#include <boost/intrusive/detail/workaround.hpp>
26#include <boost/move/detail/placement_new.hpp>
27#include <boost/move/detail/force_ptr.hpp>
28
29namespace boost {
30namespace intrusive {
31namespace detail {
32
33//This is not standard, but should work with all compilers
34union max_align
35{
36 char char_;
37 short short_;
38 int int_;
39 long long_;
40 #ifdef BOOST_HAS_LONG_LONG
41 ::boost::long_long_type long_long_;
42 #endif
43 float float_;
44 double double_;
45 long double long_double_;
46 void * void_ptr_;
47};
48
49template<class T, std::size_t N>
50class array_initializer
51{
52 public:
53 template<class CommonInitializer>
54 array_initializer(const CommonInitializer &init)
55 {
56 char *init_buf = (char*)rawbuf;
57 std::size_t i = 0;
58 BOOST_INTRUSIVE_TRY{
59 for(; i != N; ++i){
60 ::new(init_buf, boost_move_new_t()) T(init);
61 init_buf += sizeof(T);
62 }
63 }
64 BOOST_INTRUSIVE_CATCH(...){
65 while(i--){
66 init_buf -= sizeof(T);
67 move_detail::force_ptr<T*>(init_buf)->~T();
68 }
69 BOOST_INTRUSIVE_RETHROW;
70 }
71 BOOST_INTRUSIVE_CATCH_END
72 }
73
74 operator T* ()
75 { return (T*)(rawbuf); }
76
77 operator const T*() const
78 { return (const T*)(rawbuf); }
79
80 ~array_initializer()
81 {
82 char *init_buf = (char*)rawbuf + N*sizeof(T);
83 for(std::size_t i = 0; i != N; ++i){
84 init_buf -= sizeof(T);
85 move_detail::force_ptr<T*>(init_buf)->~T();
86 }
87 }
88
89 private:
90 detail::max_align rawbuf[(N*sizeof(T)-1)/sizeof(detail::max_align)+1];
91};
92
93} //namespace detail{
94} //namespace intrusive{
95} //namespace boost{
96
97#endif //BOOST_INTRUSIVE_DETAIL_ARRAY_INITIALIZER_HPP
98

source code of boost/libs/intrusive/include/boost/intrusive/detail/array_initializer.hpp