Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / histogram / accumulators / thread_safe.hpp
index 699bd4b..87ea416 100644 (file)
@@ -8,6 +8,7 @@
 #define BOOST_HISTOGRAM_ACCUMULATORS_THREAD_SAFE_HPP
 
 #include <atomic>
+#include <boost/core/nvp.hpp>
 #include <boost/mp11/utility.hpp>
 #include <type_traits>
 
@@ -30,6 +31,7 @@ namespace accumulators {
 template <class T>
 class thread_safe : public std::atomic<T> {
 public:
+  using value_type = T;
   using super_t = std::atomic<T>;
 
   thread_safe() noexcept : super_t(static_cast<T>(0)) {}
@@ -40,14 +42,31 @@ public:
     return *this;
   }
 
-  thread_safe(T arg) : super_t(arg) {}
-  thread_safe& operator=(T arg) {
+  thread_safe(value_type arg) : super_t(arg) {}
+  thread_safe& operator=(value_type arg) {
     super_t::store(arg);
     return *this;
   }
 
-  void operator+=(T arg) { super_t::fetch_add(arg, std::memory_order_relaxed); }
-  void operator++() { operator+=(static_cast<T>(1)); }
+  thread_safe& operator+=(const thread_safe& arg) {
+    operator+=(arg.load());
+    return *this;
+  }
+  thread_safe& operator+=(value_type arg) {
+    super_t::fetch_add(arg, std::memory_order_relaxed);
+    return *this;
+  }
+  thread_safe& operator++() {
+    operator+=(static_cast<value_type>(1));
+    return *this;
+  }
+
+  template <class Archive>
+  void serialize(Archive& ar, unsigned /* version */) {
+    auto value = super_t::load();
+    ar& make_nvp("value", value);
+    super_t::store(value);
+  }
 };
 
 } // namespace accumulators