Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / lockfree / detail / copy_payload.hpp
1 //  boost lockfree: copy_payload helper
2 //
3 //  Copyright (C) 2011 Tim Blechmann
4 //
5 //  Distributed under the Boost Software License, Version 1.0. (See
6 //  accompanying file LICENSE_1_0.txt or copy at
7 //  http://www.boost.org/LICENSE_1_0.txt)
8
9 #ifndef BOOST_LOCKFREE_DETAIL_COPY_PAYLOAD_HPP_INCLUDED
10 #define BOOST_LOCKFREE_DETAIL_COPY_PAYLOAD_HPP_INCLUDED
11
12 #include <boost/mpl/if.hpp>
13 #include <boost/type_traits/is_convertible.hpp>
14
15 namespace boost    {
16 namespace lockfree {
17 namespace detail   {
18
19 struct copy_convertible
20 {
21     template <typename T, typename U>
22     static void copy(T & t, U & u)
23     {
24         u = t;
25     }
26 };
27
28 struct copy_constructible_and_copyable
29 {
30     template <typename T, typename U>
31     static void copy(T & t, U & u)
32     {
33         u = U(t);
34     }
35 };
36
37 template <typename T, typename U>
38 void copy_payload(T & t, U & u)
39 {
40     typedef typename boost::mpl::if_<typename boost::is_convertible<T, U>::type,
41                                      copy_convertible,
42                                      copy_constructible_and_copyable
43                                     >::type copy_type;
44     copy_type::copy(t, u);
45 }
46
47 template <typename T>
48 struct consume_via_copy
49 {
50     consume_via_copy(T & out):
51         out_(out)
52     {}
53
54     template <typename U>
55     void operator()(U & element)
56     {
57         copy_payload(element, out_);
58     }
59
60     T & out_;
61 };
62
63 struct consume_noop
64 {
65     template <typename U>
66     void operator()(const U &)
67     {
68     }
69 };
70
71
72 }}}
73
74 #endif  /* BOOST_LOCKFREE_DETAIL_COPY_PAYLOAD_HPP_INCLUDED */