SST 16.0.0
Structural Simulation Toolkit
serialize_atomic.h
1// Copyright 2009-2026 NTESS. Under the terms
2// of Contract DE-NA0003525 with NTESS, the U.S.
3// Government retains certain rights in this software.
4//
5// Copyright (c) 2009-2026, NTESS
6// All rights reserved.
7//
8// This file is part of the SST software package. For license
9// information, see the LICENSE file in the top level directory of the
10// distribution.
11
12#ifndef SST_CORE_SERIALIZATION_IMPL_SERIALIZE_ATOMIC_H
13#define SST_CORE_SERIALIZATION_IMPL_SERIALIZE_ATOMIC_H
14
15#ifndef SST_INCLUDING_SERIALIZE_H
16#warning \
17 "The header file sst/core/serialization/impl/serialize_atomic.h should not be directly included as it is not part of the stable public API. The file is included in sst/core/serialization/serialize.h"
18#endif
19
20#include "sst/core/serialization/serializer.h"
21
22#include <atomic>
23
24namespace SST::Core::Serialization {
25
26template <class T>
27class serialize_impl<std::atomic<T>>
28{
29 // Proxy class which represents a reference to std::atomic<T>, is copyable, convertible to T, and assignable from T
30 //
31 // This is only used in mapping mode
32 class atomic_reference
33 {
34 std::atomic<T>& ref;
35
36 public:
37 explicit atomic_reference(std::atomic<T>& ref) :
38 ref(ref)
39 {}
40
41 // Set the referenced atomic to a value
42 atomic_reference& operator=(const T& value)
43 {
44 ref.store(value);
45 return *this;
46 }
47
48 // Convert the referenced atomic to its value
49 operator T() const { return ref.load(); }
50 };
51
52 void operator()(std::atomic<T>& v, serializer& ser, ser_opt_t UNUSED(options))
53 {
54 switch ( ser.mode() ) {
55 case serializer::SIZER:
56 case serializer::PACK:
57 {
58 T value { v.load() };
59 SST_SER(value);
60 break;
61 }
62 case serializer::UNPACK:
63 {
64 T value {};
65 SST_SER(value);
66 v.store(value);
67 break;
68 }
69 case serializer::MAP:
70 {
71 // Create an ObjectMapFundamentalReference referring to a atomic_reference proxy wrapper class
72 ser.mapper().map_hierarchy_start(ser.getMapName(),
73 new ObjectMapFundamentalReference<T, atomic_reference, std::atomic<T>>(atomic_reference(v)));
74 ser.mapper().map_hierarchy_end();
75 break;
76 }
77 }
78 }
79
80 SST_FRIEND_SERIALIZE();
81};
82
83} // namespace SST::Core::Serialization
84
85#endif // SST_CORE_SERIALIZATION_IMPL_SERIALIZE_VECTOR_H
Base serialize class.
Definition serialize.h:132
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:43