FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
AtomicSharedPtr.h
1#pragma once
4
5#include <atomic>
6#include <memory>
7
8namespace marenv
9{
17template<typename T>
19{
20public:
22 [[nodiscard]] std::shared_ptr<T> load() const
23 {
24#if defined(__cpp_lib_atomic_shared_ptr)
25 return m_ptr.load();
26#else
27# pragma clang diagnostic push
28# pragma clang diagnostic ignored "-Wdeprecated-declarations"
29 return std::atomic_load(&m_ptr);
30# pragma clang diagnostic pop
31#endif
32 }
33
35 void store(std::shared_ptr<T> ptr)
36 {
37#if defined(__cpp_lib_atomic_shared_ptr)
38 m_ptr.store(std::move(ptr));
39#else
40# pragma clang diagnostic push
41# pragma clang diagnostic ignored "-Wdeprecated-declarations"
42 std::atomic_store(&m_ptr, std::move(ptr));
43# pragma clang diagnostic pop
44#endif
45 }
46
47private:
48#if defined(__cpp_lib_atomic_shared_ptr)
49 std::atomic<std::shared_ptr<T>> m_ptr;
50#else
51 std::shared_ptr<T> m_ptr;
52#endif
53};
54} // namespace marenv
Definition AtomicSharedPtr.h:19
void store(std::shared_ptr< T > ptr)
Publishes ptr; readers that loaded the previous one keep it alive.
Definition AtomicSharedPtr.h:35
std::shared_ptr< T > load() const
The current pointer; null before the first store.
Definition AtomicSharedPtr.h:22
The marenv root namespace.
Definition CastPath.h:36