EpcTools
An event based multi-threaded C++ development framework.
emsg.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2009-2019 Brian Waters
3 * Copyright (c) 2019 Sprint
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 #ifndef __emsg_h_included
19 #define __emsg_h_included
20 
23 
24 #include "ebase.h"
25 #include "etime.h"
26 #include "etimer.h"
27 
29 class EMessage
30 {
31  friend class EQueueBase;
32 
33 public:
36  {
37  }
38 
41  {
42  }
43 
46  virtual Void getLength(ULong &length);
50  virtual Void serialize(pVoid pBuffer, ULong &nOffset);
54  virtual Void unserialize(pVoid pBuffer, ULong &nOffset);
55 
59  Void elementLength(Bool val, ULong &length)
60  {
61  length += sizeof(Bool);
62  }
66  Void elementLength(Char val, ULong &length)
67  {
68  length += sizeof(Char);
69  }
73  Void elementLength(UChar val, ULong &length)
74  {
75  length += sizeof(UChar);
76  }
80  Void elementLength(Short val, ULong &length)
81  {
82  length += sizeof(Short);
83  }
87  Void elementLength(UShort val, ULong &length)
88  {
89  length += sizeof(UShort);
90  }
94  Void elementLength(Long val, ULong &length)
95  {
96  length += sizeof(Long);
97  }
101  Void elementLength(ULong val, ULong &length)
102  {
103  length += sizeof(ULong);
104  }
108  Void elementLength(LongLong val, ULong &length)
109  {
110  length += sizeof(LongLong);
111  }
115  Void elementLength(ULongLong val, ULong &length)
116  {
117  length += sizeof(ULongLong);
118  }
122  Void elementLength(Float val, ULong &length)
123  {
124  length += sizeof(Float);
125  }
129  Void elementLength(Double val, ULong &length)
130  {
131  length += sizeof(Double);
132  }
136  Void elementLength(cpStr val, ULong &length)
137  {
138  length += (sizeof(UShort) + (UShort)strlen(val));
139  }
143  Void elementLength(ETimer &val, ULong &length)
144  {
145  length += sizeof(epctime_t);
146  }
150  Void elementLength(ETime &val, ULong &length)
151  {
152  length += sizeof(LongLong); //sizeof(val.getTimeVal().tv_sec);
153  length += sizeof(LongLong); //sizeof(val.getTimeVal().tv_usec);
154  }
155 
160  Void pack(Bool val, pVoid pBuffer, ULong &nOffset);
165  Void pack(Char val, pVoid pBuffer, ULong &nOffset);
170  Void pack(UChar val, pVoid pBuffer, ULong &nOffset);
175  Void pack(Short val, pVoid pBuffer, ULong &nOffset);
180  Void pack(UShort val, pVoid pBuffer, ULong &nOffset);
185  Void pack(Long val, pVoid pBuffer, ULong &nOffset);
190  Void pack(ULong val, pVoid pBuffer, ULong &nOffset);
195  Void pack(LongLong val, pVoid pBuffer, ULong &nOffset);
200  Void pack(ULongLong val, pVoid pBuffer, ULong &nOffset);
205  Void pack(Float val, pVoid pBuffer, ULong &nOffset);
210  Void pack(Double val, pVoid pBuffer, ULong &nOffset);
215  Void pack(cpStr val, pVoid pBuffer, ULong &nOffset);
220  Void pack(ETimer &val, pVoid pBuffer, ULong &nOffset);
225  Void pack(ETime &val, pVoid pBuffer, ULong &nOffset);
230  Void pack(EString &val, pVoid pBuffer, ULong &nOffset);
231 
236  Void unpack(Bool &val, pVoid pBuffer, ULong &nOffset);
241  Void unpack(Char &val, pVoid pBuffer, ULong &nOffset);
246  Void unpack(UChar &val, pVoid pBuffer, ULong &nOffset);
251  Void unpack(Short &val, pVoid pBuffer, ULong &nOffset);
256  Void unpack(UShort &val, pVoid pBuffer, ULong &nOffset);
261  Void unpack(Long &val, pVoid pBuffer, ULong &nOffset);
266  Void unpack(ULong &val, pVoid pBuffer, ULong &nOffset);
271  Void unpack(LongLong &val, pVoid pBuffer, ULong &nOffset);
276  Void unpack(ULongLong &val, pVoid pBuffer, ULong &nOffset);
281  Void unpack(Float &val, pVoid pBuffer, ULong &nOffset);
286  Void unpack(Double &val, pVoid pBuffer, ULong &nOffset);
291  Void unpack(pStr val, pVoid pBuffer, ULong &nOffset);
296  Void unpack(ETimer &val, pVoid pBuffer, ULong &nOffset);
301  Void unpack(ETime &val, pVoid pBuffer, ULong &nOffset);
306  Void unpack(EString &val, pVoid pBuffer, ULong &nOffset);
307 };
308 
310 template <class T>
311 class EMessageVector : public EMessage
312 {
313 public:
316  {
317  }
318 
322  {
323  m_list = val.m_list;
324  }
325 
328  {
329  }
330 
333  vector<T> &getList()
334  {
335  return m_list;
336  }
337 
340  Void vectorLength(ULong &length)
341  {
342  ULong len = 0;
343  ULong vectorLength = m_list.size();
344 
346  for (ULong i = 0; i < vectorLength; i++)
347  m_list[i].getLength(len);
348 
349  length += len;
350  }
351 
355  Void packVector(pVoid pBuffer, ULong &nOffset)
356  {
357  ULong vectorLength = m_list.size();
358 
359  pack(vectorLength, pBuffer, nOffset);
360  for (ULong i = 0; i < vectorLength; i++)
361  m_list[i].serialize(pBuffer, nOffset);
362  }
363 
367  Void unpackVector(pVoid Buffer, ULong &nOffset)
368  {
369  ULong vectorLength;
370 
371  unpack(vectorLength, Buffer, nOffset);
372 
373  for (ULong i = 0; i < vectorLength; i++)
374  {
375  T val;
376  val.unserialize(Buffer, nOffset);
377  m_list.push_back(val);
378  }
379  }
380 
381 private:
382  vector<T> m_list;
383 };
384 #endif // #ifndef __emsg_h_included
EMessage::elementLength
Void elementLength(Char val, ULong &length)
Adds the length of a Bool value to the message length.
Definition: emsg.h:66
EMessageVector::EMessageVector
EMessageVector(EMessageVector const &val)
Copy constructor.
Definition: emsg.h:319
EMessageVector::packVector
Void packVector(pVoid pBuffer, ULong &nOffset)
Packs the vector.
Definition: emsg.h:355
EMessage::~EMessage
~EMessage()
Class destructor.
Definition: emsg.h:40
ETime
Class for manipulating date and time of day values.
Definition: etime.h:199
EMessage::elementLength
Void elementLength(UChar val, ULong &length)
Adds the length of a Bool value to the message length.
Definition: emsg.h:73
EMessageVector::getList
vector< T > & getList()
Retrieves the message vector.
Definition: emsg.h:333
EMessage::elementLength
Void elementLength(Bool val, ULong &length)
Adds the length of a Bool value to the message length.
Definition: emsg.h:59
EQueueBase
The message queue base class.
Definition: eqbase.h:120
ebase.h
Macros for various standard C library functions and standard includes.
EMessage::elementLength
Void elementLength(ULongLong val, ULong &length)
Adds the length of a Bool value to the message length.
Definition: emsg.h:115
EMessage::unpack
Void unpack(Bool &val, pVoid pBuffer, ULong &nOffset)
Unpacks a Bool value.
Definition: emsg.cpp:134
EMessage::getLength
virtual Void getLength(ULong &length)
Calculates the packed length of a message.
Definition: emsg.cpp:23
EMessage::EMessage
EMessage()
Default constructor.
Definition: emsg.h:35
etimer.h
ETimer
Implements a stopwatch style timer.
Definition: etimer.h:26
etime.h
Provides class for manipulating time of day values.
EMessage::elementLength
Void elementLength(ETimer &val, ULong &length)
Adds the length of a Bool value to the message length.
Definition: emsg.h:143
EMessage::elementLength
Void elementLength(ULong val, ULong &length)
Adds the length of a Bool value to the message length.
Definition: emsg.h:101
EMessage::elementLength
Void elementLength(ETime &val, ULong &length)
Adds the length of a Bool value to the message length.
Definition: emsg.h:150
EMessageVector::unpackVector
Void unpackVector(pVoid Buffer, ULong &nOffset)
Unpacks a vector.
Definition: emsg.h:367
EMessage::serialize
virtual Void serialize(pVoid pBuffer, ULong &nOffset)
Serializes (packs) a message.
Definition: emsg.cpp:27
EMessageVector
A vector of fields.
Definition: emsg.h:311
EMessageVector::vectorLength
Void vectorLength(ULong &length)
Calcuates the packed length of the vector.
Definition: emsg.h:340
EMessage::elementLength
Void elementLength(Long val, ULong &length)
Adds the length of a Bool value to the message length.
Definition: emsg.h:94
EMessage::elementLength
Void elementLength(Float val, ULong &length)
Adds the length of a Bool value to the message length.
Definition: emsg.h:122
EMessageVector::~EMessageVector
~EMessageVector()
Class destructor.
Definition: emsg.h:327
EMessage::elementLength
Void elementLength(Short val, ULong &length)
Adds the length of a Bool value to the message length.
Definition: emsg.h:80
EMessage::elementLength
Void elementLength(cpStr val, ULong &length)
Adds the length of a Bool value to the message length.
Definition: emsg.h:136
EMessage::elementLength
Void elementLength(Double val, ULong &length)
Adds the length of a Bool value to the message length.
Definition: emsg.h:129
Buffer
Definition: efdjson.cpp:77
EMessageVector::EMessageVector
EMessageVector()
Default constructor.
Definition: emsg.h:315
EMessage::elementLength
Void elementLength(UShort val, ULong &length)
Adds the length of a Bool value to the message length.
Definition: emsg.h:87
EMessage::pack
Void pack(Bool val, pVoid pBuffer, ULong &nOffset)
Packs the Bool value.
Definition: emsg.cpp:35
EString
String class.
Definition: estring.h:30
EMessage::unserialize
virtual Void unserialize(pVoid pBuffer, ULong &nOffset)
Unserializes (unpacks) a message.
Definition: emsg.cpp:31
EMessage
The message queue base message class.
Definition: emsg.h:29
epctime_t
long long int epctime_t
time typedef
Definition: ebase.h:34
EMessage::elementLength
Void elementLength(LongLong val, ULong &length)
Adds the length of a Bool value to the message length.
Definition: emsg.h:108