atomic_ref.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * Copyright (c) 2020-2021 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/atomic_ref.hpp
  10. *
  11. * This header contains definition of \c atomic_ref template.
  12. */
  13. #ifndef BOOST_ATOMIC_ATOMIC_REF_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_ATOMIC_REF_HPP_INCLUDED_
  15. #include <boost/assert.hpp>
  16. #include <boost/memory_order.hpp>
  17. #include <boost/atomic/capabilities.hpp>
  18. #include <boost/atomic/detail/config.hpp>
  19. #include <boost/atomic/detail/intptr.hpp>
  20. #include <boost/atomic/detail/classify.hpp>
  21. #include <boost/atomic/detail/atomic_ref_impl.hpp>
  22. #include <boost/atomic/detail/type_traits/is_trivially_copyable.hpp>
  23. #include <boost/atomic/detail/header.hpp>
  24. #ifdef BOOST_HAS_PRAGMA_ONCE
  25. #pragma once
  26. #endif
  27. namespace boost {
  28. namespace atomics {
  29. //! Atomic reference to external object
  30. template< typename T >
  31. class atomic_ref :
  32. public atomics::detail::base_atomic_ref< T, typename atomics::detail::classify< T >::type, false >
  33. {
  34. private:
  35. typedef atomics::detail::base_atomic_ref< T, typename atomics::detail::classify< T >::type, false > base_type;
  36. typedef typename base_type::value_arg_type value_arg_type;
  37. public:
  38. typedef typename base_type::value_type value_type;
  39. static_assert(sizeof(value_type) > 0u, "boost::atomic_ref<T> requires T to be a complete type");
  40. #if !defined(BOOST_ATOMIC_DETAIL_NO_CXX11_IS_TRIVIALLY_COPYABLE)
  41. static_assert(atomics::detail::is_trivially_copyable< value_type >::value, "boost::atomic_ref<T> requires T to be a trivially copyable type");
  42. #endif
  43. private:
  44. typedef typename base_type::storage_type storage_type;
  45. public:
  46. BOOST_DEFAULTED_FUNCTION(atomic_ref(atomic_ref const& that) BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_DECL, BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_IMPL : base_type(static_cast< base_type const& >(that)) {})
  47. BOOST_FORCEINLINE explicit atomic_ref(value_type& v) BOOST_NOEXCEPT : base_type(v)
  48. {
  49. // Check that referenced object alignment satisfies required alignment
  50. BOOST_ASSERT((((atomics::detail::uintptr_t)this->m_value) & (base_type::required_alignment - 1u)) == 0u);
  51. }
  52. BOOST_FORCEINLINE value_type operator= (value_arg_type v) const BOOST_NOEXCEPT
  53. {
  54. this->store(v);
  55. return v;
  56. }
  57. BOOST_FORCEINLINE operator value_type() const BOOST_NOEXCEPT
  58. {
  59. return this->load();
  60. }
  61. BOOST_DELETED_FUNCTION(atomic_ref& operator= (atomic_ref const&))
  62. };
  63. #if !defined(BOOST_NO_CXX17_DEDUCTION_GUIDES)
  64. template< typename T >
  65. atomic_ref(T&) -> atomic_ref< T >;
  66. #endif // !defined(BOOST_NO_CXX17_DEDUCTION_GUIDES)
  67. //! Atomic reference factory function
  68. template< typename T >
  69. BOOST_FORCEINLINE atomic_ref< T > make_atomic_ref(T& value) BOOST_NOEXCEPT
  70. {
  71. return atomic_ref< T >(value);
  72. }
  73. } // namespace atomics
  74. using atomics::atomic_ref;
  75. using atomics::make_atomic_ref;
  76. } // namespace boost
  77. #include <boost/atomic/detail/footer.hpp>
  78. #endif // BOOST_ATOMIC_ATOMIC_REF_HPP_INCLUDED_