ipc_atomic_ref.hpp 3.2 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-2021 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/ipc_atomic_ref.hpp
  10. *
  11. * This header contains definition of \c ipc_atomic_ref template.
  12. */
  13. #ifndef BOOST_ATOMIC_IPC_ATOMIC_REF_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_IPC_ATOMIC_REF_HPP_INCLUDED_
  15. #include <boost/assert.hpp>
  16. #include <boost/static_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 for inter-process communication
  31. template< typename T >
  32. class ipc_atomic_ref :
  33. public atomics::detail::base_atomic_ref< T, typename atomics::detail::classify< T >::type, true >
  34. {
  35. private:
  36. typedef atomics::detail::base_atomic_ref< T, typename atomics::detail::classify< T >::type, true > base_type;
  37. typedef typename base_type::value_arg_type value_arg_type;
  38. public:
  39. typedef typename base_type::value_type value_type;
  40. BOOST_STATIC_ASSERT_MSG(sizeof(value_type) > 0u, "boost::ipc_atomic_ref<T> requires T to be a complete type");
  41. #if !defined(BOOST_ATOMIC_DETAIL_NO_CXX11_IS_TRIVIALLY_COPYABLE)
  42. BOOST_STATIC_ASSERT_MSG(atomics::detail::is_trivially_copyable< value_type >::value, "boost::ipc_atomic_ref<T> requires T to be a trivially copyable type");
  43. #endif
  44. private:
  45. typedef typename base_type::storage_type storage_type;
  46. public:
  47. BOOST_DEFAULTED_FUNCTION(ipc_atomic_ref(ipc_atomic_ref const& that) BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_DECL, BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_IMPL : base_type(static_cast< base_type const& >(that)) {})
  48. BOOST_FORCEINLINE explicit ipc_atomic_ref(value_type& v) BOOST_NOEXCEPT : base_type(v)
  49. {
  50. // Check that referenced object alignment satisfies required alignment
  51. BOOST_ASSERT((((atomics::detail::uintptr_t)this->m_value) & (base_type::required_alignment - 1u)) == 0u);
  52. }
  53. BOOST_FORCEINLINE value_type operator= (value_arg_type v) const BOOST_NOEXCEPT
  54. {
  55. this->store(v);
  56. return v;
  57. }
  58. BOOST_FORCEINLINE operator value_type() const BOOST_NOEXCEPT
  59. {
  60. return this->load();
  61. }
  62. BOOST_DELETED_FUNCTION(ipc_atomic_ref& operator= (ipc_atomic_ref const&))
  63. };
  64. #if !defined(BOOST_NO_CXX17_DEDUCTION_GUIDES)
  65. template< typename T >
  66. ipc_atomic_ref(T&) -> ipc_atomic_ref< T >;
  67. #endif // !defined(BOOST_NO_CXX17_DEDUCTION_GUIDES)
  68. //! IPC atomic reference factory function
  69. template< typename T >
  70. BOOST_FORCEINLINE ipc_atomic_ref< T > make_ipc_atomic_ref(T& value) BOOST_NOEXCEPT
  71. {
  72. return ipc_atomic_ref< T >(value);
  73. }
  74. } // namespace atomics
  75. using atomics::ipc_atomic_ref;
  76. using atomics::make_ipc_atomic_ref;
  77. } // namespace boost
  78. #include <boost/atomic/detail/footer.hpp>
  79. #endif // BOOST_ATOMIC_IPC_ATOMIC_REF_HPP_INCLUDED_