gramods
Loading...
Searching...
No Matches
SyncSData.hh
1
2#ifndef GRAMODS_NETWORK_SYNCSDATA
3#define GRAMODS_NETWORK_SYNCSDATA
4
5#include <gmNetwork/SyncData.hh>
6#include <gmCore/InvalidArgument.hh>
7
8BEGIN_NAMESPACE_GMNETWORK;
9
14template<class TYPE>
16 : public SyncData {
17
18public:
19
23 SyncSData() { back = front = {}; }
24
28 SyncSData(TYPE val) { back = front = val; }
29
31 back = s.back;
32 front = s.front;
33 }
34
38 operator TYPE () const {
39 TYPE retval;
40 {
41 std::lock_guard<std::mutex> guard(const_cast<SyncSData<TYPE>*>(this)->lock);
42 retval = front;
43 }
44 return retval;
45 }
46
50 TYPE operator*() const {
51 TYPE retval;
52 {
53 std::lock_guard<std::mutex> guard(const_cast<SyncSData<TYPE>*>(this)->lock);
54 retval = front;
55 }
56 return retval;
57 }
58
63 SyncSData<TYPE>& operator= (TYPE val) {
64 {
65 std::lock_guard<std::mutex> guard(lock);
66 back = val;
67 }
68 SyncData::pushValue();
69 return *this;
70 }
71
72protected:
73
78 void encode(std::vector<char> &d) override {
79 std::lock_guard<std::mutex> guard(lock);
80
81 d.resize(1 + sizeof(TYPE));
82
83 char * m = reinterpret_cast<char*>(&back);
84
85 for (size_t idx = 0; idx < sizeof(TYPE); ++idx) {
86 d[idx + 1] = m[idx];
87 }
88 }
89
94 void decode(std::vector<char> d) override {
95 std::lock_guard<std::mutex> guard(lock);
96
97 if (d.size() != 1 + sizeof(TYPE))
98 throw gmCore::InvalidArgument("incorrect data size for decoding");
99
100 TYPE value;
101 char * m = reinterpret_cast<char*>(&value);
102
103 for (size_t idx = 0; idx < sizeof(TYPE); ++idx)
104 m[idx] = d[idx + 1];
105
106 back = value;
107 }
108
112 void update() override {
113 std::lock_guard<std::mutex> guard(lock);
114 front = back;
115 }
116
117private:
118
122 TYPE front;
123
127 TYPE back;
128
129 std::mutex lock;
130
131};
132
133typedef SyncSData<bool> SyncSBool;
134typedef SyncSData<int32_t> SyncSInt32;
135typedef SyncSData<uint32_t> SyncSUInt32;
136typedef SyncSData<int64_t> SyncSInt64;
137typedef SyncSData<uint64_t> SyncSUInt64;
138typedef SyncSData<float> SyncSFloat32;
139typedef SyncSData<double> SyncSFloat64;
140
141END_NAMESPACE_GMNETWORK;
142
143#endif
Simple synchronizeable data container.
Definition SyncData.hh:17
Simple, single value synchronizeable data container, without support for pointers or types containing...
Definition SyncSData.hh:16
void encode(std::vector< char > &d) override
Encodes the back data of the container into the specified vector, using vector indices 1-N and leavin...
Definition SyncSData.hh:78
TYPE operator*() const
Retrieves the front value of the container.
Definition SyncSData.hh:50
void decode(std::vector< char > d) override
Decodes the specified vector into the back data of the container, using vector indices 1-N.
Definition SyncSData.hh:94
SyncSData()
Initializes without specifying value.
Definition SyncSData.hh:23
SyncSData(TYPE val)
Initializes the SyncSData to the specified value.
Definition SyncSData.hh:28
void update() override
Copies the back value to the front.
Definition SyncSData.hh:112
Standard exception for invalid arguments in a call to a function or object.
Definition InvalidArgument.hh:15