]> git.lyx.org Git - lyx.git/blob - boost/boost/optional.hpp
boost::filesystem added
[lyx.git] / boost / boost / optional.hpp
1 // (C) 2003, Fernando Luis Cacciola Carballal.
2 //
3 // This material is provided "as is", with absolutely no warranty expressed
4 // or implied. Any use is at your own risk.
5 //
6 // Permission to use or copy this software for any purpose is hereby granted
7 // without fee, provided the above notices are retained on all copies.
8 // Permission to modify the code and to distribute modified code is granted,
9 // provided the above notices are retained, and a notice that the code was
10 // modified is included with the above copyright notice.
11 //
12 // See http://www.boost.org/lib/optional for documentation.
13 //
14 // You are welcome to contact the author at:
15 //  fernando_cacciola@hotmail.com
16 //
17 #ifndef BOOST_OPTIONAL_FLC_19NOV2002_HPP
18 #define BOOST_OPTIONAL_FLC_19NOV2002_HPP
19
20 #include<new>
21 #include<algorithm>
22
23 #include "boost/config.hpp"
24 #include "boost/assert.hpp"
25 #include "boost/type_traits/alignment_of.hpp"
26 #include "boost/type_traits/type_with_alignment.hpp"
27
28 #if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
29 // VC6.0 has the following bug:
30 //   When a templated assignment operator exist, an implicit conversion
31 //   constructing an optional<T> is used when assigment of the form:
32 //     optional<T> opt ; opt = T(...);
33 //   is compiled.
34 //   However, optional's ctor is _explicit_ and the assignemt shouldn't compile.
35 //   Therefore, for VC6.0 templated assignment is disabled.
36 //
37 #define BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
38 #endif
39
40 #if BOOST_WORKAROUND(BOOST_MSVC, == 1300)
41 // VC7.0 has the following bug:
42 //   When both a non-template and a template copy-ctor exist
43 //   and the templated version is made 'explicit', the explicit is also
44 //   given to the non-templated version, making the class non-implicitely-copyable.
45 //
46 #define BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
47 #endif
48
49 namespace boost
50 {
51   namespace optional_detail
52   {
53     template <class T>
54     class aligned_storage
55     {
56          // Borland ICEs if unnamed unions are used for this!
57          union dummy_u
58          {
59              char data[ sizeof(T) ];
60              BOOST_DEDUCED_TYPENAME type_with_alignment<
61                ::boost::alignment_of<T>::value >::type aligner_;
62          } dummy_ ;
63
64       public:
65
66         void const* address() const { return &dummy_.data[0]; }
67         void      * address()       { return &dummy_.data[0]; }
68     } ;
69   }
70
71 template<class T>
72 class optional
73 {
74     typedef optional<T> this_type ;
75
76     typedef optional_detail::aligned_storage<T> storage_type ;
77
78     typedef void (this_type::*unspecified_bool_type)();
79     
80   public :
81
82     typedef T value_type ;
83
84     // Creates an optional<T> uninitialized.
85     // No-throw
86     optional ()
87       :
88       m_initialized(false) {}
89
90     // Creates an optional<T> initialized with 'val'.
91     // Can throw if T::T(T const&) does
92     explicit optional ( T const& val )
93       :
94       m_initialized(false)
95     {
96       construct(val);
97     }
98
99 #ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
100     // NOTE: MSVC needs templated versions first
101
102     // Creates a deep copy of another convertible optional<U>
103     // Requires a valid conversion from U to T.
104     // Can throw if T::T(U const&) does
105     template<class U>
106     explicit optional ( optional<U> const& rhs )
107       :
108       m_initialized(false)
109     {
110       if ( rhs )
111         construct(*rhs);
112     }
113 #endif
114
115     // Creates a deep copy of another optional<T>
116     // Can throw if T::T(T const&) does
117     optional ( optional const& rhs )
118       :
119       m_initialized(false)
120     {
121       if ( rhs )
122         construct(*rhs);
123     }
124
125     // No-throw (assuming T::~T() doesn't)
126     ~optional() { destroy() ; }
127
128 #ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
129     // Assigns from another convertible optional<U> (converts && deep-copies the rhs value)
130     // Requires a valid conversion from U to T.
131     // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
132     template<class U>
133     optional& operator= ( optional<U> const& rhs )
134       {
135         destroy(); // no-throw
136
137         if ( rhs )
138         {
139           // An exception can be thrown here.
140           // It it happens, THIS will be left uninitialized.
141           construct(*rhs);
142         }
143         return *this ;
144       }
145 #endif
146
147     // Assigns from another optional<T> (deep-copies the rhs value)
148     // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
149     optional& operator= ( optional const& rhs )
150       {
151         destroy(); // no-throw
152
153         if ( rhs )
154         {
155           // An exception can be thrown here.
156           // It it happens, THIS will be left uninitialized.
157           construct(*rhs);
158         }
159         return *this ;
160       }
161
162     // Destroys the current value, if any, leaving this UNINITIALIZED
163     // No-throw (assuming T::~T() doesn't)
164     void reset()
165       {
166         destroy();
167       }
168
169     // Replaces the current value -if any- with 'val'
170     // Basic Guarantee: If T::T( T const& ) throws this is left UNINITIALIZED.
171     void reset ( T const& val )
172       {
173         destroy();
174         construct(val);
175       }
176
177     // Returns a pointer to the value if this is initialized, otherwise,
178     // returns NULL.
179     // No-throw
180     T const* get() const { return m_initialized ? static_cast<T const*>(m_storage.address()) : 0 ; }
181     T*       get()       { return m_initialized ? static_cast<T*>      (m_storage.address()) : 0 ; }
182
183     // Returns a pointer to the value if this is initialized, otherwise,
184     // the behaviour is UNDEFINED
185     // No-throw
186     T const* operator->() const { BOOST_ASSERT(m_initialized) ; return static_cast<T const*>(m_storage.address()) ; }
187     T*       operator->()       { BOOST_ASSERT(m_initialized) ; return static_cast<T*>      (m_storage.address()) ; }
188
189     // Returns a reference to the value if this is initialized, otherwise,
190     // the behaviour is UNDEFINED
191     // No-throw
192     T const& operator *() const { BOOST_ASSERT(m_initialized) ; return *static_cast<T const*>(m_storage.address()) ; }
193     T&       operator *()       { BOOST_ASSERT(m_initialized) ; return *static_cast<T*>      (m_storage.address()) ; }
194
195     // implicit conversion to "bool"
196     // No-throw
197     operator unspecified_bool_type() const { return m_initialized ? &this_type::destroy : 0 ; }
198
199        // This is provided for those compilers which don't like the conversion to bool
200        // on some contexts.
201        bool operator!() const { return !m_initialized ; }
202
203   private :
204
205     void construct ( T const& val )
206      {
207        new (m_storage.address()) T(val) ;
208        m_initialized = true ;
209      }
210
211     void destroy()
212      {
213        if ( m_initialized )
214        {
215          get()->~T() ;
216          m_initialized = false ;
217        }
218      }
219
220     bool m_initialized ;
221     storage_type m_storage ;
222 } ;
223
224 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
225 // No-throw
226 template<class T>
227 inline
228 T const* get_pointer ( optional<T> const& opt )
229 {
230   return opt.get() ;
231 }
232
233 template<class T>
234 inline
235 T* get_pointer ( optional<T>& opt )
236 {
237   return opt.get() ;
238 }
239
240 // template<class OP> bool equal_pointees(OP const& x, OP const& y);
241 //
242 // Being OP a model of OptionalPointee (either a pointer or an optional):
243 //
244 // If both x and y have valid pointees, returns the result of (*x == *y)
245 // If only one has a valid pointee, returns false.
246 // If none have valid pointees, returns true.
247 // No-throw
248 template<class OptionalPointee>
249 inline
250 bool equal_pointees ( OptionalPointee const& x, OptionalPointee const& y )
251 {
252   return (!x) != (!y) ? false : ( !x ? true : (*x) == (*y) ) ;
253 }
254
255 // optional's operator == and != have deep-semantics (compare values).
256 // WARNING: This is UNLIKE pointers. Use equal_pointees() in generic code instead.
257 template<class T>
258 inline
259 bool operator == ( optional<T> const& x, optional<T> const& y )
260 { return equal_pointees(x,y); }
261
262 template<class T>
263 inline
264 bool operator != ( optional<T> const& x, optional<T> const& y )
265 { return !( x == y ) ; }
266
267
268 //
269 // The following swap implementation follows the GCC workaround as found in
270 //  "boost/detail/compressed_pair.hpp"
271 //
272 namespace optional_detail {
273
274 // GCC < 3.2 gets the using declaration at namespace scope (FLC, DWA)
275 #if BOOST_WORKAROUND(__GNUC__, < 3)                             \
276     || BOOST_WORKAROUND(__GNUC__, == 3) && __GNUC_MINOR__ <= 2
277    using std::swap;
278 #define BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
279 #endif
280
281 // optional's swap:
282 // If both are initialized, calls swap(T&, T&), with whatever exception guarantess are given there.
283 // If only one is initialized, calls I.reset() and U.reset(*I), with the Basic Guarantee
284 // If both are uninitialized, do nothing (no-throw)
285 template<class T>
286 inline
287 void optional_swap ( optional<T>& x, optional<T>& y )
288 {
289   if ( !x && !!y )
290   {
291     x.reset(*y); // Basic guarantee.
292     y.reset();
293   }
294   else if ( !!x && !y )
295   {
296     y.reset(*x); // Basic guarantee.
297     x.reset();
298   }
299   else if ( !!x && !!y )
300   {
301 // GCC > 3.2 and all other compilers have the using declaration at function scope (FLC)
302 #ifndef BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
303     // allow for Koenig lookup
304     using std::swap ;
305 #endif
306     swap(*x,*y);
307   }
308 }
309
310 #undef BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
311
312 } // namespace optional_detail
313
314 template<class T> inline void swap ( optional<T>& x, optional<T>& y )
315 {
316   optional_detail::optional_swap(x,y);
317 }
318
319
320 } // namespace boost
321
322 #endif
323