copy_payload.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 <type_traits>
  11. #if defined( _MSC_VER )
  12. # pragma warning( push )
  13. # pragma warning( disable : 4512 ) // assignment operator could not be generated
  14. #endif
  15. namespace boost { namespace lockfree { namespace detail {
  16. struct copy_convertible
  17. {
  18. template < typename T, typename U >
  19. static void copy( T& t, U& u )
  20. {
  21. u = t;
  22. }
  23. };
  24. struct copy_constructible_and_copyable
  25. {
  26. template < typename T, typename U >
  27. static void copy( T& t, U& u )
  28. {
  29. u = U( t );
  30. }
  31. };
  32. template < typename T, typename U >
  33. void copy_payload( T& t, U& u )
  34. {
  35. static constexpr bool is_convertible = std::is_convertible< T, U >::value;
  36. typedef std::conditional_t< is_convertible, copy_convertible, copy_constructible_and_copyable > copy_type;
  37. copy_type::copy( t, u );
  38. }
  39. }}} // namespace boost::lockfree::detail
  40. #if defined( _MSC_VER )
  41. # pragma warning( pop )
  42. #endif
  43. #endif /* BOOST_LOCKFREE_DETAIL_COPY_PAYLOAD_HPP_INCLUDED */