1 // Copyright (C) 2003, Fernando Luis Cacciola Carballal.
3 // Use, modification, and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/lib/optional for documentation.
9 // You are welcome to contact the author at:
10 // fernando_cacciola@hotmail.com
12 #ifndef BOOST_OPTIONAL_FLC_19NOV2002_HPP
13 #define BOOST_OPTIONAL_FLC_19NOV2002_HPP
18 #include "boost/config.hpp"
19 #include "boost/assert.hpp"
20 #include "boost/type.hpp"
21 #include "boost/type_traits/alignment_of.hpp"
22 #include "boost/type_traits/type_with_alignment.hpp"
23 #include "boost/type_traits/remove_reference.hpp"
24 #include "boost/type_traits/is_reference.hpp"
25 #include "boost/mpl/if.hpp"
26 #include "boost/mpl/bool.hpp"
27 #include "boost/mpl/not.hpp"
28 #include "boost/detail/reference_content.hpp"
29 #include "boost/detail/none_t.hpp"
30 #include "boost/utility/compare_pointees.hpp"
32 #if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
33 // VC6.0 has the following bug:
34 // When a templated assignment operator exist, an implicit conversion
35 // constructing an optional<T> is used when assigment of the form:
36 // optional<T> opt ; opt = T(...);
38 // However, optional's ctor is _explicit_ and the assignemt shouldn't compile.
39 // Therefore, for VC6.0 templated assignment is disabled.
41 #define BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
44 #if BOOST_WORKAROUND(BOOST_MSVC, == 1300)
45 // VC7.0 has the following bug:
46 // When both a non-template and a template copy-ctor exist
47 // and the templated version is made 'explicit', the explicit is also
48 // given to the non-templated version, making the class non-implicitely-copyable.
50 #define BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
53 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) || BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION,<=700)
54 // AFAICT only VC7.1 correctly resolves the overload set
55 // that includes the in-place factory taking functions,
56 // so for the other VC versions, in-place factory support
58 #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
61 #if BOOST_WORKAROUND(__BORLANDC__, <= 0x551)
62 // BCB (5.5.1) cannot parse the nested template struct in an inplace factory.
63 #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
66 #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) \
67 && BOOST_WORKAROUND(__BORLANDC__, <= 0x564)
68 // BCB (up to 5.64) has the following bug:
69 // If there is a member function/operator template of the form
70 // template<class Expr> mfunc( Expr expr ) ;
71 // some calls are resolved to this even if there are other better matches.
72 // The effect of this bug is that calls to converting ctors and assignments
73 // are incrorrectly sink to this general catch-all member function template as shown above.
74 #define BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
80 class InPlaceFactoryBase ;
81 class TypedInPlaceFactoryBase ;
83 namespace optional_detail {
85 // This local class is used instead of that in "aligned_storage.hpp"
86 // because I've found the 'official' class to ICE BCB5.5
87 // when some types are used with optional<>
88 // (due to sizeof() passed down as a non-type template parameter)
92 // Borland ICEs if unnamed unions are used for this!
95 char data[ sizeof(T) ];
96 BOOST_DEDUCED_TYPENAME type_with_alignment<
97 ::boost::alignment_of<T>::value >::type aligner_;
102 void const* address() const { return &dummy_.data[0]; }
103 void * address() { return &dummy_.data[0]; }
107 struct types_when_isnt_ref
109 typedef T const& reference_const_type ;
110 typedef T & reference_type ;
111 typedef T const* pointer_const_type ;
112 typedef T * pointer_type ;
113 typedef T const& argument_type ;
116 struct types_when_is_ref
118 typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type raw_type ;
120 typedef raw_type& reference_const_type ;
121 typedef raw_type& reference_type ;
122 typedef raw_type* pointer_const_type ;
123 typedef raw_type* pointer_type ;
124 typedef raw_type& argument_type ;
127 struct optional_tag {} ;
130 class optional_base : public optional_tag
134 typedef BOOST_DEDUCED_TYPENAME detail::make_reference_content<T>::type internal_type ;
136 typedef aligned_storage<internal_type> storage_type ;
138 typedef types_when_isnt_ref<T> types_when_not_ref ;
139 typedef types_when_is_ref<T> types_when_ref ;
141 typedef optional_base<T> this_type ;
145 typedef T value_type ;
147 typedef mpl::true_ is_reference_tag ;
148 typedef mpl::false_ is_not_reference_tag ;
150 typedef BOOST_DEDUCED_TYPENAME is_reference<T>::type is_reference_predicate ;
152 typedef BOOST_DEDUCED_TYPENAME mpl::if_<is_reference_predicate,types_when_ref,types_when_not_ref>::type types ;
154 typedef bool (this_type::*unspecified_bool_type)() const;
156 typedef BOOST_DEDUCED_TYPENAME types::reference_type reference_type ;
157 typedef BOOST_DEDUCED_TYPENAME types::reference_const_type reference_const_type ;
158 typedef BOOST_DEDUCED_TYPENAME types::pointer_type pointer_type ;
159 typedef BOOST_DEDUCED_TYPENAME types::pointer_const_type pointer_const_type ;
160 typedef BOOST_DEDUCED_TYPENAME types::argument_type argument_type ;
162 // Creates an optional<T> uninitialized.
166 m_initialized(false) {}
168 // Creates an optional<T> uninitialized.
170 optional_base ( detail::none_t const& )
172 m_initialized(false) {}
174 // Creates an optional<T> initialized with 'val'.
175 // Can throw if T::T(T const&) does
176 optional_base ( argument_type val )
183 // Creates a deep copy of another optional<T>
184 // Can throw if T::T(T const&) does
185 optional_base ( optional_base const& rhs )
189 if ( rhs.is_initialized() )
190 construct(rhs.get_impl());
193 // This is used for both converting and in-place constructions.
194 // Derived classes use the 'tag' to select the appropriate
195 // implementation (the correct 'construct()' overload)
197 explicit optional_base ( Expr const& expr, Expr const* tag )
204 // No-throw (assuming T::~T() doesn't)
205 ~optional_base() { destroy() ; }
207 // Assigns from another optional<T> (deep-copies the rhs value)
208 // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
209 void assign ( optional_base const& rhs )
212 if ( rhs.is_initialized() )
213 construct(rhs.get_impl());
216 // Assigns from a T (deep-copies the rhs value)
217 // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
218 void assign ( argument_type val )
224 // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
225 // No-throw (assuming T::~T() doesn't)
226 void assign ( detail::none_t const& ) { destroy(); }
228 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
230 void assign_expr ( Expr const& expr, Expr const* tag )
239 // Destroys the current value, if any, leaving this UNINITIALIZED
240 // No-throw (assuming T::~T() doesn't)
241 void reset() { destroy(); }
243 // Replaces the current value -if any- with 'val'
244 // Basic Guarantee: If T::T( T const& ) throws this is left UNINITIALIZED.
245 void reset ( argument_type val ) { assign(val); }
247 // Returns a pointer to the value if this is initialized, otherwise,
250 pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
251 pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
253 bool is_initialized() const { return m_initialized ; }
257 void construct ( argument_type val )
259 new (m_storage.address()) internal_type(val) ;
260 m_initialized = true ;
263 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
264 // Constructs in-place using the given factory
266 void construct ( Expr const& factory, InPlaceFactoryBase const* )
268 BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
269 factory.BOOST_NESTED_TEMPLATE apply<value_type>(m_storage.address()) ;
270 m_initialized = true ;
273 // Constructs in-place using the given typed factory
275 void construct ( Expr const& factory, TypedInPlaceFactoryBase const* )
277 BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
278 factory.apply(m_storage.address()) ;
279 m_initialized = true ;
283 // Constructs using any expression implicitely convertible to the single argument
284 // of a one-argument T constructor.
285 // Converting constructions of optional<T> from optional<U> uses this function with
286 // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
288 void construct ( Expr const& expr, void const* )
290 new (m_storage.address()) internal_type(expr) ;
291 m_initialized = true ;
294 #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
295 // BCB5.64 (and probably lower versions) workaround.
296 // The in-place factories are supported by means of catch-all constructors
297 // and assignment operators (the functions are parameterized in terms of
298 // an arbitrary 'Expr' type)
299 // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
300 // to the 'Expr'-taking functions even though explicit overloads are present for them.
301 // Thus, the following overload is needed to properly handle the case when the 'lhs'
302 // is another optional.
304 // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
305 // instead of choosing the wrong overload
307 // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
309 void construct ( Expr const& expr, optional_tag const* )
311 if ( expr.is_initialized() )
313 // An exception can be thrown here.
314 // It it happens, THIS will be left uninitialized.
315 new (m_storage.address()) internal_type(expr.get()) ;
316 m_initialized = true ;
324 destroy_impl(is_reference_predicate()) ;
327 unspecified_bool_type safe_bool() const { return m_initialized ? &this_type::is_initialized : 0 ; }
329 reference_const_type get_impl() const { return dereference(get_object(), is_reference_predicate() ) ; }
330 reference_type get_impl() { return dereference(get_object(), is_reference_predicate() ) ; }
332 pointer_const_type get_ptr_impl() const { return cast_ptr(get_object(), is_reference_predicate() ) ; }
333 pointer_type get_ptr_impl() { return cast_ptr(get_object(), is_reference_predicate() ) ; }
337 // internal_type can be either T or reference_content<T>
338 internal_type const* get_object() const { return static_cast<internal_type const*>(m_storage.address()); }
339 internal_type * get_object() { return static_cast<internal_type *> (m_storage.address()); }
341 // reference_content<T> lacks an implicit conversion to T&, so the following is needed to obtain a proper reference.
342 reference_const_type dereference( internal_type const* p, is_not_reference_tag ) const { return *p ; }
343 reference_type dereference( internal_type* p, is_not_reference_tag ) { return *p ; }
344 reference_const_type dereference( internal_type const* p, is_reference_tag ) const { return p->get() ; }
345 reference_type dereference( internal_type* p, is_reference_tag ) { return p->get() ; }
347 void destroy_impl ( is_not_reference_tag ) { get_impl().~T() ; m_initialized = false ; }
348 void destroy_impl ( is_reference_tag ) { m_initialized = false ; }
350 // If T is of reference type, trying to get a pointer to the held value must result in a compile-time error.
351 // Decent compilers should disallow conversions from reference_content<T>* to T*, but just in case,
352 // the following olverloads are used to filter out the case and guarantee an error in case of T being a reference.
353 pointer_const_type cast_ptr( internal_type const* p, is_not_reference_tag ) const { return p ; }
354 pointer_type cast_ptr( internal_type * p, is_not_reference_tag ) { return p ; }
357 storage_type m_storage ;
360 } // namespace optional_detail
363 class optional : public optional_detail::optional_base<T>
365 typedef optional_detail::optional_base<T> base ;
367 typedef BOOST_DEDUCED_TYPENAME base::unspecified_bool_type unspecified_bool_type ;
371 typedef optional<T> this_type ;
373 typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ;
374 typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ;
375 typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
376 typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ;
377 typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ;
378 typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ;
380 // Creates an optional<T> uninitialized.
382 optional() : base() {}
384 // Creates an optional<T> uninitialized.
386 optional( detail::none_t const& none_ ) : base(none_) {}
388 // Creates an optional<T> initialized with 'val'.
389 // Can throw if T::T(T const&) does
390 optional ( argument_type val ) : base(val) {}
392 #ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
393 // NOTE: MSVC needs templated versions first
395 // Creates a deep copy of another convertible optional<U>
396 // Requires a valid conversion from U to T.
397 // Can throw if T::T(U const&) does
399 explicit optional ( optional<U> const& rhs )
403 if ( rhs.is_initialized() )
404 this->construct(rhs.get());
408 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
409 // Creates an optional<T> with an expression which can be either
410 // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
411 // (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
412 // (c) Any expression implicitely convertible to the single type
413 // of a one-argument T's constructor.
414 // (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
415 // even though explicit overloads are present for these.
416 // Depending on the above some T ctor is called.
417 // Can throw is the resolved T ctor throws.
419 explicit optional ( Expr const& expr ) : base(expr,&expr) {}
422 // Creates a deep copy of another optional<T>
423 // Can throw if T::T(T const&) does
424 optional ( optional const& rhs ) : base(rhs) {}
426 // No-throw (assuming T::~T() doesn't)
429 #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
430 // Assigns from an expression. See corresponding constructor.
431 // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
433 optional& operator= ( Expr expr )
435 this->assign_expr(expr,&expr);
440 #ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
441 // Assigns from another convertible optional<U> (converts && deep-copies the rhs value)
442 // Requires a valid conversion from U to T.
443 // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
445 optional& operator= ( optional<U> const& rhs )
447 this->destroy(); // no-throw
449 if ( rhs.is_initialized() )
451 // An exception can be thrown here.
452 // It it happens, THIS will be left uninitialized.
453 this->assign(rhs.get());
459 // Assigns from another optional<T> (deep-copies the rhs value)
460 // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
461 // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
462 optional& operator= ( optional const& rhs )
464 this->assign( rhs ) ;
468 // Assigns from a T (deep-copies the rhs value)
469 // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
470 optional& operator= ( argument_type val )
472 this->assign( val ) ;
476 // Assigns from a "none"
477 // Which destroys the current value, if any, leaving this UNINITIALIZED
478 // No-throw (assuming T::~T() doesn't)
479 optional& operator= ( detail::none_t const& none_ )
481 this->assign( none_ ) ;
485 // Returns a reference to the value if this is initialized, otherwise,
486 // the behaviour is UNDEFINED
488 reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
489 reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
491 // Returns a pointer to the value if this is initialized, otherwise,
492 // the behaviour is UNDEFINED
494 pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
495 pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
497 // Returns a reference to the value if this is initialized, otherwise,
498 // the behaviour is UNDEFINED
500 reference_const_type operator *() const { return this->get() ; }
501 reference_type operator *() { return this->get() ; }
503 // implicit conversion to "bool"
505 operator unspecified_bool_type() const { return this->safe_bool() ; }
507 // This is provided for those compilers which don't like the conversion to bool
509 bool operator!() const { return !this->is_initialized() ; }
512 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
516 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
517 get ( optional<T> const& opt )
524 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
525 get ( optional<T>& opt )
530 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
534 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
535 get ( optional<T> const* opt )
537 return opt->get_ptr() ;
542 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
543 get ( optional<T>* opt )
545 return opt->get_ptr() ;
548 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
552 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
553 get_pointer ( optional<T> const& opt )
555 return opt.get_ptr() ;
560 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
561 get_pointer ( optional<T>& opt )
563 return opt.get_ptr() ;
566 // optional's relational operators ( ==, !=, <, >, <=, >= ) have deep-semantics (compare values).
567 // WARNING: This is UNLIKE pointers. Use equal_pointees()/less_pointess() in generic code instead.
571 bool operator == ( optional<T> const& x, optional<T> const& y )
572 { return equal_pointees(x,y); }
576 bool operator < ( optional<T> const& x, optional<T> const& y )
577 { return less_pointees(x,y); }
581 bool operator != ( optional<T> const& x, optional<T> const& y )
582 { return !( x == y ) ; }
586 bool operator > ( optional<T> const& x, optional<T> const& y )
591 bool operator <= ( optional<T> const& x, optional<T> const& y )
592 { return !( y < x ) ; }
596 bool operator >= ( optional<T> const& x, optional<T> const& y )
597 { return !( x < y ) ; }
601 bool operator == ( optional<T> const& x, detail::none_t const& )
602 { return equal_pointees(x, optional<T>() ); }
606 bool operator < ( optional<T> const& x, detail::none_t const& )
607 { return less_pointees(x,optional<T>() ); }
611 bool operator != ( optional<T> const& x, detail::none_t const& y )
612 { return !( x == y ) ; }
616 bool operator > ( optional<T> const& x, detail::none_t const& y )
621 bool operator <= ( optional<T> const& x, detail::none_t const& y )
622 { return !( y < x ) ; }
626 bool operator >= ( optional<T> const& x, detail::none_t const& y )
627 { return !( x < y ) ; }
631 bool operator == ( detail::none_t const& x, optional<T> const& y )
632 { return equal_pointees(optional<T>() ,y); }
636 bool operator < ( detail::none_t const& x, optional<T> const& y )
637 { return less_pointees(optional<T>() ,y); }
641 bool operator != ( detail::none_t const& x, optional<T> const& y )
642 { return !( x == y ) ; }
646 bool operator > ( detail::none_t const& x, optional<T> const& y )
651 bool operator <= ( detail::none_t const& x, optional<T> const& y )
652 { return !( y < x ) ; }
656 bool operator >= ( detail::none_t const& x, optional<T> const& y )
657 { return !( x < y ) ; }
660 // The following swap implementation follows the GCC workaround as found in
661 // "boost/detail/compressed_pair.hpp"
663 namespace optional_detail {
665 // GCC < 3.2 gets the using declaration at namespace scope (FLC, DWA)
666 #if BOOST_WORKAROUND(__GNUC__, < 3) \
667 || BOOST_WORKAROUND(__GNUC__, == 3) && __GNUC_MINOR__ <= 2
669 #define BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
673 // If both are initialized, calls swap(T&, T&), with whatever exception guarantess are given there.
674 // If only one is initialized, calls I.reset() and U.reset(*I), with the Basic Guarantee
675 // If both are uninitialized, do nothing (no-throw)
678 void optional_swap ( optional<T>& x, optional<T>& y )
682 x.reset(*y); // Basic guarantee.
685 else if ( !!x && !y )
687 y.reset(*x); // Basic guarantee.
690 else if ( !!x && !!y )
692 // GCC > 3.2 and all other compilers have the using declaration at function scope (FLC)
693 #ifndef BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
694 // allow for Koenig lookup
701 } // namespace optional_detail
703 template<class T> inline void swap ( optional<T>& x, optional<T>& y )
705 optional_detail::optional_swap(x,y);