blob: 8e34ae2a15f850833da42079314e89c88f93b160 [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001// (C) Copyright 2002-2008, Fernando Luis Cacciola Carballal.
2//
3// Distributed under the Boost Software License, Version 1.0. (See
4// accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6//
7// 21 Ago 2002 (Created) Fernando Cacciola
8// 24 Dec 2007 (Refactored and worked around various compiler bugs) Fernando Cacciola, Niels Dekker
9// 23 May 2008 (Fixed operator= const issue, added initialized_value) Niels Dekker, Fernando Cacciola
10// 21 Ago 2008 (Added swap) Niels Dekker, Fernando Cacciola
11// 20 Feb 2009 (Fixed logical const-ness issues) Niels Dekker, Fernando Cacciola
12// 03 Apr 2010 (Added initialized<T>, suggested by Jeffrey Hellrung, fixing #3472) Niels Dekker
13// 30 May 2010 (Made memset call conditional, fixing #3869) Niels Dekker
14//
Jeff Thompson3d613fd2013-10-15 15:39:04 -070015#ifndef NDNBOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
16#define NDNBOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070017
18// Note: The implementation of ndnboost::value_initialized had to deal with the
19// fact that various compilers haven't fully implemented value-initialization.
20// The constructor of ndnboost::value_initialized<T> works around these compiler
21// issues, by clearing the bytes of T, before constructing the T object it
22// contains. More details on these issues are at libs/utility/value_init.htm
23
24#include <ndnboost/aligned_storage.hpp>
Jeff Thompson3d613fd2013-10-15 15:39:04 -070025#include <ndnboost/config.hpp> // For NDNBOOST_NO_COMPLETE_VALUE_INITIALIZATION.
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070026#include <ndnboost/detail/workaround.hpp>
27#include <ndnboost/static_assert.hpp>
28#include <ndnboost/type_traits/cv_traits.hpp>
29#include <ndnboost/type_traits/alignment_of.hpp>
30#include <ndnboost/swap.hpp>
31#include <cstring>
32#include <new>
33
Jeff Thompson3d613fd2013-10-15 15:39:04 -070034#ifdef NDNBOOST_MSVC
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070035#pragma warning(push)
36#if _MSC_VER >= 1310
37// It is safe to ignore the following warning from MSVC 7.1 or higher:
38// "warning C4351: new behavior: elements of array will be default initialized"
39#pragma warning(disable: 4351)
40// It is safe to ignore the following MSVC warning, which may pop up when T is
41// a const type: "warning C4512: assignment operator could not be generated".
42#pragma warning(disable: 4512)
43#endif
44#endif
45
Jeff Thompson3d613fd2013-10-15 15:39:04 -070046#ifdef NDNBOOST_NO_COMPLETE_VALUE_INITIALIZATION
47 // Implementation detail: The macro NDNBOOST_DETAIL_VALUE_INIT_WORKAROUND_SUGGESTED
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070048 // suggests that a workaround should be applied, because of compiler issues
49 // regarding value-initialization.
Jeff Thompson3d613fd2013-10-15 15:39:04 -070050 #define NDNBOOST_DETAIL_VALUE_INIT_WORKAROUND_SUGGESTED
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070051#endif
52
Jeff Thompson3d613fd2013-10-15 15:39:04 -070053// Implementation detail: The macro NDNBOOST_DETAIL_VALUE_INIT_WORKAROUND
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070054// switches the value-initialization workaround either on or off.
Jeff Thompson3d613fd2013-10-15 15:39:04 -070055#ifndef NDNBOOST_DETAIL_VALUE_INIT_WORKAROUND
56 #ifdef NDNBOOST_DETAIL_VALUE_INIT_WORKAROUND_SUGGESTED
57 #define NDNBOOST_DETAIL_VALUE_INIT_WORKAROUND 1
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070058 #else
Jeff Thompson3d613fd2013-10-15 15:39:04 -070059 #define NDNBOOST_DETAIL_VALUE_INIT_WORKAROUND 0
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070060 #endif
61#endif
62
63namespace ndnboost {
64
65template<class T>
66class initialized
67{
68 private :
69 struct wrapper
70 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070071#if !NDNBOOST_WORKAROUND(__BORLANDC__, NDNBOOST_TESTED_AT(0x592))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070072 typename
73#endif
74 remove_const<T>::type data;
75
76 wrapper()
77 :
78 data()
79 {
80 }
81
82 wrapper(T const & arg)
83 :
84 data(arg)
85 {
86 }
87 };
88
89 mutable
Jeff Thompson3d613fd2013-10-15 15:39:04 -070090#if !NDNBOOST_WORKAROUND(__BORLANDC__, NDNBOOST_TESTED_AT(0x592))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070091 typename
92#endif
93 aligned_storage<sizeof(wrapper), alignment_of<wrapper>::value>::type x;
94
95 wrapper * wrapper_address() const
96 {
97 return static_cast<wrapper *>( static_cast<void*>(&x));
98 }
99
100 public :
101
102 initialized()
103 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700104#if NDNBOOST_DETAIL_VALUE_INIT_WORKAROUND
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700105 std::memset(&x, 0, sizeof(x));
106#endif
107 new (wrapper_address()) wrapper();
108 }
109
110 initialized(initialized const & arg)
111 {
112 new (wrapper_address()) wrapper( static_cast<wrapper const &>(*(arg.wrapper_address())));
113 }
114
115 explicit initialized(T const & arg)
116 {
117 new (wrapper_address()) wrapper(arg);
118 }
119
120 initialized & operator=(initialized const & arg)
121 {
122 // Assignment is only allowed when T is non-const.
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700123 NDNBOOST_STATIC_ASSERT( ! is_const<T>::value );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700124 *wrapper_address() = static_cast<wrapper const &>(*(arg.wrapper_address()));
125 return *this;
126 }
127
128 ~initialized()
129 {
130 wrapper_address()->wrapper::~wrapper();
131 }
132
133 T const & data() const
134 {
135 return wrapper_address()->data;
136 }
137
138 T& data()
139 {
140 return wrapper_address()->data;
141 }
142
143 void swap(initialized & arg)
144 {
145 ::ndnboost::swap( this->data(), arg.data() );
146 }
147
148 operator T const &() const
149 {
150 return wrapper_address()->data;
151 }
152
153 operator T&()
154 {
155 return wrapper_address()->data;
156 }
157
158} ;
159
160template<class T>
161T const& get ( initialized<T> const& x )
162{
163 return x.data() ;
164}
165
166template<class T>
167T& get ( initialized<T>& x )
168{
169 return x.data() ;
170}
171
172template<class T>
173void swap ( initialized<T> & lhs, initialized<T> & rhs )
174{
175 lhs.swap(rhs) ;
176}
177
178template<class T>
179class value_initialized
180{
181 private :
182
183 // initialized<T> does value-initialization by default.
184 initialized<T> m_data;
185
186 public :
187
188 value_initialized()
189 :
190 m_data()
191 { }
192
193 T const & data() const
194 {
195 return m_data.data();
196 }
197
198 T& data()
199 {
200 return m_data.data();
201 }
202
203 void swap(value_initialized & arg)
204 {
205 m_data.swap(arg.m_data);
206 }
207
208 operator T const &() const
209 {
210 return m_data;
211 }
212
213 operator T&()
214 {
215 return m_data;
216 }
217} ;
218
219
220template<class T>
221T const& get ( value_initialized<T> const& x )
222{
223 return x.data() ;
224}
225
226template<class T>
227T& get ( value_initialized<T>& x )
228{
229 return x.data() ;
230}
231
232template<class T>
233void swap ( value_initialized<T> & lhs, value_initialized<T> & rhs )
234{
235 lhs.swap(rhs) ;
236}
237
238
239class initialized_value_t
240{
241 public :
242
243 template <class T> operator T() const
244 {
245 return initialized<T>().data();
246 }
247};
248
249initialized_value_t const initialized_value = {} ;
250
251
252} // namespace ndnboost
253
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700254#ifdef NDNBOOST_MSVC
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700255#pragma warning(pop)
256#endif
257
258#endif