copy_payload.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #ifndef BOOST_LOCKFREE_DETAIL_COPY_PAYLOAD_HPP_INCLUDED
  9. #define BOOST_LOCKFREE_DETAIL_COPY_PAYLOAD_HPP_INCLUDED
  10. #include <boost/mpl/if.hpp>
  11. #include <boost/type_traits/is_convertible.hpp>
  12. namespace boost {
  13. namespace lockfree {
  14. namespace detail {
  15. struct copy_convertible
  16. {
  17. template <typename T, typename U>
  18. static void copy(T & t, U & u)
  19. {
  20. u = t;
  21. }
  22. };
  23. struct copy_constructible_and_copyable
  24. {
  25. template <typename T, typename U>
  26. static void copy(T & t, U & u)
  27. {
  28. u = U(t);
  29. }
  30. };
  31. template <typename T, typename U>
  32. void copy_payload(T & t, U & u)
  33. {
  34. typedef typename boost::mpl::if_<typename boost::is_convertible<T, U>::type,
  35. copy_convertible,
  36. copy_constructible_and_copyable
  37. >::type copy_type;
  38. copy_type::copy(t, u);
  39. }
  40. template <typename T>
  41. struct consume_via_copy
  42. {
  43. consume_via_copy(T & out):
  44. out(out)
  45. {}
  46. template <typename U>
  47. void operator()(U & element)
  48. {
  49. copy_payload(element, out);
  50. }
  51. T & out;
  52. };
  53. }}}
  54. #endif /* BOOST_LOCKFREE_DETAIL_COPY_PAYLOAD_HPP_INCLUDED */