copy_payload.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // boost lockfree: copy_payload helper
  2. //
  3. // Copyright (C) 2011, 2016 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. #if defined(_MSC_VER)
  13. #pragma warning(push)
  14. #pragma warning(disable: 4512) // assignment operator could not be generated
  15. #endif
  16. namespace boost {
  17. namespace lockfree {
  18. namespace detail {
  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. struct copy_constructible_and_copyable
  28. {
  29. template <typename T, typename U>
  30. static void copy(T & t, U & u)
  31. {
  32. u = U(t);
  33. }
  34. };
  35. template <typename T, typename U>
  36. void copy_payload(T & t, U & u)
  37. {
  38. typedef typename boost::mpl::if_<typename boost::is_convertible<T, U>::type,
  39. copy_convertible,
  40. copy_constructible_and_copyable
  41. >::type copy_type;
  42. copy_type::copy(t, u);
  43. }
  44. template <typename T>
  45. struct consume_via_copy
  46. {
  47. consume_via_copy(T & out):
  48. out_(out)
  49. {}
  50. template <typename U>
  51. void operator()(U & element)
  52. {
  53. copy_payload(element, out_);
  54. }
  55. T & out_;
  56. };
  57. struct consume_noop
  58. {
  59. template <typename U>
  60. void operator()(const U &)
  61. {
  62. }
  63. };
  64. }}}
  65. #if defined(_MSC_VER)
  66. #pragma warning(pop)
  67. #endif
  68. #endif /* BOOST_LOCKFREE_DETAIL_COPY_PAYLOAD_HPP_INCLUDED */