mirror of
https://github.com/boostorg/optional.git
synced 2025-06-26 20:41:44 +02:00
Compare commits
9 Commits
svn-branch
...
svn-branch
Author | SHA1 | Date | |
---|---|---|---|
2c839e600b | |||
3ebabcb2d8 | |||
63f6e7f45e | |||
43eac5bb12 | |||
74674531c8 | |||
a4572497be | |||
951b49f992 | |||
1afed544db | |||
66c366d18a |
@ -21,16 +21,16 @@ Distributed under the Boost Software License, Version 1.0.
|
|||||||
|
|
||||||
[/ Cited Boost resources ]
|
[/ Cited Boost resources ]
|
||||||
|
|
||||||
[def __BOOST_VARIANT__ [@/libs/variant/index.html Boost.Variant]]
|
[def __BOOST_VARIANT__ [@../../../variant/index.html Boost.Variant]]
|
||||||
[def __BOOST_TRIBOOL__ [@/doc/html/tribool.html boost::tribool]]
|
[def __BOOST_TRIBOOL__ [@../../../../doc/html/tribool.html boost::tribool]]
|
||||||
|
|
||||||
[def __OPTIONAL_POINTEE__ [@/libs/utility/OptionalPointee.html OptionalPointee]]
|
[def __OPTIONAL_POINTEE__ [@../../../utility/OptionalPointee.html OptionalPointee]]
|
||||||
[def __COPY_CONSTRUCTIBLE__ [@/libs/utility/CopyConstructible.html Copy Constructible]]
|
[def __COPY_CONSTRUCTIBLE__ [@../../../utility/CopyConstructible.html Copy Constructible]]
|
||||||
[def __FUNCTION_EQUAL_POINTEES__ [@/libs/utility/OptionalPointee.html#equal `equal_pointees()`]]
|
[def __FUNCTION_EQUAL_POINTEES__ [@../../../utility/OptionalPointee.html#equal `equal_pointees()`]]
|
||||||
[def __FUNCTION_LESS_POINTEES__ [@/libs/utility/OptionalPointee.html#less `less_pointees()`]]
|
[def __FUNCTION_LESS_POINTEES__ [@../../../utility/OptionalPointee.html#less `less_pointees()`]]
|
||||||
|
|
||||||
[def __IN_PLACE_FACTORY_HPP__ [@/boost/utility/in_place_factory.hpp in_place_factory.hpp]]
|
[def __IN_PLACE_FACTORY_HPP__ [@../../../../boost/utility/in_place_factory.hpp in_place_factory.hpp]]
|
||||||
[def __TYPED_IN_PLACE_FACTORY_HPP__ [@/boost/utility/typed_in_place_factory.hpp typed_in_place_factory.hpp]]
|
[def __TYPED_IN_PLACE_FACTORY_HPP__ [@../../../../boost/utility/typed_in_place_factory.hpp typed_in_place_factory.hpp]]
|
||||||
|
|
||||||
[/ Other web resources ]
|
[/ Other web resources ]
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
// fernando_cacciola@hotmail.com
|
// fernando_cacciola@hotmail.com
|
||||||
//
|
//
|
||||||
// Revisions:
|
// Revisions:
|
||||||
// 25 Apr 2008 (improved swap) Fernando Cacciola, Niels Dekker, Thorsten Ottosen
|
// 27 Apr 2008 (improved swap) Fernando Cacciola, Niels Dekker, Thorsten Ottosen
|
||||||
//
|
//
|
||||||
#ifndef BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
|
#ifndef BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
|
||||||
#define BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
|
#define BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
|
||||||
@ -579,7 +579,7 @@ class optional : public optional_detail::optional_base<T>
|
|||||||
void swap( optional & arg )
|
void swap( optional & arg )
|
||||||
{
|
{
|
||||||
// allow for Koenig lookup
|
// allow for Koenig lookup
|
||||||
using std::swap ;
|
using boost::swap ;
|
||||||
swap(*this, arg);
|
swap(*this, arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
|
// Copyright (C) 2003, 2008 Fernando Luis Cacciola Carballal.
|
||||||
//
|
//
|
||||||
// Use, modification, and distribution is subject to the Boost Software
|
// Use, modification, and distribution is subject to the Boost Software
|
||||||
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||||
@ -9,6 +9,9 @@
|
|||||||
// You are welcome to contact the author at:
|
// You are welcome to contact the author at:
|
||||||
// fernando_cacciola@hotmail.com
|
// fernando_cacciola@hotmail.com
|
||||||
//
|
//
|
||||||
|
// Revisions:
|
||||||
|
// 10 May 2008 (added swap related forward declaration) Niels Dekker
|
||||||
|
//
|
||||||
#ifndef BOOST_OPTIONAL_OPTIONAL_FWD_FLC_19NOV2002_HPP
|
#ifndef BOOST_OPTIONAL_OPTIONAL_FWD_FLC_19NOV2002_HPP
|
||||||
#define BOOST_OPTIONAL_OPTIONAL_FWD_FLC_19NOV2002_HPP
|
#define BOOST_OPTIONAL_OPTIONAL_FWD_FLC_19NOV2002_HPP
|
||||||
|
|
||||||
@ -16,6 +19,10 @@ namespace boost {
|
|||||||
|
|
||||||
template<class T> class optional ;
|
template<class T> class optional ;
|
||||||
|
|
||||||
|
template<class T> void swap ( optional<T>& , optional<T>& ) ;
|
||||||
|
|
||||||
|
template<class T> struct optional_swap_should_use_default_constructor ;
|
||||||
|
|
||||||
} // namespace boost
|
} // namespace boost
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
// fernando_cacciola@hotmail.com
|
// fernando_cacciola@hotmail.com
|
||||||
//
|
//
|
||||||
// Revisions:
|
// Revisions:
|
||||||
// 25 Apr 2008 (added more swap tests)
|
// 12 May 2008 (added more swap tests)
|
||||||
//
|
//
|
||||||
#include<iostream>
|
#include<iostream>
|
||||||
#include<stdexcept>
|
#include<stdexcept>
|
||||||
@ -1000,6 +1000,49 @@ namespace optional_swap_test
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// Class template whose default constructor should be used by optional::swap.
|
||||||
|
// Its copy constructor should be avoided!
|
||||||
|
//
|
||||||
|
template <class T>
|
||||||
|
class template_whose_default_ctor_should_be_used : public base_class_with_forbidden_assignment
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
T data;
|
||||||
|
explicit template_whose_default_ctor_should_be_used(T arg) : data(arg) { }
|
||||||
|
|
||||||
|
template_whose_default_ctor_should_be_used() : data('\0') { }
|
||||||
|
|
||||||
|
template_whose_default_ctor_should_be_used(const template_whose_default_ctor_should_be_used &)
|
||||||
|
{
|
||||||
|
BOOST_CHECK(!"This copy constructor should not be used while swapping!");
|
||||||
|
throw copy_ctor_exception();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// Class whose explicit constructor should be used by optional::swap.
|
||||||
|
// Its other constructors should be avoided!
|
||||||
|
//
|
||||||
|
class class_whose_explicit_ctor_should_be_used : public base_class_with_forbidden_assignment
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
char data;
|
||||||
|
explicit class_whose_explicit_ctor_should_be_used(char arg) : data(arg) { }
|
||||||
|
|
||||||
|
class_whose_explicit_ctor_should_be_used()
|
||||||
|
{
|
||||||
|
BOOST_CHECK(!"This default constructor should not be used while swapping!");
|
||||||
|
throw default_ctor_exception();
|
||||||
|
}
|
||||||
|
|
||||||
|
class_whose_explicit_ctor_should_be_used(const class_whose_explicit_ctor_should_be_used &)
|
||||||
|
{
|
||||||
|
BOOST_CHECK(!"This copy constructor should not be used while swapping!");
|
||||||
|
throw copy_ctor_exception();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void swap(class_whose_default_ctor_should_not_be_used & lhs, class_whose_default_ctor_should_not_be_used & rhs)
|
void swap(class_whose_default_ctor_should_not_be_used & lhs, class_whose_default_ctor_should_not_be_used & rhs)
|
||||||
{
|
{
|
||||||
std::swap(lhs.data, rhs.data);
|
std::swap(lhs.data, rhs.data);
|
||||||
@ -1015,20 +1058,107 @@ namespace optional_swap_test
|
|||||||
std::swap(lhs.data, rhs.data);
|
std::swap(lhs.data, rhs.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void swap(class_whose_explicit_ctor_should_be_used & lhs, class_whose_explicit_ctor_should_be_used & rhs)
|
||||||
|
{
|
||||||
|
std::swap(lhs.data, rhs.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void swap(template_whose_default_ctor_should_be_used<T> & lhs, template_whose_default_ctor_should_be_used<T> & rhs)
|
||||||
|
{
|
||||||
|
std::swap(lhs.data, rhs.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// optional<T>::swap should be customized when neither the copy constructor
|
||||||
|
// nor the default constructor of T are supposed to be used when swapping, e.g.,
|
||||||
|
// for the following type T = class_whose_explicit_ctor_should_be_used.
|
||||||
|
//
|
||||||
|
void swap(boost::optional<class_whose_explicit_ctor_should_be_used> & x, boost::optional<class_whose_explicit_ctor_should_be_used> & y)
|
||||||
|
{
|
||||||
|
bool hasX = x;
|
||||||
|
bool hasY = y;
|
||||||
|
|
||||||
|
if ( !hasX && !hasY )
|
||||||
|
return;
|
||||||
|
|
||||||
|
if( !hasX )
|
||||||
|
x = boost::in_place('\0');
|
||||||
|
else if ( !hasY )
|
||||||
|
y = boost::in_place('\0');
|
||||||
|
|
||||||
|
optional_swap_test::swap(*x,*y);
|
||||||
|
|
||||||
|
if( !hasX )
|
||||||
|
y = boost::none ;
|
||||||
|
else if( !hasY )
|
||||||
|
x = boost::none ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // End of namespace optional_swap_test.
|
} // End of namespace optional_swap_test.
|
||||||
|
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
|
|
||||||
//
|
//
|
||||||
// Compile time tweaking on whether or not swap should use the default constructor:
|
// Compile time tweaking on whether or not swap should use the default constructor:
|
||||||
//
|
//
|
||||||
template <> struct optional_swap_should_use_default_constructor<
|
|
||||||
::optional_swap_test::class_whose_default_ctor_should_be_used> : mpl::true_ {} ;
|
|
||||||
|
|
||||||
template <> struct optional_swap_should_use_default_constructor<
|
template <> struct optional_swap_should_use_default_constructor<
|
||||||
::optional_swap_test::class_whose_default_ctor_should_not_be_used> : mpl::false_ {} ;
|
optional_swap_test::class_whose_default_ctor_should_be_used> : mpl::true_ {} ;
|
||||||
|
|
||||||
|
template <> struct optional_swap_should_use_default_constructor<
|
||||||
|
optional_swap_test::class_whose_default_ctor_should_not_be_used> : mpl::false_ {} ;
|
||||||
|
|
||||||
|
template <class T> struct optional_swap_should_use_default_constructor<
|
||||||
|
optional_swap_test::template_whose_default_ctor_should_be_used<T> > : mpl::true_ {} ;
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// Specialization of boost::swap:
|
||||||
|
//
|
||||||
|
template <>
|
||||||
|
void swap(optional<optional_swap_test::class_whose_explicit_ctor_should_be_used> & x, optional<optional_swap_test::class_whose_explicit_ctor_should_be_used> & y)
|
||||||
|
{
|
||||||
|
optional_swap_test::swap(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace boost
|
||||||
|
|
||||||
|
|
||||||
|
namespace std {
|
||||||
|
|
||||||
|
//
|
||||||
|
// Specializations of std::swap:
|
||||||
|
//
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void swap(optional_swap_test::class_whose_default_ctor_should_be_used & x, optional_swap_test::class_whose_default_ctor_should_be_used & y)
|
||||||
|
{
|
||||||
|
optional_swap_test::swap(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void swap(optional_swap_test::class_whose_default_ctor_should_not_be_used & x, optional_swap_test::class_whose_default_ctor_should_not_be_used & y)
|
||||||
|
{
|
||||||
|
optional_swap_test::swap(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void swap(optional_swap_test::class_without_default_ctor & x, optional_swap_test::class_without_default_ctor & y)
|
||||||
|
{
|
||||||
|
optional_swap_test::swap(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void swap(optional_swap_test::class_whose_explicit_ctor_should_be_used & x, optional_swap_test::class_whose_explicit_ctor_should_be_used & y)
|
||||||
|
{
|
||||||
|
optional_swap_test::swap(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace std
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Tests whether the swap function works properly for optional<T>.
|
// Tests whether the swap function works properly for optional<T>.
|
||||||
@ -1044,16 +1174,25 @@ bool test_swap_function( T const* )
|
|||||||
optional<T> obj1;
|
optional<T> obj1;
|
||||||
optional<T> obj2('a');
|
optional<T> obj2('a');
|
||||||
|
|
||||||
// Test if obj1 and obj2 are properly initialized.
|
// Self-swap should not have any effect.
|
||||||
|
swap(obj1, obj1);
|
||||||
|
swap(obj2, obj2);
|
||||||
BOOST_CHECK(!obj1);
|
BOOST_CHECK(!obj1);
|
||||||
BOOST_CHECK(!!obj2 && obj2->data == 'a');
|
BOOST_CHECK(!!obj2 && obj2->data == 'a');
|
||||||
|
|
||||||
// Call non-member swap
|
// Call non-member swap.
|
||||||
swap(obj1, obj2);
|
swap(obj1, obj2);
|
||||||
|
|
||||||
// Test if obj1 and obj2 are really swapped
|
// Test if obj1 and obj2 are really swapped.
|
||||||
BOOST_CHECK(!!obj1 && obj1->data == 'a');
|
BOOST_CHECK(!!obj1 && obj1->data == 'a');
|
||||||
BOOST_CHECK(!obj2);
|
BOOST_CHECK(!obj2);
|
||||||
|
|
||||||
|
// Call non-member swap one more time.
|
||||||
|
swap(obj1, obj2);
|
||||||
|
|
||||||
|
// Test if obj1 and obj2 are swapped back.
|
||||||
|
BOOST_CHECK(!obj1);
|
||||||
|
BOOST_CHECK(!!obj2 && obj2->data == 'a');
|
||||||
}
|
}
|
||||||
catch(const std::exception &)
|
catch(const std::exception &)
|
||||||
{
|
{
|
||||||
@ -1077,16 +1216,25 @@ bool test_swap_member_function( T const* )
|
|||||||
optional<T> obj1;
|
optional<T> obj1;
|
||||||
optional<T> obj2('a');
|
optional<T> obj2('a');
|
||||||
|
|
||||||
// Test if obj1 and obj2 are properly initialized.
|
// Self-swap should not have any effect.
|
||||||
|
obj1.swap(obj1);
|
||||||
|
obj2.swap(obj2);
|
||||||
BOOST_CHECK(!obj1);
|
BOOST_CHECK(!obj1);
|
||||||
BOOST_CHECK(!!obj2 && obj2->data == 'a');
|
BOOST_CHECK(!!obj2 && obj2->data == 'a');
|
||||||
|
|
||||||
// Call member swap
|
// Call member swap.
|
||||||
obj1.swap(obj2);
|
obj1.swap(obj2);
|
||||||
|
|
||||||
// Test if obj1 and obj2 are really swapped
|
// Test if obj1 and obj2 are really swapped.
|
||||||
BOOST_CHECK(!!obj1 && obj1->data == 'a');
|
BOOST_CHECK(!!obj1 && obj1->data == 'a');
|
||||||
BOOST_CHECK(!obj2);
|
BOOST_CHECK(!obj2);
|
||||||
|
|
||||||
|
// Call member swap one more time.
|
||||||
|
obj1.swap(obj2);
|
||||||
|
|
||||||
|
// Test if obj1 and obj2 are swapped back.
|
||||||
|
BOOST_CHECK(!obj1);
|
||||||
|
BOOST_CHECK(!!obj2 && obj2->data == 'a');
|
||||||
}
|
}
|
||||||
catch(const std::exception &)
|
catch(const std::exception &)
|
||||||
{
|
{
|
||||||
@ -1106,9 +1254,13 @@ void test_swap_tweaking()
|
|||||||
BOOST_CHECK( test_swap_function( ARG(optional_swap_test::class_without_default_ctor) ) );
|
BOOST_CHECK( test_swap_function( ARG(optional_swap_test::class_without_default_ctor) ) );
|
||||||
BOOST_CHECK( test_swap_function( ARG(optional_swap_test::class_whose_default_ctor_should_be_used) ) );
|
BOOST_CHECK( test_swap_function( ARG(optional_swap_test::class_whose_default_ctor_should_be_used) ) );
|
||||||
BOOST_CHECK( test_swap_function( ARG(optional_swap_test::class_whose_default_ctor_should_not_be_used) ) );
|
BOOST_CHECK( test_swap_function( ARG(optional_swap_test::class_whose_default_ctor_should_not_be_used) ) );
|
||||||
|
BOOST_CHECK( test_swap_function( ARG(optional_swap_test::class_whose_explicit_ctor_should_be_used) ) );
|
||||||
|
BOOST_CHECK( test_swap_function( ARG(optional_swap_test::template_whose_default_ctor_should_be_used<char>) ) );
|
||||||
BOOST_CHECK( test_swap_member_function( ARG(optional_swap_test::class_without_default_ctor) ) );
|
BOOST_CHECK( test_swap_member_function( ARG(optional_swap_test::class_without_default_ctor) ) );
|
||||||
BOOST_CHECK( test_swap_member_function( ARG(optional_swap_test::class_whose_default_ctor_should_be_used) ) );
|
BOOST_CHECK( test_swap_member_function( ARG(optional_swap_test::class_whose_default_ctor_should_be_used) ) );
|
||||||
BOOST_CHECK( test_swap_member_function( ARG(optional_swap_test::class_whose_default_ctor_should_not_be_used) ) );
|
BOOST_CHECK( test_swap_member_function( ARG(optional_swap_test::class_whose_default_ctor_should_not_be_used) ) );
|
||||||
|
BOOST_CHECK( test_swap_member_function( ARG(optional_swap_test::class_whose_explicit_ctor_should_be_used) ) );
|
||||||
|
BOOST_CHECK( test_swap_member_function( ARG(optional_swap_test::template_whose_default_ctor_should_be_used<char>) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_main( int, char* [] )
|
int test_main( int, char* [] )
|
||||||
|
Reference in New Issue
Block a user