Skip to content

Commit

Permalink
Fix some issues that occur when compiling with c++17 or c++20 (#149)
Browse files Browse the repository at this point in the history
- The `noexcept` specifier on an std::function will not compile in c++17
or c++20
- In c++20 is_pod is deprecated
- In c++20 the separate functions for atomic shared pointers are
deprecated
  • Loading branch information
TrentHouliston authored Sep 2, 2024
1 parent 8cf2120 commit e01abfa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/dsl/word/UDP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ namespace dsl {
// We can cast ourselves to a reference type so long as
// that reference type is plain old data
template <typename T>
operator std::enable_if_t<std::is_pod<T>::value, const T&>() {
operator std::enable_if_t<std::is_trivially_copyable<T>::value, const T&>() {
return *reinterpret_cast<const T*>(payload.data());
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/threading/ReactionTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace threading {

public:
/// Type of the functions that ReactionTasks execute
using TaskFunction = std::function<void(ReactionTask&) noexcept>;
using TaskFunction = std::function<void(ReactionTask&)>;

/**
* Gets the current executing task, or nullptr if there isn't one.
Expand Down
23 changes: 20 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,23 @@ 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
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::atomic<std::shared_ptr<Value>> TypeMap<MapID, Key, Value>::data;
#else
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::shared_ptr<Value> TypeMap<MapID, Key, Value>::data;
#endif

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

0 comments on commit e01abfa

Please sign in to comment.