Skip to content

Commit

Permalink
the shared_ptr atomic_store functions are deprecated in c++20, this u…
Browse files Browse the repository at this point in the history
…ses the atomic partial specialisation instead
  • Loading branch information
TrentHouliston committed Sep 2, 2024
1 parent 1c187ae commit 0d6d120
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/util/TypeMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ namespace util {
TypeMap operator=(TypeMap&& /*other*/) noexcept = delete;

private:
/// The data variable where the data is stored for this map key.
/// The data variable where the data is stored for this map key.
#if __cplusplus >= 202002L
static std::atomic<std::shared_ptr<Value>> data; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
#else
static std::shared_ptr<Value> data; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
#endif

public:
/**
Expand All @@ -68,7 +72,11 @@ namespace util {
* @param d A pointer to the data to be stored (the map takes ownership)
*/
static void set(std::shared_ptr<Value> d) {
#if __cplusplus >= 202002L
data.store(std::move(d), std::memory_order_release);
#else
std::atomic_store_explicit(&data, std::move(d), std::memory_order_release);
#endif
}

/**
Expand All @@ -77,14 +85,21 @@ namespace util {
* @return A shared_ptr to the data that was previously stored
*/
static std::shared_ptr<Value> get() {
#if __cplusplus >= 202002L
return data.load(std::memory_order_acquire);
#else
return std::atomic_load_explicit(&data, std::memory_order_acquire);
#endif
}
};

/// Initialize our shared_ptr data
template <typename MapID, typename Key, typename Value>
std::shared_ptr<Value>
TypeMap<MapID, Key, Value>::data; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
#if __cplusplus >= 202002L
std::atomic<std::shared_ptr<Value>> TypeMap<MapID, Key, Value>::data;
#else
std::shared_ptr<Value> TypeMap<MapID, Key, Value>::data;
#endif

} // namespace util
} // namespace NUClear
Expand Down

0 comments on commit 0d6d120

Please sign in to comment.