diff --git a/src/util/TypeMap.hpp b/src/util/TypeMap.hpp index fb469685..d8c76d1f 100644 --- a/src/util/TypeMap.hpp +++ b/src/util/TypeMap.hpp @@ -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> data; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) +#else static std::shared_ptr data; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) +#endif public: /** @@ -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 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 } /** @@ -77,14 +85,21 @@ namespace util { * @return A shared_ptr to the data that was previously stored */ static std::shared_ptr 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 - std::shared_ptr - TypeMap::data; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) +#if __cplusplus >= 202002L + std::atomic> TypeMap::data; +#else + std::shared_ptr TypeMap::data; +#endif } // namespace util } // namespace NUClear