Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/boost/optional/optional.hpp @ 13

Last change on this file since 13 was 12, checked in by landauf, 17 years ago

added boost

File size: 24.2 KB
Line 
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_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_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
78namespace boost {
79
80class in_place_factory_base ;
81class typed_in_place_factory_base ;
82
83namespace 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)
89template <class T>
90class 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
106template<class T>
107struct 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} ;
115template<class T>
116struct 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
127struct optional_tag {} ;
128
129template<class T>
130class 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 ( 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
194    // This is used for both converting and in-place constructions.
195    // Derived classes use the 'tag' to select the appropriate
196    // implementation (the correct 'construct()' overload)
197    template<class Expr>
198    explicit optional_base ( Expr const& expr, Expr const* tag )
199      :
200      m_initialized(false)
201    {
202      construct(expr,tag);
203    }
204
205
206
207    // No-throw (assuming T::~T() doesn't)
208    ~optional_base() { destroy() ; }
209
210    // Assigns from another optional<T> (deep-copies the rhs value)
211    void assign ( optional_base const& rhs )
212    {
213      if (is_initialized())
214      {
215        if ( rhs.is_initialized() )
216             assign_value(rhs.get_impl(), is_reference_predicate() );
217        else destroy();
218      }
219      else
220      {
221        if ( rhs.is_initialized() )
222          construct(rhs.get_impl());
223      }
224    }
225
226    // Assigns from a T (deep-copies the rhs value)
227    void assign ( argument_type val )
228    {
229      if (is_initialized())
230           assign_value(val, is_reference_predicate() );
231      else construct(val);
232    }
233
234    // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
235    // No-throw (assuming T::~T() doesn't)
236    void assign ( none_t const& ) { destroy(); }
237
238#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
239    template<class Expr>
240    void assign_expr ( Expr const& expr, Expr const* tag )
241      {
242        if (is_initialized())
243             assign_expr_to_initialized(expr,tag);
244        else construct(expr,tag);
245      }
246#endif
247
248  public :
249
250    // Destroys the current value, if any, leaving this UNINITIALIZED
251    // No-throw (assuming T::~T() doesn't)
252    void reset() { destroy(); }
253
254    // Replaces the current value -if any- with 'val'
255    void reset ( argument_type val ) { assign(val); }
256
257    // Returns a pointer to the value if this is initialized, otherwise,
258    // returns NULL.
259    // No-throw
260    pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
261    pointer_type       get_ptr()       { return m_initialized ? get_ptr_impl() : 0 ; }
262
263    bool is_initialized() const { return m_initialized ; }
264
265  protected :
266
267    void construct ( argument_type val )
268     {
269       new (m_storage.address()) internal_type(val) ;
270       m_initialized = true ;
271     }
272
273#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
274    // Constructs in-place using the given factory
275    template<class Expr>
276    void construct ( Expr const& factory, in_place_factory_base const* )
277     {
278       BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
279       factory.BOOST_NESTED_TEMPLATE apply<value_type>(m_storage.address()) ;
280       m_initialized = true ;
281     }
282
283    // Constructs in-place using the given typed factory
284    template<class Expr>
285    void construct ( Expr const& factory, typed_in_place_factory_base const* )
286     {
287       BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
288       factory.apply(m_storage.address()) ;
289       m_initialized = true ;
290     }
291
292    template<class Expr>
293    void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
294     {
295       destroy();
296       construct(factory,tag);
297     }
298
299    // Constructs in-place using the given typed factory
300    template<class Expr>
301    void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
302     {
303       destroy();
304       construct(factory,tag);
305     }
306#endif
307
308    // Constructs using any expression implicitely convertible to the single argument
309    // of a one-argument T constructor.
310    // Converting constructions of optional<T> from optional<U> uses this function with
311    // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
312    template<class Expr>
313    void construct ( Expr const& expr, void const* )
314     {
315       new (m_storage.address()) internal_type(expr) ;
316       m_initialized = true ;
317     }
318
319    // Assigns using a form any expression implicitely convertible to the single argument
320    // of a T's assignment operator.
321    // Converting assignments of optional<T> from optional<U> uses this function with
322    // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
323    template<class Expr>
324    void assign_expr_to_initialized ( Expr const& expr, void const* )
325     {
326       assign_value(expr, is_reference_predicate());
327     }
328
329#ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
330    // BCB5.64 (and probably lower versions) workaround.
331    //   The in-place factories are supported by means of catch-all constructors
332    //   and assignment operators (the functions are parameterized in terms of
333    //   an arbitrary 'Expr' type)
334    //   This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
335    //   to the 'Expr'-taking functions even though explicit overloads are present for them.
336    //   Thus, the following overload is needed to properly handle the case when the 'lhs'
337    //   is another optional.
338    //
339    // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
340    // instead of choosing the wrong overload
341    //
342    // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
343    template<class Expr>
344    void construct ( Expr const& expr, optional_tag const* )
345     {
346       if ( expr.is_initialized() )
347       {
348         // An exception can be thrown here.
349         // It it happens, THIS will be left uninitialized.
350         new (m_storage.address()) internal_type(expr.get()) ;
351         m_initialized = true ;
352       }
353     }
354#endif
355
356    void assign_value ( argument_type val, is_not_reference_tag ) { get_impl() = val; }
357    void assign_value ( argument_type val, is_reference_tag     ) { construct(val); }
358
359    void destroy()
360    {
361      if ( m_initialized )
362        destroy_impl(is_reference_predicate()) ;
363    }
364
365    unspecified_bool_type safe_bool() const { return m_initialized ? &this_type::is_initialized : 0 ; }
366
367    reference_const_type get_impl() const { return dereference(get_object(), is_reference_predicate() ) ; }
368    reference_type       get_impl()       { return dereference(get_object(), is_reference_predicate() ) ; }
369
370    pointer_const_type get_ptr_impl() const { return cast_ptr(get_object(), is_reference_predicate() ) ; }
371    pointer_type       get_ptr_impl()       { return cast_ptr(get_object(), is_reference_predicate() ) ; }
372
373  private :
374
375    // internal_type can be either T or reference_content<T>
376    internal_type const* get_object() const { return static_cast<internal_type const*>(m_storage.address()); }
377    internal_type *      get_object()       { return static_cast<internal_type *>     (m_storage.address()); }
378
379    // reference_content<T> lacks an implicit conversion to T&, so the following is needed to obtain a proper reference.
380    reference_const_type dereference( internal_type const* p, is_not_reference_tag ) const { return *p ; }
381    reference_type       dereference( internal_type*       p, is_not_reference_tag )       { return *p ; }
382    reference_const_type dereference( internal_type const* p, is_reference_tag     ) const { return p->get() ; }
383    reference_type       dereference( internal_type*       p, is_reference_tag     )       { return p->get() ; }
384
385#if BOOST_WORKAROUND(__BORLANDC__, <= 0x564)
386    void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->internal_type::~internal_type() ; m_initialized = false ; }
387#else
388    void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->T::~T() ; m_initialized = false ; }
389#endif
390
391    void destroy_impl ( is_reference_tag     ) { m_initialized = false ; }
392
393    // If T is of reference type, trying to get a pointer to the held value must result in a compile-time error.
394    // Decent compilers should disallow conversions from reference_content<T>* to T*, but just in case,
395    // the following olverloads are used to filter out the case and guarantee an error in case of T being a reference.
396    pointer_const_type cast_ptr( internal_type const* p, is_not_reference_tag ) const { return p ; }
397    pointer_type       cast_ptr( internal_type *      p, is_not_reference_tag )       { return p ; }
398
399    bool m_initialized ;
400    storage_type m_storage ;
401} ;
402
403} // namespace optional_detail
404
405template<class T>
406class optional : public optional_detail::optional_base<T>
407{
408    typedef optional_detail::optional_base<T> base ;
409
410    typedef BOOST_DEDUCED_TYPENAME base::unspecified_bool_type  unspecified_bool_type ;
411
412  public :
413
414    typedef optional<T> this_type ;
415
416    typedef BOOST_DEDUCED_TYPENAME base::value_type           value_type ;
417    typedef BOOST_DEDUCED_TYPENAME base::reference_type       reference_type ;
418    typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
419    typedef BOOST_DEDUCED_TYPENAME base::pointer_type         pointer_type ;
420    typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type   pointer_const_type ;
421    typedef BOOST_DEDUCED_TYPENAME base::argument_type        argument_type ;
422
423    // Creates an optional<T> uninitialized.
424    // No-throw
425    optional() : base() {}
426
427    // Creates an optional<T> uninitialized.
428    // No-throw
429    optional( none_t const& none_ ) : base(none_) {}
430
431    // Creates an optional<T> initialized with 'val'.
432    // Can throw if T::T(T const&) does
433    optional ( argument_type val ) : base(val) {}
434
435
436#ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
437    // NOTE: MSVC needs templated versions first
438
439    // Creates a deep copy of another convertible optional<U>
440    // Requires a valid conversion from U to T.
441    // Can throw if T::T(U const&) does
442    template<class U>
443    explicit optional ( optional<U> const& rhs )
444      :
445      base()
446    {
447      if ( rhs.is_initialized() )
448        this->construct(rhs.get());
449    }
450#endif
451
452#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
453    // Creates an optional<T> with an expression which can be either
454    //  (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
455    //  (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
456    //  (c) Any expression implicitely convertible to the single type
457    //      of a one-argument T's constructor.
458    //  (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
459    //       even though explicit overloads are present for these.
460    // Depending on the above some T ctor is called.
461    // Can throw is the resolved T ctor throws.
462    template<class Expr>
463    explicit optional ( Expr const& expr ) : base(expr,&expr) {}
464#endif
465
466    // Creates a deep copy of another optional<T>
467    // Can throw if T::T(T const&) does
468    optional ( optional const& rhs ) : base(rhs) {}
469
470   // No-throw (assuming T::~T() doesn't)
471    ~optional() {}
472
473#if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
474    // Assigns from an expression. See corresponding constructor.
475    // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
476    template<class Expr>
477    optional& operator= ( Expr expr )
478      {
479        this->assign_expr(expr,&expr);
480        return *this ;
481      }
482#endif
483
484#ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
485    // Assigns from another convertible optional<U> (converts && deep-copies the rhs value)
486    // Requires a valid conversion from U to T.
487    // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
488    template<class U>
489    optional& operator= ( optional<U> const& rhs )
490      {
491        this->assign(rhs.get());
492        return *this ;
493      }
494#endif
495
496    // Assigns from another optional<T> (deep-copies the rhs value)
497    // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
498    //  (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
499    optional& operator= ( optional const& rhs )
500      {
501        this->assign( rhs ) ;
502        return *this ;
503      }
504
505    // Assigns from a T (deep-copies the rhs value)
506    // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
507    optional& operator= ( argument_type val )
508      {
509        this->assign( val ) ;
510        return *this ;
511      }
512
513    // Assigns from a "none"
514    // Which destroys the current value, if any, leaving this UNINITIALIZED
515    // No-throw (assuming T::~T() doesn't)
516    optional& operator= ( none_t const& none_ )
517      {
518        this->assign( none_ ) ;
519        return *this ;
520      }
521
522    // Returns a reference to the value if this is initialized, otherwise,
523    // the behaviour is UNDEFINED
524    // No-throw
525    reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
526    reference_type       get()       { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
527
528    // Returns a pointer to the value if this is initialized, otherwise,
529    // the behaviour is UNDEFINED
530    // No-throw
531    pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
532    pointer_type       operator->()       { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
533
534    // Returns a reference to the value if this is initialized, otherwise,
535    // the behaviour is UNDEFINED
536    // No-throw
537    reference_const_type operator *() const { return this->get() ; }
538    reference_type       operator *()       { return this->get() ; }
539
540    // implicit conversion to "bool"
541    // No-throw
542    operator unspecified_bool_type() const { return this->safe_bool() ; }
543
544       // This is provided for those compilers which don't like the conversion to bool
545       // on some contexts.
546       bool operator!() const { return !this->is_initialized() ; }
547} ;
548
549// Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
550// No-throw
551template<class T>
552inline
553BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
554get ( optional<T> const& opt )
555{
556  return opt.get() ;
557}
558
559template<class T>
560inline
561BOOST_DEDUCED_TYPENAME optional<T>::reference_type
562get ( optional<T>& opt )
563{
564  return opt.get() ;
565}
566
567// Returns a pointer to the value if this is initialized, otherwise, returns NULL.
568// No-throw
569template<class T>
570inline
571BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
572get ( optional<T> const* opt )
573{
574  return opt->get_ptr() ;
575}
576
577template<class T>
578inline
579BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
580get ( optional<T>* opt )
581{
582  return opt->get_ptr() ;
583}
584
585// Returns a pointer to the value if this is initialized, otherwise, returns NULL.
586// No-throw
587template<class T>
588inline
589BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
590get_pointer ( optional<T> const& opt )
591{
592  return opt.get_ptr() ;
593}
594
595template<class T>
596inline
597BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
598get_pointer ( optional<T>& opt )
599{
600  return opt.get_ptr() ;
601}
602
603// optional's relational operators ( ==, !=, <, >, <=, >= ) have deep-semantics (compare values).
604// WARNING: This is UNLIKE pointers. Use equal_pointees()/less_pointess() in generic code instead.
605
606template<class T>
607inline
608bool operator == ( optional<T> const& x, optional<T> const& y )
609{ return equal_pointees(x,y); }
610
611template<class T>
612inline
613bool operator < ( optional<T> const& x, optional<T> const& y )
614{ return less_pointees(x,y); }
615
616template<class T>
617inline
618bool operator != ( optional<T> const& x, optional<T> const& y )
619{ return !( x == y ) ; }
620
621template<class T>
622inline
623bool operator > ( optional<T> const& x, optional<T> const& y )
624{ return y < x ; }
625
626template<class T>
627inline
628bool operator <= ( optional<T> const& x, optional<T> const& y )
629{ return !( y < x ) ; }
630
631template<class T>
632inline
633bool operator >= ( optional<T> const& x, optional<T> const& y )
634{ return !( x < y ) ; }
635
636template<class T>
637inline
638bool operator == ( optional<T> const& x, none_t const& )
639{ return equal_pointees(x, optional<T>() ); }
640
641template<class T>
642inline
643bool operator < ( optional<T> const& x, none_t const& )
644{ return less_pointees(x,optional<T>() ); }
645
646template<class T>
647inline
648bool operator != ( optional<T> const& x, none_t const& y )
649{ return !( x == y ) ; }
650
651template<class T>
652inline
653bool operator > ( optional<T> const& x, none_t const& y )
654{ return y < x ; }
655
656template<class T>
657inline
658bool operator <= ( optional<T> const& x, none_t const& y )
659{ return !( y < x ) ; }
660
661template<class T>
662inline
663bool operator >= ( optional<T> const& x, none_t const& y )
664{ return !( x < y ) ; }
665
666template<class T>
667inline
668bool operator == ( none_t const& x, optional<T> const& y )
669{ return equal_pointees(optional<T>() ,y); }
670
671template<class T>
672inline
673bool operator < ( none_t const& x, optional<T> const& y )
674{ return less_pointees(optional<T>() ,y); }
675
676template<class T>
677inline
678bool operator != ( none_t const& x, optional<T> const& y )
679{ return !( x == y ) ; }
680
681template<class T>
682inline
683bool operator > ( none_t const& x, optional<T> const& y )
684{ return y < x ; }
685
686template<class T>
687inline
688bool operator <= ( none_t const& x, optional<T> const& y )
689{ return !( y < x ) ; }
690
691template<class T>
692inline
693bool operator >= ( none_t const& x, optional<T> const& y )
694{ return !( x < y ) ; }
695
696//
697// The following swap implementation follows the GCC workaround as found in
698//  "boost/detail/compressed_pair.hpp"
699//
700namespace optional_detail {
701
702// GCC < 3.2 gets the using declaration at namespace scope (FLC, DWA)
703#if BOOST_WORKAROUND(__GNUC__, < 3)                             \
704    || BOOST_WORKAROUND(__GNUC__, == 3) && __GNUC_MINOR__ <= 2
705   using std::swap;
706#define BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
707#endif
708
709// optional's swap:
710// If both are initialized, calls swap(T&, T&). If this swap throws, both will remain initialized but their values are now unspecified.
711// If only one is initialized, calls U.reset(*I), THEN I.reset().
712// If U.reset(*I) throws, both are left UNCHANGED (U is kept uinitialized and I is never reset)
713// If both are uninitialized, do nothing (no-throw)
714template<class T>
715inline
716void optional_swap ( optional<T>& x, optional<T>& y )
717{
718  if ( !x && !!y )
719  {
720    x.reset(*y);
721    y.reset();
722  }
723  else if ( !!x && !y )
724  {
725    y.reset(*x);
726    x.reset();
727  }
728  else if ( !!x && !!y )
729  {
730// GCC > 3.2 and all other compilers have the using declaration at function scope (FLC)
731#ifndef BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
732    // allow for Koenig lookup
733    using std::swap ;
734#endif
735    swap(*x,*y);
736  }
737}
738
739} // namespace optional_detail
740
741template<class T> inline void swap ( optional<T>& x, optional<T>& y )
742{
743  optional_detail::optional_swap(x,y);
744}
745
746} // namespace boost
747
748#endif
749
Note: See TracBrowser for help on using the repository browser.