]> git.donarmstrong.com Git - rsem.git/blob - boost/optional/optional.hpp
88041d1441ddf0a5d6e3b0a8779a2b4e77ab099a
[rsem.git] / boost / optional / optional.hpp
1 // Copyright (C) 2003, Fernando Luis Cacciola Carballal.
2 //
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)
6 //
7 // See http://www.boost.org/libs/optional for documentation.
8 //
9 // You are welcome to contact the author at:
10 //  fernando_cacciola@hotmail.com
11 //
12 #ifndef BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
13 #define BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
14
15 #include<new>
16 #include<algorithm>
17
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/none.hpp"
30 #include "boost/utility/compare_pointees.hpp"
31
32 #include "boost/optional/optional_fwd.hpp"
33
34 #if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
35 // VC6.0 has the following bug:
36 //   When a templated assignment operator exist, an implicit conversion
37 //   constructing an optional<T> is used when assigment of the form:
38 //     optional<T> opt ; opt = T(...);
39 //   is compiled.
40 //   However, optional's ctor is _explicit_ and the assignemt shouldn't compile.
41 //   Therefore, for VC6.0 templated assignment is disabled.
42 //
43 #define BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
44 #endif
45
46 #if BOOST_WORKAROUND(BOOST_MSVC, == 1300)
47 // VC7.0 has the following bug:
48 //   When both a non-template and a template copy-ctor exist
49 //   and the templated version is made 'explicit', the explicit is also
50 //   given to the non-templated version, making the class non-implicitely-copyable.
51 //
52 #define BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
53 #endif
54
55 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) || BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION,<=700)
56 // AFAICT only VC7.1 correctly resolves the overload set
57 // that includes the in-place factory taking functions,
58 // so for the other VC versions, in-place factory support
59 // is disabled
60 #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
61 #endif
62
63 #if BOOST_WORKAROUND(__BORLANDC__, <= 0x551)
64 // BCB (5.5.1) cannot parse the nested template struct in an inplace factory.
65 #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
66 #endif
67
68 #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) \
69     && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581) )
70 // BCB (up to 5.64) has the following bug:
71 //   If there is a member function/operator template of the form
72 //     template<class Expr> mfunc( Expr expr ) ;
73 //   some calls are resolved to this even if there are other better matches.
74 //   The effect of this bug is that calls to converting ctors and assignments
75 //   are incrorrectly sink to this general catch-all member function template as shown above.
76 #define BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
77 #endif
78
79 // Daniel Wallin discovered that bind/apply.hpp badly interacts with the apply<>
80 // member template of a factory as used in the optional<> implementation.
81 // He proposed this simple fix which is to move the call to apply<> outside
82 // namespace boost.
83 namespace boost_optional_detail
84 {
85   template <class T, class Factory>
86   void construct(Factory const& factory, void* address)
87   {
88     factory.BOOST_NESTED_TEMPLATE apply<T>(address);
89   }
90 }
91
92
93 namespace boost {
94
95 class in_place_factory_base ;
96 class typed_in_place_factory_base ;
97
98 namespace optional_detail {
99
100 // This local class is used instead of that in "aligned_storage.hpp"
101 // because I've found the 'official' class to ICE BCB5.5
102 // when some types are used with optional<>
103 // (due to sizeof() passed down as a non-type template parameter)
104 template <class T>
105 class aligned_storage
106 {
107     // Borland ICEs if unnamed unions are used for this!
108     union dummy_u
109     {
110         char data[ sizeof(T) ];
111         BOOST_DEDUCED_TYPENAME type_with_alignment<
112           ::boost::alignment_of<T>::value >::type aligner_;
113     } dummy_ ;
114
115   public:
116
117     void const* address() const { return &dummy_.data[0]; }
118     void      * address()       { return &dummy_.data[0]; }
119 } ;
120
121 template<class T>
122 struct types_when_isnt_ref
123 {
124   typedef T const& reference_const_type ;
125   typedef T &      reference_type ;
126   typedef T const* pointer_const_type ;
127   typedef T *      pointer_type ;
128   typedef T const& argument_type ;
129 } ;
130 template<class T>
131 struct types_when_is_ref
132 {
133   typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type raw_type ;
134
135   typedef raw_type& reference_const_type ;
136   typedef raw_type& reference_type ;
137   typedef raw_type* pointer_const_type ;
138   typedef raw_type* pointer_type ;
139   typedef raw_type& argument_type ;
140 } ;
141
142 struct optional_tag {} ;
143
144 template<class T>
145 class optional_base : public optional_tag
146 {
147   private :
148
149     typedef
150 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
151     BOOST_DEDUCED_TYPENAME
152 #endif 
153     ::boost::detail::make_reference_content<T>::type internal_type ;
154
155     typedef aligned_storage<internal_type> storage_type ;
156
157     typedef types_when_isnt_ref<T> types_when_not_ref ;
158     typedef types_when_is_ref<T>   types_when_ref   ;
159
160     typedef optional_base<T> this_type ;
161
162   protected :
163
164     typedef T value_type ;
165
166     typedef mpl::true_  is_reference_tag ;
167     typedef mpl::false_ is_not_reference_tag ;
168
169     typedef BOOST_DEDUCED_TYPENAME is_reference<T>::type is_reference_predicate ;
170
171   public:
172     typedef BOOST_DEDUCED_TYPENAME mpl::if_<is_reference_predicate,types_when_ref,types_when_not_ref>::type types ;
173
174   protected:
175     typedef bool (this_type::*unspecified_bool_type)() const;
176
177     typedef BOOST_DEDUCED_TYPENAME types::reference_type       reference_type ;
178     typedef BOOST_DEDUCED_TYPENAME types::reference_const_type reference_const_type ;
179     typedef BOOST_DEDUCED_TYPENAME types::pointer_type         pointer_type ;
180     typedef BOOST_DEDUCED_TYPENAME types::pointer_const_type   pointer_const_type ;
181     typedef BOOST_DEDUCED_TYPENAME types::argument_type        argument_type ;
182
183     // Creates an optional<T> uninitialized.
184     // No-throw
185     optional_base()
186       :
187       m_initialized(false) {}
188
189     // Creates an optional<T> uninitialized.
190     // No-throw
191     optional_base ( none_t )
192       :
193       m_initialized(false) {}
194
195     // Creates an optional<T> initialized with 'val'.
196     // Can throw if T::T(T const&) does
197     optional_base ( argument_type val )
198       :
199       m_initialized(false)
200     {
201       construct(val);
202     }
203     
204     // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialzed optional<T>.
205     // Can throw if T::T(T const&) does
206     optional_base ( bool cond, argument_type val )
207       :
208       m_initialized(false)
209     {
210       if ( cond )
211         construct(val);
212     }
213
214     // Creates a deep copy of another optional<T>
215     // Can throw if T::T(T const&) does
216     optional_base ( optional_base const& rhs )
217       :
218       m_initialized(false)
219     {
220       if ( rhs.is_initialized() )
221         construct(rhs.get_impl());
222     }
223
224
225     // This is used for both converting and in-place constructions.
226     // Derived classes use the 'tag' to select the appropriate
227     // implementation (the correct 'construct()' overload)
228     template<class Expr>
229     explicit optional_base ( Expr const& expr, Expr const* tag )
230       :
231       m_initialized(false)
232     {
233       construct(expr,tag);
234     }
235
236
237
238     // No-throw (assuming T::~T() doesn't)
239     ~optional_base() { destroy() ; }
240
241     // Assigns from another optional<T> (deep-copies the rhs value)
242     void assign ( optional_base const& rhs )
243     {
244       if (is_initialized())
245       {
246         if ( rhs.is_initialized() )
247              assign_value(rhs.get_impl(), is_reference_predicate() );
248         else destroy();
249       }
250       else
251       {
252         if ( rhs.is_initialized() )
253           construct(rhs.get_impl());
254       }
255     }
256
257     // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
258     template<class U>
259     void assign ( optional<U> const& rhs )
260     {
261       if (is_initialized())
262       {
263         if ( rhs.is_initialized() )
264              assign_value(static_cast<value_type>(rhs.get()), is_reference_predicate() );
265         else destroy();
266       }
267       else
268       {
269         if ( rhs.is_initialized() )
270           construct(static_cast<value_type>(rhs.get()));
271       }
272     }
273
274     // Assigns from a T (deep-copies the rhs value)
275     void assign ( argument_type val )
276     {
277       if (is_initialized())
278            assign_value(val, is_reference_predicate() );
279       else construct(val);
280     }
281
282     // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
283     // No-throw (assuming T::~T() doesn't)
284     void assign ( none_t ) { destroy(); }
285
286 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
287     template<class Expr>
288     void assign_expr ( Expr const& expr, Expr const* tag )
289       {
290         if (is_initialized())
291              assign_expr_to_initialized(expr,tag);
292         else construct(expr,tag);
293       }
294 #endif
295
296   public :
297
298     // Destroys the current value, if any, leaving this UNINITIALIZED
299     // No-throw (assuming T::~T() doesn't)
300     void reset() { destroy(); }
301
302     // Replaces the current value -if any- with 'val'
303     void reset ( argument_type val ) { assign(val); }
304
305     // Returns a pointer to the value if this is initialized, otherwise,
306     // returns NULL.
307     // No-throw
308     pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
309     pointer_type       get_ptr()       { return m_initialized ? get_ptr_impl() : 0 ; }
310
311     bool is_initialized() const { return m_initialized ; }
312
313   protected :
314
315     void construct ( argument_type val )
316      {
317        new (m_storage.address()) internal_type(val) ;
318        m_initialized = true ;
319      }
320
321 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
322     // Constructs in-place using the given factory
323     template<class Expr>
324     void construct ( Expr const& factory, in_place_factory_base const* )
325      {
326        BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
327        boost_optional_detail::construct<value_type>(factory, m_storage.address());
328        m_initialized = true ;
329      }
330
331     // Constructs in-place using the given typed factory
332     template<class Expr>
333     void construct ( Expr const& factory, typed_in_place_factory_base const* )
334      {
335        BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
336        factory.apply(m_storage.address()) ;
337        m_initialized = true ;
338      }
339
340     template<class Expr>
341     void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
342      {
343        destroy();
344        construct(factory,tag);
345      }
346
347     // Constructs in-place using the given typed factory
348     template<class Expr>
349     void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
350      {
351        destroy();
352        construct(factory,tag);
353      }
354 #endif
355
356     // Constructs using any expression implicitely convertible to the single argument
357     // of a one-argument T constructor.
358     // Converting constructions of optional<T> from optional<U> uses this function with
359     // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
360     template<class Expr>
361     void construct ( Expr const& expr, void const* )
362      {
363        new (m_storage.address()) internal_type(expr) ;
364        m_initialized = true ;
365      }
366
367     // Assigns using a form any expression implicitely convertible to the single argument
368     // of a T's assignment operator.
369     // Converting assignments of optional<T> from optional<U> uses this function with
370     // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
371     template<class Expr>
372     void assign_expr_to_initialized ( Expr const& expr, void const* )
373      {
374        assign_value(expr, is_reference_predicate());
375      }
376
377 #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
378     // BCB5.64 (and probably lower versions) workaround.
379     //   The in-place factories are supported by means of catch-all constructors
380     //   and assignment operators (the functions are parameterized in terms of
381     //   an arbitrary 'Expr' type)
382     //   This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
383     //   to the 'Expr'-taking functions even though explicit overloads are present for them.
384     //   Thus, the following overload is needed to properly handle the case when the 'lhs'
385     //   is another optional.
386     //
387     // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
388     // instead of choosing the wrong overload
389     //
390     // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
391     template<class Expr>
392     void construct ( Expr const& expr, optional_tag const* )
393      {
394        if ( expr.is_initialized() )
395        {
396          // An exception can be thrown here.
397          // It it happens, THIS will be left uninitialized.
398          new (m_storage.address()) internal_type(expr.get()) ;
399          m_initialized = true ;
400        }
401      }
402 #endif
403
404     void assign_value ( argument_type val, is_not_reference_tag ) { get_impl() = val; }
405     void assign_value ( argument_type val, is_reference_tag     ) { construct(val); }
406
407     void destroy()
408     {
409       if ( m_initialized )
410         destroy_impl(is_reference_predicate()) ;
411     }
412
413     unspecified_bool_type safe_bool() const { return m_initialized ? &this_type::is_initialized : 0 ; }
414
415     reference_const_type get_impl() const { return dereference(get_object(), is_reference_predicate() ) ; }
416     reference_type       get_impl()       { return dereference(get_object(), is_reference_predicate() ) ; }
417
418     pointer_const_type get_ptr_impl() const { return cast_ptr(get_object(), is_reference_predicate() ) ; }
419     pointer_type       get_ptr_impl()       { return cast_ptr(get_object(), is_reference_predicate() ) ; }
420
421   private :
422
423     // internal_type can be either T or reference_content<T>
424     internal_type const* get_object() const { return static_cast<internal_type const*>(m_storage.address()); }
425     internal_type *      get_object()       { return static_cast<internal_type *>     (m_storage.address()); }
426
427     // reference_content<T> lacks an implicit conversion to T&, so the following is needed to obtain a proper reference.
428     reference_const_type dereference( internal_type const* p, is_not_reference_tag ) const { return *p ; }
429     reference_type       dereference( internal_type*       p, is_not_reference_tag )       { return *p ; }
430     reference_const_type dereference( internal_type const* p, is_reference_tag     ) const { return p->get() ; }
431     reference_type       dereference( internal_type*       p, is_reference_tag     )       { return p->get() ; }
432
433 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
434     void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->internal_type::~internal_type() ; m_initialized = false ; }
435 #else
436     void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->T::~T() ; m_initialized = false ; }
437 #endif
438
439     void destroy_impl ( is_reference_tag     ) { m_initialized = false ; }
440
441     // If T is of reference type, trying to get a pointer to the held value must result in a compile-time error.
442     // Decent compilers should disallow conversions from reference_content<T>* to T*, but just in case,
443     // the following olverloads are used to filter out the case and guarantee an error in case of T being a reference.
444     pointer_const_type cast_ptr( internal_type const* p, is_not_reference_tag ) const { return p ; }
445     pointer_type       cast_ptr( internal_type *      p, is_not_reference_tag )       { return p ; }
446     pointer_const_type cast_ptr( internal_type const* p, is_reference_tag     ) const { return &p->get() ; }
447     pointer_type       cast_ptr( internal_type *      p, is_reference_tag     )       { return &p->get() ; }
448
449     bool m_initialized ;
450     storage_type m_storage ;
451 } ;
452
453 } // namespace optional_detail
454
455 template<class T>
456 class optional : public optional_detail::optional_base<T>
457 {
458     typedef optional_detail::optional_base<T> base ;
459
460     typedef BOOST_DEDUCED_TYPENAME base::unspecified_bool_type  unspecified_bool_type ;
461
462   public :
463
464     typedef optional<T> this_type ;
465
466     typedef BOOST_DEDUCED_TYPENAME base::value_type           value_type ;
467     typedef BOOST_DEDUCED_TYPENAME base::reference_type       reference_type ;
468     typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
469     typedef BOOST_DEDUCED_TYPENAME base::pointer_type         pointer_type ;
470     typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type   pointer_const_type ;
471     typedef BOOST_DEDUCED_TYPENAME base::argument_type        argument_type ;
472
473     // Creates an optional<T> uninitialized.
474     // No-throw
475     optional() : base() {}
476
477     // Creates an optional<T> uninitialized.
478     // No-throw
479     optional( none_t none_ ) : base(none_) {}
480
481     // Creates an optional<T> initialized with 'val'.
482     // Can throw if T::T(T const&) does
483     optional ( argument_type val ) : base(val) {}
484
485     // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
486     // Can throw if T::T(T const&) does
487     optional ( bool cond, argument_type val ) : base(cond,val) {}
488
489 #ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
490     // NOTE: MSVC needs templated versions first
491
492     // Creates a deep copy of another convertible optional<U>
493     // Requires a valid conversion from U to T.
494     // Can throw if T::T(U const&) does
495     template<class U>
496     explicit optional ( optional<U> const& rhs )
497       :
498       base()
499     {
500       if ( rhs.is_initialized() )
501         this->construct(rhs.get());
502     }
503 #endif
504
505 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
506     // Creates an optional<T> with an expression which can be either
507     //  (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
508     //  (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
509     //  (c) Any expression implicitely convertible to the single type
510     //      of a one-argument T's constructor.
511     //  (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
512     //       even though explicit overloads are present for these.
513     // Depending on the above some T ctor is called.
514     // Can throw is the resolved T ctor throws.
515     template<class Expr>
516     explicit optional ( Expr const& expr ) : base(expr,&expr) {}
517 #endif
518
519     // Creates a deep copy of another optional<T>
520     // Can throw if T::T(T const&) does
521     optional ( optional const& rhs ) : base(rhs) {}
522
523    // No-throw (assuming T::~T() doesn't)
524     ~optional() {}
525
526 #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
527     // Assigns from an expression. See corresponding constructor.
528     // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
529     template<class Expr>
530     optional& operator= ( Expr expr )
531       {
532         this->assign_expr(expr,&expr);
533         return *this ;
534       }
535 #endif
536
537
538 #ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
539     // Assigns from another convertible optional<U> (converts && deep-copies the rhs value)
540     // Requires a valid conversion from U to T.
541     // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
542     template<class U>
543     optional& operator= ( optional<U> const& rhs )
544       {
545         this->assign(rhs);
546         return *this ;
547       }
548 #endif
549
550     // Assigns from another optional<T> (deep-copies the rhs value)
551     // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
552     //  (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
553     optional& operator= ( optional const& rhs )
554       {
555         this->assign( rhs ) ;
556         return *this ;
557       }
558
559     // Assigns from a T (deep-copies the rhs value)
560     // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
561     optional& operator= ( argument_type val )
562       {
563         this->assign( val ) ;
564         return *this ;
565       }
566
567     // Assigns from a "none"
568     // Which destroys the current value, if any, leaving this UNINITIALIZED
569     // No-throw (assuming T::~T() doesn't)
570     optional& operator= ( none_t none_ )
571       {
572         this->assign( none_ ) ;
573         return *this ;
574       }
575
576     // Returns a reference to the value if this is initialized, otherwise,
577     // the behaviour is UNDEFINED
578     // No-throw
579     reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
580     reference_type       get()       { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
581
582     // Returns a copy of the value if this is initialized, 'v' otherwise
583     reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; }
584     reference_type       get_value_or ( reference_type       v )       { return this->is_initialized() ? get() : v ; }
585     
586     // Returns a pointer to the value if this is initialized, otherwise,
587     // the behaviour is UNDEFINED
588     // No-throw
589     pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
590     pointer_type       operator->()       { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
591
592     // Returns a reference to the value if this is initialized, otherwise,
593     // the behaviour is UNDEFINED
594     // No-throw
595     reference_const_type operator *() const { return this->get() ; }
596     reference_type       operator *()       { return this->get() ; }
597
598     // implicit conversion to "bool"
599     // No-throw
600     operator unspecified_bool_type() const { return this->safe_bool() ; }
601
602        // This is provided for those compilers which don't like the conversion to bool
603        // on some contexts.
604        bool operator!() const { return !this->is_initialized() ; }
605 } ;
606
607 // Returns optional<T>(v)
608 template<class T> 
609 inline 
610 optional<T> make_optional ( T const& v  )
611 {
612   return optional<T>(v);
613 }
614
615 // Returns optional<T>(cond,v)
616 template<class T> 
617 inline 
618 optional<T> make_optional ( bool cond, T const& v )
619 {
620   return optional<T>(cond,v);
621 }
622
623 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
624 // No-throw
625 template<class T>
626 inline
627 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
628 get ( optional<T> const& opt )
629 {
630   return opt.get() ;
631 }
632
633 template<class T>
634 inline
635 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
636 get ( optional<T>& opt )
637 {
638   return opt.get() ;
639 }
640
641 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
642 // No-throw
643 template<class T>
644 inline
645 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
646 get ( optional<T> const* opt )
647 {
648   return opt->get_ptr() ;
649 }
650
651 template<class T>
652 inline
653 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
654 get ( optional<T>* opt )
655 {
656   return opt->get_ptr() ;
657 }
658
659 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
660 // No-throw
661 template<class T>
662 inline
663 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
664 get_optional_value_or ( optional<T> const& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type v )
665 {
666   return opt.get_value_or(v) ;
667 }
668
669 template<class T>
670 inline
671 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
672 get_optional_value_or ( optional<T>& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_type v )
673 {
674   return opt.get_value_or(v) ;
675 }
676
677 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
678 // No-throw
679 template<class T>
680 inline
681 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
682 get_pointer ( optional<T> const& opt )
683 {
684   return opt.get_ptr() ;
685 }
686
687 template<class T>
688 inline
689 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
690 get_pointer ( optional<T>& opt )
691 {
692   return opt.get_ptr() ;
693 }
694
695 // optional's relational operators ( ==, !=, <, >, <=, >= ) have deep-semantics (compare values).
696 // WARNING: This is UNLIKE pointers. Use equal_pointees()/less_pointess() in generic code instead.
697
698
699 //
700 // optional<T> vs optional<T> cases
701 //
702
703 template<class T>
704 inline
705 bool operator == ( optional<T> const& x, optional<T> const& y )
706 { return equal_pointees(x,y); }
707
708 template<class T>
709 inline
710 bool operator < ( optional<T> const& x, optional<T> const& y )
711 { return less_pointees(x,y); }
712
713 template<class T>
714 inline
715 bool operator != ( optional<T> const& x, optional<T> const& y )
716 { return !( x == y ) ; }
717
718 template<class T>
719 inline
720 bool operator > ( optional<T> const& x, optional<T> const& y )
721 { return y < x ; }
722
723 template<class T>
724 inline
725 bool operator <= ( optional<T> const& x, optional<T> const& y )
726 { return !( y < x ) ; }
727
728 template<class T>
729 inline
730 bool operator >= ( optional<T> const& x, optional<T> const& y )
731 { return !( x < y ) ; }
732
733
734 //
735 // optional<T> vs T cases
736 //
737 template<class T>
738 inline
739 bool operator == ( optional<T> const& x, T const& y )
740 { return equal_pointees(x, optional<T>(y)); }
741
742 template<class T>
743 inline
744 bool operator < ( optional<T> const& x, T const& y )
745 { return less_pointees(x, optional<T>(y)); }
746
747 template<class T>
748 inline
749 bool operator != ( optional<T> const& x, T const& y )
750 { return !( x == y ) ; }
751
752 template<class T>
753 inline
754 bool operator > ( optional<T> const& x, T const& y )
755 { return y < x ; }
756
757 template<class T>
758 inline
759 bool operator <= ( optional<T> const& x, T const& y )
760 { return !( y < x ) ; }
761
762 template<class T>
763 inline
764 bool operator >= ( optional<T> const& x, T const& y )
765 { return !( x < y ) ; }
766
767 //
768 // T vs optional<T> cases
769 //
770
771 template<class T>
772 inline
773 bool operator == ( T const& x, optional<T> const& y )
774 { return equal_pointees( optional<T>(x), y ); }
775
776 template<class T>
777 inline
778 bool operator < ( T const& x, optional<T> const& y )
779 { return less_pointees( optional<T>(x), y ); }
780
781 template<class T>
782 inline
783 bool operator != ( T const& x, optional<T> const& y )
784 { return !( x == y ) ; }
785
786 template<class T>
787 inline
788 bool operator > ( T const& x, optional<T> const& y )
789 { return y < x ; }
790
791 template<class T>
792 inline
793 bool operator <= ( T const& x, optional<T> const& y )
794 { return !( y < x ) ; }
795
796 template<class T>
797 inline
798 bool operator >= ( T const& x, optional<T> const& y )
799 { return !( x < y ) ; }
800
801
802 //
803 // optional<T> vs none cases
804 //
805
806 template<class T>
807 inline
808 bool operator == ( optional<T> const& x, none_t )
809 { return equal_pointees(x, optional<T>() ); }
810
811 template<class T>
812 inline
813 bool operator < ( optional<T> const& x, none_t )
814 { return less_pointees(x,optional<T>() ); }
815
816 template<class T>
817 inline
818 bool operator != ( optional<T> const& x, none_t y )
819 { return !( x == y ) ; }
820
821 template<class T>
822 inline
823 bool operator > ( optional<T> const& x, none_t y )
824 { return y < x ; }
825
826 template<class T>
827 inline
828 bool operator <= ( optional<T> const& x, none_t y )
829 { return !( y < x ) ; }
830
831 template<class T>
832 inline
833 bool operator >= ( optional<T> const& x, none_t y )
834 { return !( x < y ) ; }
835
836 //
837 // none vs optional<T> cases
838 //
839
840 template<class T>
841 inline
842 bool operator == ( none_t x, optional<T> const& y )
843 { return equal_pointees(optional<T>() ,y); }
844
845 template<class T>
846 inline
847 bool operator < ( none_t x, optional<T> const& y )
848 { return less_pointees(optional<T>() ,y); }
849
850 template<class T>
851 inline
852 bool operator != ( none_t x, optional<T> const& y )
853 { return !( x == y ) ; }
854
855 template<class T>
856 inline
857 bool operator > ( none_t x, optional<T> const& y )
858 { return y < x ; }
859
860 template<class T>
861 inline
862 bool operator <= ( none_t x, optional<T> const& y )
863 { return !( y < x ) ; }
864
865 template<class T>
866 inline
867 bool operator >= ( none_t x, optional<T> const& y )
868 { return !( x < y ) ; }
869
870 //
871 // The following swap implementation follows the GCC workaround as found in
872 //  "boost/detail/compressed_pair.hpp"
873 //
874 namespace optional_detail {
875
876 // GCC < 3.2 gets the using declaration at namespace scope (FLC, DWA)
877 #if BOOST_WORKAROUND(__GNUC__, < 3)                             \
878     || BOOST_WORKAROUND(__GNUC__, == 3) && __GNUC_MINOR__ <= 2
879    using std::swap;
880 #define BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
881 #endif
882
883 // optional's swap:
884 // If both are initialized, calls swap(T&, T&). If this swap throws, both will remain initialized but their values are now unspecified.
885 // If only one is initialized, calls U.reset(*I), THEN I.reset().
886 // If U.reset(*I) throws, both are left UNCHANGED (U is kept uinitialized and I is never reset)
887 // If both are uninitialized, do nothing (no-throw)
888 template<class T>
889 inline
890 void optional_swap ( optional<T>& x, optional<T>& y )
891 {
892   if ( !x && !!y )
893   {
894     x.reset(*y);
895     y.reset();
896   }
897   else if ( !!x && !y )
898   {
899     y.reset(*x);
900     x.reset();
901   }
902   else if ( !!x && !!y )
903   {
904 // GCC > 3.2 and all other compilers have the using declaration at function scope (FLC)
905 #ifndef BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
906     // allow for Koenig lookup
907     using std::swap ;
908 #endif
909     swap(*x,*y);
910   }
911 }
912
913 } // namespace optional_detail
914
915 template<class T> inline void swap ( optional<T>& x, optional<T>& y )
916 {
917   optional_detail::optional_swap(x,y);
918 }
919
920
921 } // namespace boost
922
923 #endif
924