VR-Vantage 2.7 API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DtMessage.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2007 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
8 #ifndef DtMessage_H_
9 #define DtMessage_H_
10 
11 #include <vrvUtil/vrvUtil.h>
12 #include <vector>
13 #include <sstream>
14 #include <ostream>
15 
16 #include <tbb/tbb_allocator.h>
17 
20 
21 namespace makVrv
22 {
23 
24  class DtUnicode;
25 
31 {
32 public:
33 
34  typedef unsigned char Byte;
35  typedef std::vector<Byte, tbb::tbb_allocator<Byte> > ByteBuffer;
36 
37  static const int theHeaderSize = (sizeof(unsigned short) + sizeof(char)) * 2;
38 
39  //Helper Macro to pack the size of a type into a unique id.
40  #define packSizeOf(type) ((sizeof(type)-1)<<4)
41 
42  //Helper Macro to unpack the size of a datatype.
43  #define unpackSizeOf(dataType) (((dataType)>>4)+1)
44 
45  //Help macro to check to see if a datatype enum is a simple type.
46  #define isSimpleType(dataType) ((dataType)<128)
47 
48  //Simple data types are encoded as 1 bit 0, 3 bit size, 4 bit id
49  //Complex data types are encoded as 8bit id, w/ MSBit = 1 (ie >= 128)
50  enum DataType
51  {
52  Unknown = 0,
53  Char = 1 | packSizeOf(char),
54  UnsignedChar = 2 | packSizeOf(unsigned char),
55  Short = 3 | packSizeOf(short),
56  UnsignedShort = 4 | packSizeOf(unsigned short),
57  Int = 5 | packSizeOf(int),
58  UnsignedInt = 6 | packSizeOf(unsigned int),
59  Float = 7 | packSizeOf(float),
60  LongLong = 8 | packSizeOf(long long),
61  UnsignedLongLong = 9 | packSizeOf(unsigned long long),
62  Double = 10 | packSizeOf(double),
63  Bool = 11 | packSizeOf(bool),
64  Long = 12 | packSizeOf(long),
65  UnsignedLong = 13 | packSizeOf(unsigned long),
66  String = 128,
67  FixedArray = 129,
68  UnicodeString = 130,
69  User_Start = 150,
70  User_Last = 255
71  };
72 
73  //Data Format:
75  //
78  //
82 
86  DtMessage(unsigned short classId = 0,unsigned short functionId = 0);
87 
92  DtMessage(const Byte* buffer,size_t size);
93 
97  DtMessage(const DtMessage& orig);
98 
100  ~DtMessage();
101 
103  void printDebugMessageContents(std::ostream&);
104 
106  const char* dataTypeName(unsigned char dataType);
107 
109  unsigned short classId() const;
110 
112  unsigned short functionId() const;
113 
115  const Byte* data() const;
116 
118  size_t size() const;
119 
121  void emptyMessage();
122 
125  void resetRead();
126 
130  void serializeHeader(unsigned short classId, unsigned short functionId);
131 
134  bool serializeWriteOut() const;
135 
138  void setSerializeWriteOut(bool writeOut);
139 
141 
142 
143  DtMessage& operator >> (bool& b);
144  DtMessage& operator >> (char& c);
145  DtMessage& operator >> (unsigned char& c);
146  DtMessage& operator >> (short& i);
147  DtMessage& operator >> (unsigned short& i);
148  DtMessage& operator >> (int& i);
149  DtMessage& operator >> (unsigned int& i);
150  DtMessage& operator >> (long long& i);
151  DtMessage& operator >> (unsigned long long& i);
152  DtMessage& operator >> (float& f);
153  DtMessage& operator >> (double& f);
154  DtMessage& operator >> (long& i);
155  DtMessage& operator >> (unsigned long& i);
158  DtMessage& operator >> (ByteBuffer& rawData);
159 
161  DtMessage& operator >> (std::pair<void*,int>& rawData);
163 
165 
166  DtMessage& operator << (bool b);
167  DtMessage& operator << (char c);
168  DtMessage& operator << (unsigned char c);
169  DtMessage& operator << (short i);
170  DtMessage& operator << (unsigned short i);
171  DtMessage& operator << (int i);
172  DtMessage& operator << (unsigned int i);
173  DtMessage& operator << (long long i);
174  DtMessage& operator << (unsigned long long i);
175  DtMessage& operator << (float f);
176  DtMessage& operator << (double f);
177  DtMessage& operator << (long i);
178  DtMessage& operator << (unsigned long i);
181  DtMessage& operator << (const char* str);
182  DtMessage& operator << (const ByteBuffer& rawData);
183  DtMessage& operator << (const std::pair<void*, int>& rawData);
184 
186 
188  void addOnlyData(DtMessage& message);
189 
191  void addData(unsigned int dataType,const void* data,int dataSize);
192 
194  bool canReadData() const;
195 
198  unsigned char nextDataType() const;
199 
204  unsigned int nextDataSize() const;
205 
208  const void* nextDataPointer() const;
209 
211  void skipToNextData();
212 
215  void advanceRead(size_t bytes);
216 
220  void getData(int dataType,void* output);
221 
223 
224 
225  void addMultiple()
226  {
227  }
228 
229  template <typename T1>
230  void addMultiple(T1 t1)
231  {
232  *this << t1;
233  }
234 
235  template <typename T1,typename T2>
236  void addMultiple(T1 t1,T2 t2)
237  {
238  *this << t1 << t2;
239  }
240 
241  template <typename T1,typename T2,typename T3>
242  void addMultiple(T1 t1,T2 t2,T3 t3)
243  {
244  *this << t1 << t2 << t3;
245  }
246 
247  template <typename T1,typename T2,typename T3,typename T4>
248  void addMultiple(T1 t1,T2 t2,T3 t3,T4 t4)
249  {
250  *this << t1 << t2 << t3 << t4;
251  }
252 
253  template <typename T1,typename T2,typename T3,typename T4,typename T5>
254  void addMultiple(T1 t1,T2 t2,T3 t3,T4 t4,T5 t5)
255  {
256  *this << t1 << t2 << t3 << t4 << t5;
257  }
258 
259  template <typename T1,typename T2,typename T3,typename T4,typename T5,
260  typename T6>
261  void addMultiple(T1 t1,T2 t2,T3 t3,T4 t4,T5 t5,T6 t6)
262  {
263  *this << t1 << t2 << t3 << t4 << t5 << t6;
264  }
265 
266  template <typename T1,typename T2,typename T3,typename T4,typename T5,
267  typename T6,typename T7>
268  void addMultiple(T1 t1,T2 t2,T3 t3,T4 t4,T5 t5,T6 t6,T7 t7)
269  {
270  *this << t1 << t2 << t3 << t4 << t5 << t6 << t7;
271  }
272 
273  template <typename T1,typename T2,typename T3,typename T4,typename T5,
274  typename T6,typename T7,typename T8>
275  void addMultiple(T1 t1,T2 t2,T3 t3,T4 t4,T5 t5,T6 t6,T7 t7,T8 t8)
276  {
277  *this << t1 << t2 << t3 << t4 << t5 << t6 << t7 << t8;
278  }
279 
280  void readMultiple()
281  {
282  }
283 
284  template <typename T1>
285  void readMultiple(T1& t1)
286  {
287  *this >> t1;
288  }
289 
290  template <typename T1,typename T2>
291  void readMultiple(T1& t1,T2& t2)
292  {
293  *this >> t1 >> t2;
294  }
295 
296  template <typename T1,typename T2,typename T3>
297  void readMultiple(T1& t1,T2& t2,T3& t3)
298  {
299  *this >> t1 >> t2 >> t3;
300  }
301 
302  template <typename T1,typename T2,typename T3,typename T4>
303  void readMultiple(T1& t1,T2& t2,T3& t3,T4& t4)
304  {
305  *this >> t1 >> t2 >> t3 >> t4;
306  }
307 
308  template <typename T1,typename T2,typename T3,typename T4,typename T5>
309  void readMultiple(T1& t1,T2& t2,T3& t3,T4& t4,T5& t5)
310  {
311  *this >> t1 >> t2 >> t3 >> t4 >> t5;
312  }
313 
314  template <typename T1,typename T2,typename T3,typename T4,typename T5,
315  typename T6>
316  void readMultiple(T1& t1,T2& t2,T3& t3,T4& t4,T5& t5,T6& t6)
317  {
318  *this >> t1 >> t2 >> t3 >> t4 >> t5 >> t6;
319  }
320 
321  template <typename T1,typename T2,typename T3,typename T4,typename T5,
322  typename T6,typename T7>
323  void readMultiple(T1& t1,T2& t2,T3& t3,T4& t4,T5& t5,T6& t6,T7& t7)
324  {
325  *this >> t1 >> t2 >> t3 >> t4 >> t5 >> t6 >> t7;
326  }
327 
328  template <typename T1,typename T2,typename T3,typename T4,typename T5,
329  typename T6,typename T7,typename T8>
330  void readMultiple(T1& t1,T2& t2,T3& t3,T4& t4,T5& t5,T6& t6,T7& t7,T8& t8)
331  {
332  *this >> t1 >> t2 >> t3 >> t4 >> t5 >> t6 >> t7 >> t8;
333  }
334 
335 
336  template <typename T1>
337  void serialize(T1& t1)
338  {
339  if(mySerializeWriteOutFlag)
340  *this >> t1;
341  else
342  *this << t1;
343  }
344 
345  template <typename T1,typename T2>
346  void serialize(T1& t1,T2& t2)
347  {
348  if(mySerializeWriteOutFlag)
349  *this >> t1 >> t2;
350  else
351  *this << t1 << t2;
352  }
353 
354  template <typename T1,typename T2,typename T3>
355  void serialize(T1& t1,T2& t2,T3& t3)
356  {
357  if(mySerializeWriteOutFlag)
358  *this >> t1 >> t2 >> t3;
359  else
360  *this << t1 << t2 << t3;
361  }
362 
363  template <typename T1,typename T2,typename T3,typename T4>
364  void serialize(T1& t1,T2& t2,T3& t3,T4& t4)
365  {
366  if(mySerializeWriteOutFlag)
367  *this >> t1 >> t2 >> t3 >> t4;
368  else
369  *this << t1 << t2 << t3 << t4;
370  }
371 
372  template <typename T1,typename T2,typename T3,typename T4,typename T5>
373  void serialize(T1& t1,T2& t2,T3& t3,T4& t4,T5& t5)
374  {
375  if(mySerializeWriteOutFlag)
376  *this >> t1 >> t2 >> t3 >> t4 >> t5;
377  else
378  *this << t1 << t2 << t3 << t4 << t5;
379  }
380 
381  template <typename T1,typename T2,typename T3,typename T4,typename T5,
382  typename T6>
383  void serialize(T1& t1,T2& t2,T3& t3,T4& t4,T5& t5,T6& t6)
384  {
385  if(mySerializeWriteOutFlag)
386  *this >> t1 >> t2 >> t3 >> t4 >> t5 >> t6;
387  else
388  *this << t1 << t2 << t3 << t4 << t5 << t6;
389  }
390 
391  template <typename T1,typename T2,typename T3,typename T4,typename T5,
392  typename T6,typename T7>
393  void serialize(T1& t1,T2& t2,T3& t3,T4& t4,T5& t5,T6& t6,T7& t7)
394  {
395  if(mySerializeWriteOutFlag)
396  *this >> t1 >> t2 >> t3 >> t4 >> t5 >> t6 >> t7;
397  else
398  *this << t1 << t2 << t3 << t4 << t5 << t6 << t7;
399  }
400 
401  template <typename T1,typename T2,typename T3,typename T4,typename T5,
402  typename T6,typename T7,typename T8>
403  void serialize(T1& t1,T2& t2,T3& t3,T4& t4,T5& t5,T6& t6,T7& t7,T8& t8)
404  {
405  if(mySerializeWriteOutFlag)
406  *this >> t1 >> t2 >> t3 >> t4 >> t5 >> t6 >> t7 >> t8;
407  else
408  *this << t1 << t2 << t3 << t4 << t5 << t6 << t7 << t8;
409  }
410 
411  //Allows serialize any serialize via archive classes.
412  template <typename T>
413  void serializeViaArchive(T& t)
414  {
418  class FastStringBuf : public std::stringbuf
419  {
420  public:
421  const char* c_str() const { return pbase();}
422  size_t outputSize() const
423  {
424  return pptr() - pbase();
425  }
426  };
427 
428  std::pair<void*,int> configMemory;
429  //Are we writing to the configuration.
430  if(serializeWriteOut())
431  {
432  //Yank out a binary chunk from the message.
433  serialize(configMemory);
434  //Create an input string stream.
435  std::istringstream memoryStream;
436  //Set the stream to the message data.
437  memoryStream.str(std::string((char*)configMemory.first,configMemory.second));
438  //Create an archive to read the stream.
439  portable_binary_iarchive ia(memoryStream);
440  //Serialize out from the stream.
441  //t.serialize(ia,0);
442  ia & t;
443  //Clean up the message memory.
444  delete[] (char*)configMemory.first;
445  }
446  else
447  {
448 
449  //Create a fast buffer.
450  FastStringBuf fastBuffer;
451  //Create a output stream for it.
452  std::ostream memoryStream(&fastBuffer);
453  //Create an archive to write to it.
454  portable_binary_oarchive oa(memoryStream);
455  //Write the object to the archive.
456  oa & t;
457  //t.serialize(oa,boost::serialization::version<T>::value);
458  //Grab the written data.
459  configMemory.first = (void*) fastBuffer.c_str();
460  configMemory.second = fastBuffer.outputSize();
461  //Put that data into this message.
462  serialize(configMemory);
463  }
464  }
465 
466 protected:
467 
469  void setClassId(unsigned short classId);
470 
472  void setFunctionId(unsigned short functionId);
473 
475  ByteBuffer::const_iterator myReadIter;
477 
478 };
479 
480 }
481 
482 #endif


Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)