Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / move / example / doc_move_return.cpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2014-2014.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // See http://www.boost.org/libs/move for documentation.
9 //
10 //////////////////////////////////////////////////////////////////////////////
11
12 #include <boost/move/detail/config_begin.hpp>
13
14 //[move_return_example
15 #include "movable.hpp"
16 #include "copymovable.hpp"
17 #include <boost/move/core.hpp>
18
19 template<class Type>
20 struct factory_functor
21 {
22    typedef Type return_type;
23
24    Type operator()() const
25    {  Type t;  return BOOST_MOVE_RET(Type, t);  }
26 };
27
28 struct return_reference
29 {
30    typedef non_copy_movable &return_type;
31
32    non_copy_movable &operator()() const
33    {  return ncm; }
34
35    static non_copy_movable ncm;
36 };
37
38 non_copy_movable return_reference::ncm;
39
40 //A wrapper that locks a mutex while the
41 //factory creates a new value.
42 //It must generically move the return value
43 //if possible both in C++03 and C++11
44 template <class Factory>
45 typename Factory::return_type lock_wrapper(Factory f)
46 {
47    typedef typename Factory::return_type return_type;
48    //LOCK();
49    return_type r = f();
50    //UNLOCK();
51
52    //In C++03: boost::move() if R is not a reference and
53    //has move emulation enabled. In C++11: just return r.
54    return BOOST_MOVE_RET(return_type, r);
55 }
56
57 int main()
58 {
59    movable m            = lock_wrapper(factory_functor<movable>     ());
60    copy_movable cm      = lock_wrapper(factory_functor<copy_movable>());
61    copyable c           = lock_wrapper(factory_functor<copyable>    ());
62    non_copy_movable &mr = lock_wrapper(return_reference             ());
63    //<-
64    (void)m; (void)cm; (void)c; (void)mr;
65    //->
66    return 0;
67 }
68 //]
69
70 #include <boost/move/detail/config_end.hpp>