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