Added to trunk

[SVN r70067]
This commit is contained in:
Ion Gaztañaga
2011-03-17 16:41:04 +00:00
parent 3362ca85e6
commit d4983b7afe
53 changed files with 5652 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
##############################################################################
##
## (C) Copyright Ion Gaztanaga 2008-2009 Distributed under the Boost
## Software License, Version 1.0. (See accompanying file
## LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
##
##
##############################################################################
rule test_all
{
local all_rules = ;
for local fileb in [ glob *.cpp ]
{
all_rules += [ run $(fileb)
: # additional args
: # test-files
: # requirements
] ;
}
return $(all_rules) ;
}
test-suite move_test : [ test_all r ]
: <link>static
;
+61
View File
@@ -0,0 +1,61 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright David Abrahams, Vicente Botet, Ion Gaztanaga 2009.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/move for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/move/move.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/list.hpp>
#include <boost/interprocess/containers/stable_vector.hpp>
#include "../example/movable.hpp"
template<class Container>
int move_test()
{
//Default construct 10 movable objects
Container v(10);
//Test default constructed value
if(v.begin()->moved()){
return 1;
}
//Move values
Container v2;
std::copy(v.begin(), v.end(), boost::back_move_inserter(v2));
//Test values have been moved
if(!v.begin()->moved()){
return 1;
}
if(v2.size() != 10){
return 1;
}
if(v2.begin()->moved()){
return 1;
}
return 0;
}
int main()
{
namespace bi = ::boost::interprocess;
if(move_test< bi::vector<movable> >()){
return 1;
}
if(move_test< bi::list<movable> >()){
return 1;
}
if(move_test< bi::stable_vector<movable> >()){
return 1;
}
return 0;
}
+115
View File
@@ -0,0 +1,115 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright David Abrahams, Vicente Botet, Ion Gaztanaga 2009-2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/move for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/move/move.hpp>
#include <boost/utility/enable_if.hpp>
#include "../example/movable.hpp"
#include "../example/copymovable.hpp"
#include <cstdio>
class non_movable
{
public:
non_movable()
{}
};
template<class MaybeRvalue>
void catch_test(BOOST_RV_REF(MaybeRvalue) x
#ifdef BOOST_NO_RVALUE_REFERENCES
,typename ::boost::enable_if< ::boost::has_move_emulation_enabled<MaybeRvalue> >::type* = 0
#endif //BOOST_NO_RVALUE_REFERENCES
)
{ (void)x;}
template<class MaybeRvalue>
void catch_test(BOOST_COPY_ASSIGN_REF(MaybeRvalue) x
#ifdef BOOST_NO_RVALUE_REFERENCES
,typename ::boost::enable_if< ::boost::has_move_emulation_enabled<MaybeRvalue> >::type* = 0
#endif //BOOST_NO_RVALUE_REFERENCES
)
{ (void)x;}
template<class MaybeRvalue>
void catch_test(MaybeRvalue &x
#ifdef BOOST_NO_RVALUE_REFERENCES
,typename ::boost::enable_if< ::boost::has_move_emulation_enabled<MaybeRvalue> >::type* = 0
#endif //BOOST_NO_RVALUE_REFERENCES
)
{ (void)x;}
#ifdef BOOST_NO_RVALUE_REFERENCES
template<class MaybeRvalue>
void catch_test(const MaybeRvalue& x
,typename ::boost::disable_if< ::boost::has_move_emulation_enabled<MaybeRvalue> >::type* = 0
)
{ (void)x;}
#endif //BOOST_NO_RVALUE_REFERENCES
movable create_movable()
{ return movable(); }
copy_movable create_copy_movable()
{ return copy_movable(); }
non_movable create_non_movable()
{ return non_movable(); }
void catch_test()
{
movable m;
const movable constm;
catch_test<movable>(boost::move(m));
#ifdef BOOST_CATCH_CONST_RLVALUE
catch_test<movable>(create_movable());
#endif
catch_test<movable>(m);
catch_test<movable>(constm);
copy_movable cm;
const copy_movable constcm;
catch_test<copy_movable>(boost::move(cm));
catch_test<copy_movable>(create_copy_movable());
catch_test<copy_movable>(cm);
catch_test<copy_movable>(constcm);
non_movable nm;
const non_movable constnm;
catch_test<non_movable>(boost::move(nm));
catch_test<non_movable>(create_non_movable());
catch_test<non_movable>(nm);
catch_test<non_movable>(constnm);
}
template<class MaybeMovableOnly, class MaybeRvalue>
void function_construct(BOOST_FWD_REF(MaybeRvalue) x)
{
//Moves in case Convertible is boost::rv<movable> copies otherwise
//For C++0x perfect forwarding
MaybeMovableOnly m(boost::forward<MaybeRvalue>(x));
}
void forward_test()
{
movable m;
function_construct<movable>(boost::move(m));
// non_movable nm;
// function_construct<non_movable>(boost::move(nm));
// const non_movable cnm;
// function_construct<non_movable>(cnm);
}
int main()
{
catch_test();
forward_test();
return 0;
}
+677
View File
@@ -0,0 +1,677 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright David Abrahams, Vicente Botet, Ion Gaztanaga 2010-2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/move for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/move/move.hpp>
#include <boost/type_traits/aligned_storage.hpp>
#include <boost/type_traits/is_class.hpp>
#include <cassert>
#include <boost/move/move_helpers.hpp>
enum ConstructionType { Copied, Moved, Other };
class conversion_source
{
public:
conversion_source(){}
operator int() const { return 0; }
};
class conversion_target
{
ConstructionType c_type_;
public:
conversion_target(conversion_source)
{ c_type_ = Other; }
conversion_target()
{ c_type_ = Other; }
conversion_target(const conversion_target &)
{ c_type_ = Copied; }
ConstructionType construction_type() const
{ return c_type_; }
};
class conversion_target_copymovable
{
ConstructionType c_type_;
BOOST_COPYABLE_AND_MOVABLE(conversion_target_copymovable)
public:
conversion_target_copymovable()
{ c_type_ = Other; }
conversion_target_copymovable(conversion_source)
{ c_type_ = Other; }
conversion_target_copymovable(const conversion_target_copymovable &)
{ c_type_ = Copied; }
conversion_target_copymovable(BOOST_RV_REF(conversion_target_copymovable) )
{ c_type_ = Moved; }
conversion_target_copymovable &operator=(BOOST_RV_REF(conversion_target_copymovable) )
{ c_type_ = Moved; return *this; }
conversion_target_copymovable &operator=(BOOST_COPY_ASSIGN_REF(conversion_target_copymovable) )
{ c_type_ = Copied; return *this; }
ConstructionType construction_type() const
{ return c_type_; }
};
class conversion_target_movable
{
ConstructionType c_type_;
BOOST_MOVABLE_BUT_NOT_COPYABLE(conversion_target_movable)
public:
conversion_target_movable()
{ c_type_ = Other; }
conversion_target_movable(conversion_source)
{ c_type_ = Other; }
conversion_target_movable(BOOST_RV_REF(conversion_target_movable) )
{ c_type_ = Moved; }
conversion_target_movable &operator=(BOOST_RV_REF(conversion_target_movable) )
{ c_type_ = Moved; return *this; }
ConstructionType construction_type() const
{ return c_type_; }
};
template<class T>
class container
{
typename ::boost::aligned_storage<sizeof(T), ::boost::alignment_of<T>::value>::type storage_;
public:
typedef T * iterator;
typedef const T * const_iterator;
BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator)
ConstructionType construction_type() const
{ return construction_type_impl(typename ::boost::is_class<T>::type()); }
ConstructionType construction_type_impl(::boost::true_type) const
{ return reinterpret_cast<const T&>(storage_).construction_type(); }
ConstructionType construction_type_impl(::boost::false_type) const
{ return Copied; }
iterator begin() { return iterator(0); }
private:
template<class U>
void priv_push_back(BOOST_MOVE_CATCH_FWD(U) x)
{ new (&storage_) T(::boost::forward<U>(x)); }
template<class U>
iterator priv_insert(const_iterator, BOOST_MOVE_CATCH_FWD(U) x)
{ new (&storage_) T(::boost::forward<U>(x)); return 0; }
};
int main()
{
conversion_target_movable a;
conversion_target_movable b(::boost::move(a));
{
container<conversion_target> c;
{
conversion_target x;
c.push_back(x);
assert(c.construction_type() == Copied);
c.insert(c.begin(), x);
assert(c.construction_type() == Copied);
}
{
const conversion_target x;
c.push_back(x);
assert(c.construction_type() == Copied);
c.insert(c.begin(), x);
assert(c.construction_type() == Copied);
}
{
c.push_back(conversion_target());
assert(c.construction_type() == Copied);
c.insert(c.begin(), conversion_target());
assert(c.construction_type() == Copied);
}
{
conversion_source x;
c.push_back(x);
assert(c.construction_type() == Copied);
c.insert(c.begin(), x);
assert(c.construction_type() == Copied);
}
{
const conversion_source x;
c.push_back(x);
assert(c.construction_type() == Copied);
c.insert(c.begin(), x);
assert(c.construction_type() == Copied);
}
{
c.push_back(conversion_source());
assert(c.construction_type() == Copied);
c.insert(c.begin(), conversion_source());
assert(c.construction_type() == Copied);
}
}
{
container<conversion_target_copymovable> c;
{
conversion_target_copymovable x;
c.push_back(x);
assert(c.construction_type() == Copied);
c.insert(c.begin(), x);
assert(c.construction_type() == Copied);
}
{
const conversion_target_copymovable x;
c.push_back(x);
assert(c.construction_type() == Copied);
c.insert(c.begin(), x);
assert(c.construction_type() == Copied);
}
{
c.push_back(conversion_target_copymovable());
assert(c.construction_type() == Moved);
c.insert(c.begin(), conversion_target_copymovable());
assert(c.construction_type() == Moved);
}
{
conversion_source x;
c.push_back(x);
assert(c.construction_type() == Moved);
c.insert(c.begin(), x);
assert(c.construction_type() == Moved);
}
{
const conversion_source x;
c.push_back(x);
assert(c.construction_type() == Moved);
c.insert(c.begin(), x);
assert(c.construction_type() == Moved);
}
{
c.push_back(conversion_source());
assert(c.construction_type() == Moved);
c.insert(c.begin(), conversion_source());
assert(c.construction_type() == Moved);
}
}
{
container<conversion_target_movable> c;
//This should not compile
//{
// conversion_target_movable x;
// c.push_back(x);
// assert(c.construction_type() == Copied);
//}
//{
// const conversion_target_movable x;
// c.push_back(x);
// assert(c.construction_type() == Copied);
//}
{
c.push_back(conversion_target_movable());
assert(c.construction_type() == Moved);
c.insert(c.begin(), conversion_target_movable());
assert(c.construction_type() == Moved);
}
{
conversion_source x;
c.push_back(x);
assert(c.construction_type() == Moved);
c.insert(c.begin(), x);
assert(c.construction_type() == Moved);
}
{
const conversion_source x;
c.push_back(x);
assert(c.construction_type() == Moved);
c.insert(c.begin(), x);
assert(c.construction_type() == Moved);
}
{
c.push_back(conversion_source());
assert(c.construction_type() == Moved);
c.insert(c.begin(), conversion_source());
assert(c.construction_type() == Moved);
}
}
{
container<int> c;
{
int x;
c.push_back(x);
assert(c.construction_type() == Copied);
c.insert(c.begin(), c.construction_type());
assert(c.construction_type() == Copied);
}
{
const int x = 0;
c.push_back(x);
assert(c.construction_type() == Copied);
c.insert(c.begin(), x);
assert(c.construction_type() == Copied);
}
{
c.push_back(int(0));
assert(c.construction_type() == Copied);
c.insert(c.begin(), int(0));
assert(c.construction_type() == Copied);
}
{
conversion_source x;
c.push_back(x);
assert(c.construction_type() == Copied);
c.insert(c.begin(), x);
assert(c.construction_type() == Copied);
}
{
const conversion_source x;
c.push_back(x);
assert(c.construction_type() == Copied);
c.insert(c.begin(), x);
assert(c.construction_type() == Copied);
}
{
c.push_back(conversion_source());
assert(c.construction_type() == Copied);
c.insert(c.begin(), conversion_source());
assert(c.construction_type() == Copied);
}
}
return 0;
}
/*
#include <boost/move/move.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/mpl/if.hpp>
#include <boost/type_traits/aligned_storage.hpp>
#include <boost/type_traits/is_class.hpp>
#include <cassert>
enum ConstructionType { Default, Copied, Moved, Other };
class conversion_source
{
public:
conversion_source(){}
operator int() const { return 0; }
};
class conversion_target
{
ConstructionType c_type_;
public:
conversion_target(conversion_source)
{ c_type_ = Other; }
conversion_target()
{ c_type_ = Default; }
conversion_target(const conversion_target &)
{ c_type_ = Copied; }
ConstructionType construction_type() const
{ return c_type_; }
};
class conversion_target_copymovable
{
ConstructionType c_type_;
BOOST_COPYABLE_AND_MOVABLE(conversion_target_copymovable)
public:
conversion_target_copymovable()
{ c_type_ = Default; }
conversion_target_copymovable(conversion_source)
{ c_type_ = Other; }
conversion_target_copymovable(const conversion_target_copymovable &)
{ c_type_ = Copied; }
conversion_target_copymovable(BOOST_RV_REF(conversion_target_copymovable) )
{ c_type_ = Moved; }
conversion_target_copymovable &operator=(BOOST_RV_REF(conversion_target_copymovable) )
{ c_type_ = Moved; return *this; }
conversion_target_copymovable &operator=(BOOST_COPY_ASSIGN_REF(conversion_target_copymovable) )
{ c_type_ = Copied; return *this; }
ConstructionType construction_type() const
{ return c_type_; }
};
class conversion_target_movable
{
ConstructionType c_type_;
BOOST_MOVABLE_BUT_NOT_COPYABLE(conversion_target_movable)
public:
conversion_target_movable()
{ c_type_ = Default; }
conversion_target_movable(conversion_source)
{ c_type_ = Other; }
conversion_target_movable(BOOST_RV_REF(conversion_target_movable) )
{ c_type_ = Moved; }
conversion_target_movable &operator=(BOOST_RV_REF(conversion_target_movable) )
{ c_type_ = Moved; return *this; }
ConstructionType construction_type() const
{ return c_type_; }
};
struct not_a_type;
#if defined(BOOST_NO_RVALUE_REFERENCES)
#define BOOST_MOVE_CATCH_CONST(U) \
typename ::boost::mpl::if_< ::boost::is_class<T>, BOOST_CATCH_CONST_RLVALUE(U), const U &>::type
#define BOOST_MOVE_CATCH_RVALUE(U)\
typename ::boost::mpl::if_< ::boost::is_class<T>, BOOST_RV_REF(T), not_a_type>::type
#else
#define BOOST_MOVE_CATCH_CONST(U) BOOST_CATCH_CONST_RLVALUE(U)
#define BOOST_MOVE_CATCH_RVALUE(U) BOOST_RV_REF(U)
#endif
// BEGIN JLH additional definitions...
template< class T > struct remove_const_remove_reference { typedef T type; };
template< class T > struct remove_const_remove_reference< const T > : remove_const_remove_reference<T> { };
template< class T > struct remove_const_remove_reference< volatile T > : remove_const_remove_reference<T> { };
template< class T > struct remove_const_remove_reference< const volatile T > : remove_const_remove_reference<T> { };
template< class T > struct remove_const_remove_reference< T& > : remove_const_remove_reference<T> { };
template< class T > struct remove_const_remove_reference< boost::rv<T> > : remove_const_remove_reference<T> { };
template< class T > struct add_reference_add_const { typedef T const & type; };
template< class T > struct add_reference_add_const< T& > { typedef T& type; };
template< class T, class U >
struct is_same_sans_const_sans_reference
: boost::is_same<
typename remove_const_remove_reference<T>::type,
typename remove_const_remove_reference<U>::type
>
{ };
template< class T > struct add_lvalue_reference { typedef T& type; };
template<> struct add_lvalue_reference< void > { typedef void type; };
template<> struct add_lvalue_reference< const void > { typedef const void type; };
template<> struct add_lvalue_reference< volatile void > { typedef volatile void type; };
template<> struct add_lvalue_reference< const volatile void > { typedef const volatile void type; };
template< class T > struct add_lvalue_reference< T& > { typedef T& type; };
template< class T > struct add_lvalue_reference< boost::rv<T> > { typedef T& type; };
template< class T > struct add_lvalue_reference< const boost::rv<T> > { typedef const T& type; };
template< class T > struct add_lvalue_reference< volatile boost::rv<T> > { typedef volatile T& type; };
template< class T > struct add_lvalue_reference< const volatile boost::rv<T> > { typedef const volatile T& type; };
template< class T > struct add_lvalue_reference< boost::rv<T>& > { typedef T& type; };
template< class T > struct add_lvalue_reference< const boost::rv<T>& > { typedef const T& type; };
template< class T > struct add_lvalue_reference< volatile boost::rv<T>& > { typedef volatile T& type; };
template< class T > struct add_lvalue_reference< const volatile boost::rv<T>& > { typedef const volatile T& type; };
template< class T > struct remove_rvalue_reference { typedef T type; };
template< class T > struct remove_rvalue_reference< boost::rv<T> > { typedef T type; };
template< class T > struct remove_rvalue_reference< const boost::rv<T> > { typedef T type; };
template< class T > struct remove_rvalue_reference< volatile boost::rv<T> > { typedef T type; };
template< class T > struct remove_rvalue_reference< const volatile boost::rv<T> > { typedef T type; };
template< class T > struct remove_rvalue_reference< boost::rv<T>& > { typedef T type; };
template< class T > struct remove_rvalue_reference< const boost::rv<T>& > { typedef T type; };
template< class T > struct remove_rvalue_reference< volatile boost::rv<T>& > { typedef T type; };
template< class T > struct remove_rvalue_reference< const volatile boost::rv<T>& > { typedef T type; };
template< class T >
struct add_rvalue_reference
: boost::mpl::if_<
boost::has_move_emulation_enabled<T>,
boost::rv<T>&,
T
>
{ };
template< class T >
struct remove_crv
: remove_rvalue_reference<T>
{ };
template< class T >
struct remove_crv< const T >
: remove_rvalue_reference<T>
{ };
template< class T >
inline typename add_lvalue_reference<T>::type
as_lvalue(T& x)
{ return x; }
// END JLH additional definitions...
template<class T>
class container
{
typename ::boost::aligned_storage<sizeof(T), ::boost::alignment_of<T>::value>::type storage_;
public:
ConstructionType construction_type() const
{ return construction_type_impl(typename ::boost::is_class<T>::type()); }
ConstructionType construction_type_impl(::boost::true_type) const
{ return reinterpret_cast<const T&>(storage_).construction_type(); }
ConstructionType construction_type_impl(::boost::false_type) const
{ return Copied; }
#if 0
// Ion's original implementation
void push_back(BOOST_MOVE_CATCH_CONST(T) x)
{ return priv_push_back(static_cast<const T&>(x)); }
void push_back(BOOST_MOVE_CATCH_RVALUE(T) x)
{ return priv_push_back(::boost::move(x)); }
//Tricks for C++03
#if defined(BOOST_NO_RVALUE_REFERENCES)
void push_back(T &x)
{ priv_push_back(const_cast<const T &>(x)); }
template<class U>
typename ::boost::enable_if_c
< ::boost::is_class<T>::value &&
::boost::is_same<T, U>::value &&
!::boost::has_move_emulation_enabled<U>::value
>::type
push_back(const U &u)
{ return priv_push_back(u); }
template<class U>
typename ::boost::enable_if_c
< ::boost::is_class<U>::value &&
!::boost::is_same<T, U>::value &&
!::boost::move_detail::is_rv<U>::value
>::type
push_back(const U &u)
{
T t(u);
priv_push_back(::boost::move(t));
}
#endif
private:
template<class U>
void priv_push_back(BOOST_FWD_REF(U) x)
{ new (&storage_) T(::boost::forward<U>(x)); }
#else // #if 0|1
// JLH's current implementation (roughly; only showing C++03 here)
template< class U >
typename boost::disable_if<
is_same_sans_const_sans_reference<U,T>
>::type
push_back(const U& x) { priv_push_back(as_lvalue(x)); }
template< class U >
void
push_back(U& x) { priv_push_back(x); }
typedef typename add_reference_add_const<
typename add_rvalue_reference<T>::type
>::type rv_param_type;
void
push_back(rv_param_type x) { priv_push_back(x); }
private:
template< class U >
void
priv_push_back(U& x) { new (&storage_) T(x); }
#endif // #if 0|1
};
int main()
{
conversion_target_movable a;
conversion_target_movable b(::boost::move(a));
{
container<conversion_target> c;
{
conversion_target x;
c.push_back(x);
assert(c.construction_type() == Copied);
}
{
const conversion_target x;
c.push_back(x);
assert(c.construction_type() == Copied);
}
{
c.push_back(conversion_target());
assert(c.construction_type() == Copied);
}
{
conversion_source x;
c.push_back(x);
//assert(c.construction_type() == Copied);
assert(c.construction_type() == Other);
}
{
const conversion_source x;
c.push_back(x);
//assert(c.construction_type() == Copied);
assert(c.construction_type() == Other);
}
{
c.push_back(conversion_source());
//assert(c.construction_type() == Copied);
assert(c.construction_type() == Other);
}
}
{
container<conversion_target_copymovable> c;
{
conversion_target_copymovable x;
c.push_back(x);
assert(c.construction_type() == Copied);
}
{
const conversion_target_copymovable x;
c.push_back(x);
assert(c.construction_type() == Copied);
}
{
c.push_back(conversion_target_copymovable());
assert(c.construction_type() == Moved);
}
{
conversion_source x;
c.push_back(x);
//assert(c.construction_type() == Moved);
assert(c.construction_type() == Other);
}
{
const conversion_source x;
c.push_back(x);
//assert(c.construction_type() == Moved);
assert(c.construction_type() == Other);
}
{
c.push_back(conversion_source());
//assert(c.construction_type() == Moved);
assert(c.construction_type() == Other);
}
}
{
container<conversion_target_movable> c;
//This should not compile
//{
// conversion_target_movable x;
// c.push_back(x);
// assert(c.construction_type() == Copied);
//}
//{
// const conversion_target_movable x;
// c.push_back(x);
// assert(c.construction_type() == Copied);
//}
{
c.push_back(conversion_target_movable());
assert(c.construction_type() == Moved);
}
{
conversion_source x;
c.push_back(x);
//assert(c.construction_type() == Moved);
assert(c.construction_type() == Other);
}
{
const conversion_source x;
c.push_back(x);
//assert(c.construction_type() == Moved);
assert(c.construction_type() == Other);
}
{
c.push_back(conversion_source());
//assert(c.construction_type() == Moved);
assert(c.construction_type() == Other);
}
}
{
container<int> c;
{
int x;
c.push_back(x);
assert(c.construction_type() == Copied);
}
{
const int x = 0;
c.push_back(x);
assert(c.construction_type() == Copied);
}
{
c.push_back(int(0));
assert(c.construction_type() == Copied);
}
{
conversion_source x;
c.push_back(x);
assert(c.construction_type() == Copied);
}
{
const conversion_source x;
c.push_back(x);
assert(c.construction_type() == Copied);
}
{
c.push_back(conversion_source());
assert(c.construction_type() == Copied);
}
}
return 0;
}
*/
+170
View File
@@ -0,0 +1,170 @@
// Copyright David Abrahams 2009. Distributed under the Boost
// Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <iostream>
#ifdef NO_MOVE
# undef BOOST_COPY_ASSIGN_REF
# define BOOST_COPY_ASSIGN_REF(X) X const&
# undef BOOST_COPYABLE_AND_MOVABLE
# define BOOST_COPYABLE_AND_MOVABLE(X)
# define MOVE(x) (x)
#else
#include <boost/move/move.hpp>
# define MOVE(x) boost::move(x)
#endif
struct X
{
X() : id(instances++)
{
std::cout << "X" << id << ": construct\n";
}
X(X const& rhs) : id(instances++)
{
std::cout << "X" << id << ": <- " << "X" << rhs.id << ": **copy**\n";
++copies;
}
// This particular test doesn't exercise assignment, but for
// completeness:
X& operator=(BOOST_COPY_ASSIGN_REF(X) rhs)
{
std::cout << "X" << id << ": <- " << "X" << rhs.id << ": assign\n";
return *this;
}
#ifndef NO_MOVE
X& operator=(BOOST_RV_REF(X) rhs)
{
std::cout << "X" << id << ": <- " << "X" << rhs.id << ": move assign\n";
return *this;
}
X(BOOST_RV_REF(X) rhs) : id(instances++)
{
std::cout << "X" << id << ": <- " << "X" << rhs.id << ": ..move construct..\n";
++copies;
}
#endif
~X() { std::cout << "X" << id << ": destroy\n"; }
unsigned id;
static unsigned copies;
static unsigned instances;
BOOST_COPYABLE_AND_MOVABLE(X)
};
unsigned X::copies = 0;
unsigned X::instances = 0;
#define CHECK_COPIES( stmt, min, max, comment ) \
{ \
unsigned const old_copies = X::copies; \
\
std::cout << "\n" comment "\n" #stmt "\n===========\n"; \
{ \
stmt; \
} \
unsigned const n = X::copies - old_copies; \
if (n > max) \
std::cout << "*** max is too low or compiler is buggy ***\n"; \
if (n < min) \
std::cout << "*** min is too high or compiler is buggy ***\n"; \
\
std::cout << "-----------\n" \
<< n << "/" << max \
<< " possible copies/moves made\n" \
<< max - n << "/" << max - min \
<< " possible elisions performed\n\n"; \
\
if (n > min) \
std::cout << "*** " << n - min \
<< " possible elisions missed! ***\n"; \
}
struct trace
{
trace(char const* name)
: name(name)
{
std::cout << "->: " << name << "\n";
}
~trace()
{
std::cout << "<-: " << name << "\n";
}
char const* name;
};
void sink(X a)
{
trace t("sink");
}
X nrvo_source()
{
trace t("nrvo_source");
X a;
return a;
}
X urvo_source()
{
trace t("urvo_source");
return X();
}
X identity(X a)
{
trace t("identity");
return a;
}
X lvalue_;
X& lvalue()
{
return lvalue_;
}
typedef X rvalue;
X ternary( bool y )
{
X a, b;
return MOVE(y?a:b);
}
int main(int argc, char* argv[])
{
(void)argv;
// Double parens prevent "most vexing parse"
CHECK_COPIES( X a(( lvalue() )), 1, 1, "Direct initialization from lvalue");
CHECK_COPIES( X a(( rvalue() )), 0, 1, "Direct initialization from rvalue");
CHECK_COPIES( X a = lvalue(), 1, 1, "Copy initialization from lvalue" );
CHECK_COPIES( X a = rvalue(), 0, 1, "Copy initialization from rvalue" );
CHECK_COPIES( sink( lvalue() ), 1, 1, "Pass lvalue by value" );
CHECK_COPIES( sink( rvalue() ), 0, 1, "Pass rvalue by value" );
CHECK_COPIES( nrvo_source(), 0, 1, "Named return value optimization (NRVO)" );
CHECK_COPIES( urvo_source(), 0, 1, "Unnamed return value optimization (URVO)" );
// Just to prove these things compose properly
CHECK_COPIES( X a(urvo_source()), 0, 2, "Return value used as ctor arg" );
// Expect to miss one possible elision here
CHECK_COPIES( identity( rvalue() ), 0, 2, "Return rvalue passed by value" );
// Expect to miss an elision in at least one of the following lines
CHECK_COPIES( X a = ternary( argc == 1000 ), 0, 2, "Return result of ternary operation" );
CHECK_COPIES( X a = ternary( argc != 1000 ), 0, 2, "Return result of ternary operation again" );
return 0;
}
+105
View File
@@ -0,0 +1,105 @@
//We need to declare:
//
//2 conversions: rv<T> & and const rv<T> &
//1 rv<T> & constructor: move constructor
//1 const rv<T> & constructor: copy constructor
//1 T & constructor: copy constructor
//
//Optimization:
//Since RVO is better than move-construction,
//avoid copy constructor overloading.
#include <boost/move/move.hpp>
#include <iostream>
bool moved = false;
class obj
{
BOOST_COPYABLE_AND_MOVABLE(obj)
public:
obj()
{
std::cout << "constructing obj" << "\n";
}
~obj()
{}
obj(const obj &)
{
std::cout << "copy construct from const obj" << "\n";
}
// copy construct from movable object (non-const rvalue, explicitly moved lvalue)
obj(BOOST_RV_REF(obj))
{
std::cout << "move construct from movable rvalue" << "\n";
}
obj& operator =(BOOST_COPY_ASSIGN_REF(obj))
{
std::cout << "copy assign from const obj" << "\n";
return *this;
}
obj& operator =(BOOST_RV_REF(obj))
{
std::cout << "move assign from movable rvalue" << "\n";
return *this;
}
};
obj rvalue_func() { return obj(); }
const obj const_rvalue_func() { return obj(); }
obj& lvalue_func() { static obj o; return o; }
const obj& const_lvalue_func() { static obj o; return o; }
obj produce() { return obj(); }
void consume(obj){}
int main()
{
{ consume(produce()); }
{ obj o = produce(); }
{ obj o(produce()); }
{
obj o1(rvalue_func());
obj o2 = const_rvalue_func();
obj o3 = lvalue_func();
obj o4 = const_lvalue_func();
// can't explicitly move temporaries
//obj o5 = boost::move(rvalue_func());
obj o5;
//Maybe missed optimization: copied
o5 = rvalue_func();
//Explicit forward works OK and optimized
o5 = boost::forward<obj>(rvalue_func());
obj o7 = boost::move(lvalue_func());
obj o8 = boost::move(const_lvalue_func());
obj o;
o = rvalue_func();
o = const_rvalue_func();
o = lvalue_func();
o = const_lvalue_func();
// can't explicitly move temporaries
//o = boost::move(rvalue_func());
o = boost::forward<obj>(rvalue_func());
o = boost::move(const_rvalue_func());
o = boost::move(lvalue_func());
o = boost::move(const_lvalue_func());
}
return 0;
}
//We need to declare:
//
//2 conversions: rv<T> & and const rv<T> &
//1 rv<T> & constructor: move constructor
//1 const rv<T> & constructor: copy constructor
//1 T & constructor: copy constructor
+114
View File
@@ -0,0 +1,114 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright David Abrahams, Vicente Botet, Ion Gaztanaga 2009.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/move for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/move/move.hpp>
#include "../example/movable.hpp"
#include <boost/static_assert.hpp>
movable function(movable m)
{
return movable(boost::move(m));
}
movable functionr(BOOST_RV_REF(movable) m)
{
return movable(boost::move(m));
}
movable function2(movable m)
{
return boost::move(m);
}
BOOST_RV_REF(movable) function2r(BOOST_RV_REF(movable) m)
{
return boost::move(m);
}
movable move_return_function2 ()
{
return movable();
}
movable move_return_function ()
{
movable m;
return (boost::move(m));
}
//Catch by value
void function_value(movable)
{}
//Catch by reference
void function_ref(const movable &)
{}
//Catch by reference
void function_ref(BOOST_RV_REF(movable))
{}
struct copyable
{};
movable create_movable()
{ return movable(); }
int main()
{
#if defined(BOOST_NO_RVALUE_REFERENCES)
BOOST_STATIC_ASSERT((boost::has_nothrow_move<movable>::value == true));
BOOST_STATIC_ASSERT((boost::has_nothrow_move<copyable>::value == false));
BOOST_STATIC_ASSERT((boost::has_move_emulation_enabled<copyable>::value == false));
BOOST_STATIC_ASSERT((boost::has_move_emulation_enabled<copyable*>::value == false));
BOOST_STATIC_ASSERT((boost::has_move_emulation_enabled<int>::value == false));
BOOST_STATIC_ASSERT((boost::has_move_emulation_enabled<int&>::value == false));
BOOST_STATIC_ASSERT((boost::has_move_emulation_enabled<int*>::value == false));
#endif
{
movable m;
movable m2(boost::move(m));
movable m3(function(movable(boost::move(m2))));
movable m4(function(boost::move(m3)));
}
{
movable m;
movable m2(boost::move(m));
movable m3(functionr(movable(boost::move(m2))));
movable m4(functionr(boost::move(m3)));
}
{
movable m;
movable m2(boost::move(m));
movable m3(function2(movable(boost::move(m2))));
movable m4(function2(boost::move(m3)));
}
{
movable m;
movable m2(boost::move(m));
movable m3(function2r(movable(boost::move(m2))));
movable m4(function2r(boost::move(m3)));
}
{
movable m;
movable m2(boost::move(m));
movable m3(move_return_function());
}
{
movable m;
movable m2(boost::move(m));
movable m3(move_return_function2());
}
//limitations_test();
return 0;
}
+55
View File
@@ -0,0 +1,55 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright David Abrahams, Vicente Botet, Ion Gaztanaga 2009.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/move for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/move/move.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include "../example/movable.hpp"
int main()
{
namespace bi = ::boost::interprocess;
//Default construct 10 movable objects
bi::vector<movable> v(10);
bi::vector<movable> v2(10);
//Move to v2
boost::move(v.begin(), v.end(), v2.begin());
//Test values have been moved
if(!v[0].moved()){
return 1;
}
if(v2.size() != 10){
return 1;
}
if(v2[0].moved()){
return 1;
}
//Move to v again
boost::move_backward(v2.begin(), v2.end(), v.end());
//Test values have been moved
if(!v2[0].moved()){
return 1;
}
if(v.size() != 10){
return 1;
}
if(v[0].moved()){
return 1;
}
return 0;
}
+104
View File
@@ -0,0 +1,104 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright David Abrahams, Vicente Botet, Ion Gaztanaga 2009.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/move for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/move/move.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include "../example/movable.hpp"
int main()
{
namespace bi = ::boost::interprocess;
//Default construct 10 movable objects
bi::vector<movable> v(10);
//Test default constructed value
if(v[0].moved()){
return 1;
}
//Move values
bi::vector<movable> v2
(boost::make_move_iterator(v.begin()), boost::make_move_iterator(v.end()));
//Test values have been moved
if(!v[0].moved()){
return 1;
}
if(v2.size() != 10){
return 1;
}
//Move again
v.assign(boost::make_move_iterator(v2.begin()), boost::make_move_iterator(v2.end()));
//Test values have been moved
if(!v2[0].moved()){
return 1;
}
if(v[0].moved()){
return 1;
}
return 0;
}
/*
#include <boost/move/move.hpp>
class copy_movable
{
BOOST_COPYABLE_AND_MOVABLE(copy_movable)
int value_;
public:
copy_movable() : value_(1){}
//Move constructor and assignment
copy_movable(BOOST_RV_REF(copy_movable) m)
{ value_ = m.value_; m.value_ = 0; }
copy_movable(const copy_movable &m)
{ value_ = m.value_; }
copy_movable & operator=(BOOST_RV_REF(copy_movable) m)
{ value_ = m.value_; m.value_ = 0; return *this; }
copy_movable & operator=(BOOST_COPY_ASSIGN_REF(copy_movable) m)
{ value_ = m.value_; return *this; }
bool moved() const //Observer
{ return value_ == 0; }
};
struct copy_movable_wrapper
{
copy_movable cm;
};
copy_movable produce()
{ return copy_movable(); }
int main()
{
copy_movable cm;
cm = produce();
const copy_movable_wrapper cmw;
copy_movable_wrapper cmw2;
cmw2 = cmw;
return 0;
}
*/