atomic_ref.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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-2025 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 <type_traits>
  16. #include <boost/assert.hpp>
  17. #include <boost/memory_order.hpp>
  18. #include <boost/atomic/capabilities.hpp>
  19. #include <boost/atomic/detail/config.hpp>
  20. #include <boost/atomic/detail/intptr.hpp>
  21. #include <boost/atomic/detail/classify.hpp>
  22. #include <boost/atomic/detail/atomic_ref_impl.hpp>
  23. #include <boost/atomic/detail/type_traits/is_trivially_copyable.hpp>
  24. #include <boost/atomic/detail/header.hpp>
  25. #ifdef BOOST_HAS_PRAGMA_ONCE
  26. #pragma once
  27. #endif
  28. namespace boost {
  29. namespace atomics {
  30. //! Atomic reference to external object
  31. template< typename T >
  32. class atomic_ref :
  33. public atomics::detail::base_atomic_ref< T, typename atomics::detail::classify< T >::type, false >
  34. {
  35. private:
  36. using base_type = atomics::detail::base_atomic_ref< T, typename atomics::detail::classify< T >::type, false >;
  37. using value_arg_type = typename base_type::value_arg_type;
  38. public:
  39. using value_type = typename base_type::value_type;
  40. static_assert(sizeof(value_type) > 0u, "boost::atomic_ref<T> requires T to be a complete type");
  41. static_assert(atomics::detail::is_trivially_copyable< value_type >::value, "boost::atomic_ref<T> requires T to be a trivially copyable type");
  42. private:
  43. using storage_type = typename base_type::storage_type;
  44. public:
  45. atomic_ref(atomic_ref const&) = default;
  46. BOOST_FORCEINLINE explicit atomic_ref(value_type& v) noexcept : base_type(v)
  47. {
  48. // Check that referenced object alignment satisfies required alignment
  49. BOOST_ASSERT((((atomics::detail::uintptr_t)this->m_value) & (base_type::required_alignment - 1u)) == 0u);
  50. }
  51. atomic_ref& operator= (atomic_ref const&) = delete;
  52. BOOST_FORCEINLINE value_type operator= (value_arg_type v) const noexcept
  53. {
  54. this->store(v);
  55. return v;
  56. }
  57. BOOST_FORCEINLINE operator value_type() const noexcept
  58. {
  59. return this->load();
  60. }
  61. };
  62. #if !defined(BOOST_NO_CXX17_DEDUCTION_GUIDES)
  63. template< typename T >
  64. atomic_ref(T&) -> atomic_ref< T >;
  65. #endif // !defined(BOOST_NO_CXX17_DEDUCTION_GUIDES)
  66. //! Atomic reference factory function
  67. template< typename T >
  68. BOOST_FORCEINLINE atomic_ref< T > make_atomic_ref(T& value) noexcept
  69. {
  70. return atomic_ref< T >(value);
  71. }
  72. } // namespace atomics
  73. using atomics::atomic_ref;
  74. using atomics::make_atomic_ref;
  75. } // namespace boost
  76. #include <boost/atomic/detail/footer.hpp>
  77. #endif // BOOST_ATOMIC_ATOMIC_REF_HPP_INCLUDED_