]> git.lyx.org Git - lyx.git/blob - boost/boost/optional.hpp
64-bit fix to boost::format.
[lyx.git] / boost / boost / 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/lib/optional for documentation.
8 //
9 // You are welcome to contact the author at:
10 //  fernando_cacciola@hotmail.com
11 //
12 #ifndef BOOST_OPTIONAL_FLC_19NOV2002_HPP
13 #define BOOST_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/detail/none_t.hpp"
30 #include "boost/utility/compare_pointees.hpp"
31
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(...);
37 //   is compiled.
38 //   However, optional's ctor is _explicit_ and the assignemt shouldn't compile.
39 //   Therefore, for VC6.0 templated assignment is disabled.
40 //
41 #define BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
42 #endif
43
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.
49 //
50 #define BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
51 #endif
52
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
57 // is disabled
58 #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
59 #endif
60
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
64 #endif
65
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
75 #endif
76
77
78 namespace boost {
79
80 class InPlaceFactoryBase ;
81 class TypedInPlaceFactoryBase ;
82
83 namespace optional_detail {
84
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)
89 template <class T>
90 class aligned_storage
91 {
92     // Borland ICEs if unnamed unions are used for this!
93     union dummy_u
94     {
95         char data[ sizeof(T) ];
96         BOOST_DEDUCED_TYPENAME type_with_alignment<
97           ::boost::alignment_of<T>::value >::type aligner_;
98     } dummy_ ;
99
100   public:
101
102     void const* address() const { return &dummy_.data[0]; }
103     void      * address()       { return &dummy_.data[0]; }
104 } ;
105
106 template<class T>
107 struct types_when_isnt_ref
108 {
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 ;
114 } ;
115 template<class T>
116 struct types_when_is_ref
117 {
118   typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type raw_type ;
119
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 ;
125 } ;
126
127 struct optional_tag {} ;
128
129 template<class T>
130 class optional_base : public optional_tag
131 {
132   private :
133
134     typedef BOOST_DEDUCED_TYPENAME detail::make_reference_content<T>::type internal_type ;
135
136     typedef aligned_storage<internal_type> storage_type ;
137
138     typedef types_when_isnt_ref<T> types_when_not_ref ;
139     typedef types_when_is_ref<T>   types_when_ref   ;
140
141     typedef optional_base<T> this_type ;
142
143   protected :
144
145     typedef T value_type ;
146     
147     typedef mpl::true_  is_reference_tag ;
148     typedef mpl::false_ is_not_reference_tag ;
149
150     typedef BOOST_DEDUCED_TYPENAME is_reference<T>::type is_reference_predicate ;
151
152     typedef BOOST_DEDUCED_TYPENAME mpl::if_<is_reference_predicate,types_when_ref,types_when_not_ref>::type types ;
153
154     typedef bool (this_type::*unspecified_bool_type)() const;
155
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 ;
161
162     // Creates an optional<T> uninitialized.
163     // No-throw
164     optional_base()
165       :
166       m_initialized(false) {}
167
168     // Creates an optional<T> uninitialized.
169     // No-throw
170     optional_base ( detail::none_t const& )
171       :
172       m_initialized(false) {}
173
174     // Creates an optional<T> initialized with 'val'.
175     // Can throw if T::T(T const&) does
176     optional_base ( argument_type val )
177       :
178       m_initialized(false)
179     {
180       construct(val);
181     }
182
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 )
186       :
187       m_initialized(false)
188     {
189       if ( rhs.is_initialized() )
190         construct(rhs.get_impl());
191     }
192
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)
196     template<class Expr>
197     explicit optional_base ( Expr const& expr, Expr const* tag )
198       :
199       m_initialized(false)
200     {
201       construct(expr,tag);
202     }
203
204     // No-throw (assuming T::~T() doesn't)
205     ~optional_base() { destroy() ; }
206
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 )
210       {
211         destroy();
212         if ( rhs.is_initialized() )
213           construct(rhs.get_impl());
214       }
215
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 )
219       {
220         destroy();
221         construct(val);
222       }
223
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(); }
227
228 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
229     template<class Expr>
230     void assign_expr ( Expr const& expr, Expr const* tag ) 
231       {
232         destroy();
233         construct(expr,tag);
234       }
235 #endif
236
237   public :
238
239     // Destroys the current value, if any, leaving this UNINITIALIZED
240     // No-throw (assuming T::~T() doesn't)
241     void reset() { destroy(); }
242
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); }
246
247     // Returns a pointer to the value if this is initialized, otherwise,
248     // returns NULL.
249     // No-throw
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 ; }
252
253     bool is_initialized() const { return m_initialized ; }
254
255   protected :
256
257     void construct ( argument_type val )
258      {
259        new (m_storage.address()) internal_type(val) ;
260        m_initialized = true ;
261      }
262
263 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
264     // Constructs in-place using the given factory
265     template<class Expr>
266     void construct ( Expr const& factory, InPlaceFactoryBase const* )
267      {
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 ;
271      }
272
273     // Constructs in-place using the given typed factory
274     template<class Expr>
275     void construct ( Expr const& factory, TypedInPlaceFactoryBase const* )
276      {
277        BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
278        factory.apply(m_storage.address()) ;
279        m_initialized = true ;
280      }
281 #endif
282
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.
287     template<class Expr>
288     void construct ( Expr const& expr, void const* )
289      {
290        new (m_storage.address()) internal_type(expr) ;
291        m_initialized = true ;
292      }
293
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.
303     //
304     // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
305     // instead of choosing the wrong overload
306     //
307     // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
308     template<class Expr>
309     void construct ( Expr const& expr, optional_tag const* )
310      {
311        if ( expr.is_initialized() )
312        {
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 ;
317        }
318      }
319 #endif
320
321     void destroy()
322       {
323         if ( m_initialized )
324           destroy_impl(is_reference_predicate()) ;
325       }
326
327     unspecified_bool_type safe_bool() const { return m_initialized ? &this_type::is_initialized : 0 ; }
328
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() ) ; }
331
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() ) ; }
334
335   private :
336
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()); }
340
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() ; }
346
347     void destroy_impl ( is_not_reference_tag ) { get_impl().~T() ; m_initialized = false ; }
348     void destroy_impl ( is_reference_tag     ) { m_initialized = false ; }
349
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 ; }
355
356     bool m_initialized ;
357     storage_type m_storage ;
358 } ;
359
360 } // namespace optional_detail
361
362 template<class T>
363 class optional : public optional_detail::optional_base<T>
364 {
365     typedef optional_detail::optional_base<T> base ;
366
367     typedef BOOST_DEDUCED_TYPENAME base::unspecified_bool_type  unspecified_bool_type ;
368
369   public :
370
371     typedef optional<T> this_type ;
372
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 ;
379
380     // Creates an optional<T> uninitialized.
381     // No-throw
382     optional() : base() {}
383
384     // Creates an optional<T> uninitialized.
385     // No-throw
386     optional( detail::none_t const& none_ ) : base(none_) {}
387
388     // Creates an optional<T> initialized with 'val'.
389     // Can throw if T::T(T const&) does
390     optional ( argument_type val ) : base(val) {}
391
392 #ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
393     // NOTE: MSVC needs templated versions first
394
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
398     template<class U>
399     explicit optional ( optional<U> const& rhs )
400       :
401       base()
402     {
403       if ( rhs.is_initialized() )
404         this->construct(rhs.get());
405     }
406 #endif
407
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.
418     template<class Expr>
419     explicit optional ( Expr const& expr ) : base(expr,&expr) {}
420 #endif
421
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) {}
425
426     // No-throw (assuming T::~T() doesn't)
427     ~optional() {}
428
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
432     template<class Expr>
433     optional& operator= ( Expr expr )
434       {
435         this->assign_expr(expr,&expr);
436         return *this ;
437       }
438 #endif
439
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
444     template<class U>
445     optional& operator= ( optional<U> const& rhs )
446       {
447         this->destroy(); // no-throw
448
449         if ( rhs.is_initialized() )
450         {
451           // An exception can be thrown here.
452           // It it happens, THIS will be left uninitialized.
453           this->assign(rhs.get());
454         }
455         return *this ;
456       }
457 #endif
458
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 )
463       {
464         this->assign( rhs ) ;
465         return *this ;
466       }
467
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 )
471       {
472         this->assign( val ) ;
473         return *this ;
474       }
475
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_ )
480       {
481         this->assign( none_ ) ;
482         return *this ;
483       }
484
485     // Returns a reference to the value if this is initialized, otherwise,
486     // the behaviour is UNDEFINED
487     // No-throw
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(); }
490
491     // Returns a pointer to the value if this is initialized, otherwise,
492     // the behaviour is UNDEFINED
493     // No-throw
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() ; }
496
497     // Returns a reference to the value if this is initialized, otherwise,
498     // the behaviour is UNDEFINED
499     // No-throw
500     reference_const_type operator *() const { return this->get() ; }
501     reference_type       operator *()       { return this->get() ; }
502
503     // implicit conversion to "bool"
504     // No-throw
505     operator unspecified_bool_type() const { return this->safe_bool() ; }
506
507        // This is provided for those compilers which don't like the conversion to bool
508        // on some contexts.
509        bool operator!() const { return !this->is_initialized() ; }
510 } ;
511
512 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
513 // No-throw
514 template<class T>
515 inline
516 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
517 get ( optional<T> const& opt )
518 {
519   return opt.get() ;
520 }
521
522 template<class T>
523 inline
524 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
525 get ( optional<T>& opt )
526 {
527   return opt.get() ;
528 }
529
530 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
531 // No-throw
532 template<class T>
533 inline
534 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
535 get ( optional<T> const* opt )
536 {
537   return opt->get_ptr() ;
538 }
539
540 template<class T>
541 inline
542 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
543 get ( optional<T>* opt )
544 {
545   return opt->get_ptr() ;
546 }
547
548 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
549 // No-throw
550 template<class T>
551 inline
552 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
553 get_pointer ( optional<T> const& opt )
554 {
555   return opt.get_ptr() ;
556 }
557
558 template<class T>
559 inline
560 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
561 get_pointer ( optional<T>& opt )
562 {
563   return opt.get_ptr() ;
564 }
565
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.
568
569 template<class T>
570 inline
571 bool operator == ( optional<T> const& x, optional<T> const& y )
572 { return equal_pointees(x,y); }
573
574 template<class T>
575 inline
576 bool operator < ( optional<T> const& x, optional<T> const& y )
577 { return less_pointees(x,y); }
578
579 template<class T>
580 inline
581 bool operator != ( optional<T> const& x, optional<T> const& y )
582 { return !( x == y ) ; }
583
584 template<class T>
585 inline
586 bool operator > ( optional<T> const& x, optional<T> const& y )
587 { return y < x ; }
588
589 template<class T>
590 inline
591 bool operator <= ( optional<T> const& x, optional<T> const& y )
592 { return !( y < x ) ; }
593
594 template<class T>
595 inline
596 bool operator >= ( optional<T> const& x, optional<T> const& y )
597 { return !( x < y ) ; }
598
599 template<class T>
600 inline
601 bool operator == ( optional<T> const& x, detail::none_t const& )
602 { return equal_pointees(x, optional<T>() ); }
603
604 template<class T>
605 inline
606 bool operator < ( optional<T> const& x, detail::none_t const& )
607 { return less_pointees(x,optional<T>() ); }
608
609 template<class T>
610 inline
611 bool operator != ( optional<T> const& x, detail::none_t const& y )
612 { return !( x == y ) ; }
613
614 template<class T>
615 inline
616 bool operator > ( optional<T> const& x, detail::none_t const& y )
617 { return y < x ; }
618
619 template<class T>
620 inline
621 bool operator <= ( optional<T> const& x, detail::none_t const& y )
622 { return !( y < x ) ; }
623
624 template<class T>
625 inline
626 bool operator >= ( optional<T> const& x, detail::none_t const& y )
627 { return !( x < y ) ; }
628
629 template<class T>
630 inline
631 bool operator == ( detail::none_t const& x, optional<T> const& y )
632 { return equal_pointees(optional<T>() ,y); }
633
634 template<class T>
635 inline
636 bool operator < ( detail::none_t const& x, optional<T> const& y )
637 { return less_pointees(optional<T>() ,y); }
638
639 template<class T>
640 inline
641 bool operator != ( detail::none_t const& x, optional<T> const& y )
642 { return !( x == y ) ; }
643
644 template<class T>
645 inline
646 bool operator > ( detail::none_t const& x, optional<T> const& y )
647 { return y < x ; }
648
649 template<class T>
650 inline
651 bool operator <= ( detail::none_t const& x, optional<T> const& y )
652 { return !( y < x ) ; }
653
654 template<class T>
655 inline
656 bool operator >= ( detail::none_t const& x, optional<T> const& y )
657 { return !( x < y ) ; }
658
659 //
660 // The following swap implementation follows the GCC workaround as found in
661 //  "boost/detail/compressed_pair.hpp"
662 //
663 namespace optional_detail {
664
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
668    using std::swap;
669 #define BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
670 #endif
671
672 // optional's swap:
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)
676 template<class T>
677 inline
678 void optional_swap ( optional<T>& x, optional<T>& y )
679 {
680   if ( !x && !!y )
681   {
682     x.reset(*y); // Basic guarantee.
683     y.reset();
684   }
685   else if ( !!x && !y )
686   {
687     y.reset(*x); // Basic guarantee.
688     x.reset();
689   }
690   else if ( !!x && !!y )
691   {
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
695     using std::swap ;
696 #endif
697     swap(*x,*y);
698   }
699 }
700
701 } // namespace optional_detail
702
703 template<class T> inline void swap ( optional<T>& x, optional<T>& y )
704 {
705   optional_detail::optional_swap(x,y);
706 }
707
708 } // namespace boost
709
710 #endif
711