gramods
SyncMData.hh
1 
2 #ifndef GRAMODS_NETWORK_SYNCMDATA
3 #define GRAMODS_NETWORK_SYNCMDATA
4 
5 #include <gmNetwork/SyncData.hh>
6 #include <gmCore/InvalidArgument.hh>
7 
8 BEGIN_NAMESPACE_GMNETWORK;
9 
17 template<class TYPE>
18 class SyncMData
19  : public SyncData {
20 
21 public:
22 
26  SyncMData() {}
27 
31  SyncMData(std::vector<TYPE> val) { back = front = val; }
32 
34  back = s.back;
35  front = s.front;
36  }
37 
41  operator std::vector<TYPE> () const {
42  std::vector<TYPE> retval;
43  {
44  std::lock_guard<std::mutex> guard(const_cast<SyncMData<TYPE>*>(this)->lock);
45  retval = front;
46  }
47  return retval;
48  }
49 
53  TYPE operator[] (size_t idx) const {
54  TYPE retval;
55  {
56  std::lock_guard<std::mutex> guard(const_cast<SyncMData<TYPE>*>(this)->lock);
57  retval = front[idx];
58  }
59  return retval;
60  }
61 
66  SyncMData<TYPE>& operator= (std::vector<TYPE> val) {
67  {
68  std::lock_guard<std::mutex> guard(lock);
69  back = val;
70  }
71  SyncData::pushValue();
72  return *this;
73  }
74 
75 protected:
76 
81  void encode(std::vector<char> &d) override {
82  std::lock_guard<std::mutex> guard(lock);
83 
84  d.resize(1 + back.size() * sizeof(TYPE));
85 
86  char * m = reinterpret_cast<char*>(&back[0]);
87 
88  for (size_t idx = 0; idx < back.size() * sizeof(TYPE); ++idx) {
89  d[idx + 1] = m[idx];
90  }
91  }
92 
97  void decode(std::vector<char> d) override {
98  std::lock_guard<std::mutex> guard(lock);
99 
100  if ((d.size() - 1)%sizeof(TYPE) != 0)
101  throw gmCore::InvalidArgument("incorrect data size for decoding");
102 
103  size_t vector_size = (d.size() - 1)/sizeof(TYPE);
104 
105  std::vector<TYPE> value;
106  value.resize(vector_size);
107  char * m = reinterpret_cast<char*>(&value[0]);
108 
109  for (size_t idx = 0; idx < vector_size * sizeof(TYPE); ++idx)
110  m[idx] = d[idx + 1];
111 
112  back = std::move(value);
113  }
114 
118  void update() override {
119  std::lock_guard<std::mutex> guard(lock);
120  front = back;
121  }
122 
123 private:
124 
128  std::vector<TYPE> front;
129 
133  std::vector<TYPE> back;
134 
135  std::mutex lock;
136 
137 };
138 
139 typedef SyncMData<char> SyncMBool;
140 typedef SyncMData<int32_t> SyncMInt32;
141 typedef SyncMData<int64_t> SyncMInt64;
142 typedef SyncMData<float> SyncMFloat32;
143 typedef SyncMData<double> SyncMFloat64;
144 
145 END_NAMESPACE_GMNETWORK;
146 
147 #endif
Simple synchronizeable data container.
Definition: SyncData.hh:17
Simple, multi value (vector) synchronizeable data container, without support for pointers or types co...
Definition: SyncMData.hh:19
SyncMData(std::vector< TYPE > val)
Initializes the SyncMData to the specified value.
Definition: SyncMData.hh:31
SyncMData()
Initializes the SyncMData to an empty list.
Definition: SyncMData.hh:26
void decode(std::vector< char > d) override
Decodes the specified vector into the back data of the container, using vector indices 1-N.
Definition: SyncMData.hh:97
void update() override
Copies the back value to the front.
Definition: SyncMData.hh:118
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: SyncMData.hh:81
Standard exception for invalid arguments in a call to a function or object.
Definition: InvalidArgument.hh:15