ReplicaNet and RNLobby  1
MessageHelper.h
1 /* START_LICENSE_HEADER
2 
3 Copyright (C) 2000 Martin Piper, original design and program code
4 Copyright (C) 2001 Replica Software
5 
6 This program file is copyright (C) Replica Software and can only be used under license.
7 For more information visit: http://www.replicanet.com/
8 Or email: info@replicanet.com
9 
10 END_LICENSE_HEADER */
11 #include "RNPlatform/Inc/MemoryTracking.h"
12 #ifndef __MESSAGEHELPER_H__
13 #define __MESSAGEHELPER_H__
14 #include <string>
15 
16 namespace RNReplicaNet
17 {
18 
19 #define MESSAGEHELPER_ADDVARIABLE(x) \
20 AddVariable(&(x),sizeof(x));
21 
22 #define MESSAGEHELPER_GETVARIABLE(x) \
23 GetVariable(&(x),sizeof(x));
24 
25 #define MESSAGEHELPER_ADDVARIABLEp(y,x) \
26 y.AddVariable(&(x),sizeof(x));
27 
28 #define MESSAGEHELPER_GETVARIABLEp(y,x) \
29 y.GetVariable(&(x),sizeof(x));
30 
36 {
37 public:
38  class Exception
39  {
40  public:
41  Exception() {}
42  virtual ~Exception() {}
43  };
47  MessageHelper();
48 
52  virtual ~MessageHelper();
53 
58  void SetBuffer(void *const buffer);
59 
64  void *GetBuffer(void) const;
65 
70  void *GetCurrentPosition(void) const;
71 
77  virtual void AddVariable(const void *variable,const int size);
79  static void AddVariable(void *buffer, const void *variable, const int size);
80 
86  virtual void AddData(const void *data,const int size);
87 
93  void GetVariable(void *const variable,const int size);
95  static void GetVariable(const void *buffer, void *const variable, const int size);
96 
102  void GetData(void *const data,const int size);
103 
108  void SkipData(const int size);
109 
114  int GetSize(void) const;
115 
120  void SetSize(const int size);
121 
126  void AddInteger(const int value);
127 
132  void AddFloat(const float value);
133 
138  int GetInteger(void);
139 
145  float GetFloat(void);
146 
147  // Direct buffer modification functions with endian checking
148  // The offset is in chars into the buffer.
149  static int GetInt(const void *buffer, const int offset);
150  static short GetShort(const void *buffer, const int offset);
151  static void SetInt(void *buffer, const int offset, const int value);
152  static void SetShort(void *buffer, const int offset, const short value);
153 
157  static std::string DumpAsHex(const void *data,const int size,const bool csv = true);
158 
159  std::string DumpAsHex(const bool csv = true) const
160  {
161  return DumpAsHex(GetBuffer(),GetSize(),csv);
162  }
163 
168  void SetGuardSize(const int size = -1);
169  void SanityCheckGuardSize(const int newSize = 0);
170 
174  int GetLastSizeAdded(void) const;
175 
176  // Define operator<< and operator>> to make using this class a little easier.
177  // For all operator>> the rhs is replaced with the data in lhs.
178  // Generic cases
179  template <typename T>
180  void operator<<(const T& rhs)
181  {
182  MESSAGEHELPER_ADDVARIABLE(rhs);
183  }
184  template <typename T>
185  void operator>>(T& rhs)
186  {
187  MESSAGEHELPER_GETVARIABLE(rhs);
188  }
189 
190  // Special cases
191  void operator<<(const char *rhs);
192  void operator<<(const std::string &rhs);
193  void operator>>(std::string &rhs);
194 
195  // For platforms where sizeof(bool) is different
196  void operator<<(const bool &rhs);
197  void operator>>(bool &rhs);
198 
199 private:
200  unsigned char *mFirstPtr;
201  unsigned char *mBufferPtr;
202  int mGuardSize;
203 
204 protected:
205  int mLastSizeAdded;
206 };
207 
212 {
213 public:
218 
223  DynamicMessageHelper( const int size );
224 
230  DynamicMessageHelper( const void * message , const int size );
231 
235  virtual ~DynamicMessageHelper();
236 
237 private:
241  void SetBuffer(void *const) {}
242 public:
243 
249  void SetBufferSize(const int size = 1024);
250 
255  int GetBufferSize(void);
256 
261  void SetSize(const int size);
262 
263 
264  void AddVariable(const void *variable,const int size);
265 
266  void AddData(const void *data,const int size);
267 
271  void FreeBuffer(void);
272 
276  void EnsureBufferAllocated(void);
277 
282  bool Write(const char *filename,const bool rawData = false);
283 
288  bool Read(const char *filename,const bool rawData = false);
289 
290  // Define operator<< and operator>> to make using this class a little easier.
291  // For all operator>> the rhs is replaced with the data in lhs.
292  // Generic cases
293  template <typename T>
294  void operator<<(const T& rhs)
295  {
296  MESSAGEHELPER_ADDVARIABLE(rhs);
297  }
298  template <typename T>
299  void operator>>(T& rhs)
300  {
301  MESSAGEHELPER_GETVARIABLE(rhs);
302  }
303 
304  // Special cases
305  void operator<<(const char *rhs);
306  void operator<<(const std::string &rhs);
307  void operator>>(std::string &rhs);
308 
309  void operator<<(const DynamicMessageHelper &rhs);
310  void operator>>(DynamicMessageHelper &rhs);
311 
312  // Tests using GetSize() for the length of the data to test.
313  bool operator==(const DynamicMessageHelper &rhs) const;
314  bool operator!=(const DynamicMessageHelper &rhs) const;
315 
316  // For platforms where sizeof(bool) is different
317  void operator<<(const bool &rhs);
318  void operator>>(bool &rhs);
319 
323  bool ReadAsHex(const std::string &data);
324 
325 private:
326  void CheckBufferSize(const int newSize);
327  void *mBuffer;
328  int mSize;
329 };
330 
331 } // namespace RNReplicaNet
332 
333 #endif
void SetGuardSize(const int size=-1)
Definition: MessageHelper.h:38
virtual void AddData(const void *data, const int size)
void GetData(void *const data, const int size)
void GetVariable(void *const variable, const int size)
void AddVariable(const void *variable, const int size)
int GetLastSizeAdded(void) const
void AddInteger(const int value)
void * GetCurrentPosition(void) const
void SkipData(const int size)
Definition: MessageHelper.h:35
virtual void AddVariable(const void *variable, const int size)
void AddData(const void *data, const int size)
void * GetBuffer(void) const
bool ReadAsHex(const std::string &data)
void SetSize(const int size)
bool Write(const char *filename, const bool rawData=false)
static std::string DumpAsHex(const void *data, const int size, const bool csv=true)
bool Read(const char *filename, const bool rawData=false)
void AddFloat(const float value)
void SetBuffer(void *const buffer)
Definition: MessageHelper.h:211
void SetBufferSize(const int size=1024)
int GetSize(void) const