Added BOOST_MOVE_RET

This commit is contained in:
Ion Gaztañaga
2014-02-15 23:04:15 +01:00
parent 51dc02e043
commit 062000ed68
7 changed files with 496 additions and 129 deletions
+16 -2
View File
@@ -13,8 +13,8 @@
#include <boost/move/detail/config_begin.hpp>
//[movable_definition
//header file "copy_movable.hpp"
//[copy_movable_definition
//header file "copymovable.hpp"
#include <boost/move/core.hpp>
//A copy_movable class
@@ -43,6 +43,20 @@ class copy_movable
{ return value_ == 0; }
};
//A copyable-only class
class copyable
{};
//A copyable-only class
class non_copy_movable
{
public:
non_copy_movable(){}
private:
non_copy_movable(const non_copy_movable&);
non_copy_movable& operator=(const non_copy_movable&);
};
//]
#include <boost/move/detail/config_end.hpp>
+70
View File
@@ -0,0 +1,70 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2014-2014.
// 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/detail/config_begin.hpp>
//[move_return_example
#include "movable.hpp"
#include "copymovable.hpp"
#include <boost/move/core.hpp>
template<class Type>
struct factory_functor
{
typedef Type return_type;
Type operator()() const
{ Type t; return BOOST_MOVE_RET(Type, t); }
};
struct return_reference
{
typedef non_copy_movable &return_type;
non_copy_movable &operator()() const
{ return ncm; }
static non_copy_movable ncm;
};
non_copy_movable return_reference::ncm;
//A wrapper that locks a mutex while the
//factory creates a new value.
//It must generically move the return value
//if possible both in C++03 and C++11
template <class Factory>
typename Factory::return_type lock_wrapper(Factory f)
{
typedef typename Factory::return_type return_type;
//LOCK();
return_type r = f();
//UNLOCK();
//In C++03: boost::move() if R is not a reference and
//has move emulation enabled. In C++11: just return r.
return BOOST_MOVE_RET(return_type, r);
}
int main()
{
movable m = lock_wrapper(factory_functor<movable> ());
copy_movable cm = lock_wrapper(factory_functor<copy_movable>());
copyable c = lock_wrapper(factory_functor<copyable> ());
non_copy_movable &mr = lock_wrapper(return_reference ());
//<-
(void)m; (void)cm; (void)c; (void)mr;
//->
return 0;
}
//]
#include <boost/move/detail/config_end.hpp>