sipxportlib  Version 3.3
UtlSerialized.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2009 SIPez LLC.
3 // Licensed to SIPfoundry under a Contributor Agreement.
4 //
5 // Copyright (C) 2009 SIPfoundry Inc.
6 // Licensed by SIPfoundry under the LGPL license.
7 //
8 // $$
10 
11 #ifndef _UtlSerialized_h_
12 #define _UtlSerialized_h_
13 
14 // SYSTEM INCLUDES
15 // APPLICATION INCLUDES
16 // DEFINES
17 // MACROS
18 // EXTERNAL FUNCTIONS
19 // EXTERNAL VARIABLES
20 // CONSTANTS
21 // STRUCTS
22 // TYPEDEFS
23 // FORWARD DECLARATIONS
24 
25 #define UTL_SERIALIZED_SIZE 1024
26 
28 {
29 /* //////////////////////////// PUBLIC //////////////////////////////////// */
30 public:
31 
32 /* ============================ CREATORS ================================== */
34 
35 
37  inline UtlSerialized()
38  : mpEnd(mData)
39  , mSize(0)
40  {
41  };
42 
44  inline UtlSerialized(const UtlSerialized& rhs)
45  : mpEnd(mData+(rhs.mpEnd-rhs.mData))
46  , mSize(rhs.mSize)
47  {
48  memcpy(mData, rhs.mData, rhs.getSize());
49  };
50 
52  virtual ~UtlSerialized()
53  {
54  };
55 
57 
58 /* ============================ MANIPULATORS ============================== */
60 
61 
62 #define SERIALIZE_POD_DEFINE(type) \
63  inline OsStatus serialize(type val) \
64  { \
65  if (sizeof(type) > getFreeSize()) \
66  { \
67  return OS_LIMIT_REACHED; \
68  } \
69  *(type*)mpEnd = val; \
70  mpEnd += sizeof(type); \
71  mSize += sizeof(type); \
72  return OS_SUCCESS; \
73  } \
74  inline OsStatus deserialize(type &val) \
75  { \
76  if (sizeof(type) > getSize() - (mpEnd-mData)) \
77  { \
78  return OS_NO_MORE_DATA; \
79  } \
80  val = *(type*)mpEnd; \
81  mpEnd += sizeof(type); \
82  return OS_SUCCESS; \
83  }
84 
86  SERIALIZE_POD_DEFINE(short);
89  SERIALIZE_POD_DEFINE(unsigned char);
90  SERIALIZE_POD_DEFINE(unsigned short);
91  SERIALIZE_POD_DEFINE(unsigned int);
92  SERIALIZE_POD_DEFINE(unsigned long);
93 // || defined(__x86_64__)
94 #if defined(_WIN64) || defined(__ppc64__)
95  SERIALIZE_POD_DEFINE(size_t);
96 #endif
97 
98  SERIALIZE_POD_DEFINE(void *);
99 #undef SERIALIZE_POD_DEFINE
100 
101  inline OsStatus serialize(const UtlString &val)
102  {
103  // We add 1 to the length to include end-of-string \0 byte.
104  size_t length = val.length() + 1;
105  if (sizeof(int) + length > getFreeSize())
106  {
107  return OS_LIMIT_REACHED;
108  }
109  serialize(length);
110  memcpy(mpEnd, val.data(), length);
111  mpEnd += length;
112  mSize += length;
113  return OS_SUCCESS;
114  }
115 
117  {
118  OsStatus result;
119  size_t length;
120  result = deserialize(length);
121  if (result != OS_SUCCESS)
122  {
123  return result;
124  }
125  if (length > getSize() - (mpEnd-mData))
126  {
127  // Something is wrong - length of the string is bigger then available data.
128  return OS_FAILED;
129  }
130  // Resize reserves space for last \0 internally, so we should exclude
131  // it from the length.
132  val.resize(length-1, FALSE);
133  memcpy((void*)val.data(), mpEnd, length);
134  mpEnd += length;
135  return OS_SUCCESS;
136  }
137 
139  inline void finishSerialize()
140  {
141  mpEnd = mData;
142  }
143 
146  {
147  if(&rhs == this)
148  {
149  return(*this);
150  }
151 
152  memcpy(mData, rhs.mData, rhs.getSize());
153  mpEnd = mData+(rhs.mpEnd-rhs.mData);
154  mSize = rhs.mSize;
155 
156  return *this;
157  }
158 
160 
161 /* ============================ ACCESSORS ================================= */
163 
164 
166  inline size_t getMaxSize() const {return UTL_SERIALIZED_SIZE;}
167 
169  inline size_t getSize() const {return mSize;}
170 
172  inline size_t getFreeSize() const {return getMaxSize()-getSize();}
173 
175 
176 /* ============================ INQUIRY =================================== */
178 
179 
181 
182 /* //////////////////////////// PROTECTED ///////////////////////////////// */
183 protected:
184 
187  size_t mSize;
188 
189 /* //////////////////////////// PRIVATE /////////////////////////////////// */
190 private:
191 
192 };
193 
194 /* ============================ INLINE METHODS ============================ */
195 
196 #endif // _UtlSerialized_h_
OsStatus serialize(const UtlString &val)
Definition: UtlSerialized.h:101
UtlSerialized()
Constructor.
Definition: UtlSerialized.h:37
void resize(size_t N, UtlBoolean clearTail=TRUE)
Set a new size for the string.
Definition: UtlString.cpp:671
const char * data() const
Return a read-only pointer to the stored string value..
Definition: UtlString.cpp:832
OsStatus deserialize(UtlString &val)
Definition: UtlSerialized.h:116
Definition: OsStatus.h:30
uint8_t mData[UTL_SERIALIZED_SIZE]
Array to serialize data to.
Definition: UtlSerialized.h:186
unsigned char uint8_t
Definition: stdint.h:78
void finishSerialize()
Prepare for de-serialization.
Definition: UtlSerialized.h:139
#define UTL_SERIALIZED_SIZE
Definition: UtlSerialized.h:25
OsStatus
Definition: OsStatus.h:27
size_t mSize
Size of the serialized data.
Definition: UtlSerialized.h:187
virtual ~UtlSerialized()
Destructor.
Definition: UtlSerialized.h:52
Definition: OsStatus.h:41
size_t length() const
The current length of the string value.
Definition: UtlString.cpp:825
Definition: OsStatus.h:31
Definition: UtlString.h:48
size_t getFreeSize() const
Get free size in storage (in bytes).
Definition: UtlSerialized.h:172
size_t getMaxSize() const
Get size of available space (in bytes).
Definition: UtlSerialized.h:166
uint8_t * mpEnd
Pointer to the byte right after the end of data.
Definition: UtlSerialized.h:185
Definition: UtlSerialized.h:27
SERIALIZE_POD_DEFINE(char)
UtlSerialized(const UtlSerialized &rhs)
Copy constructor.
Definition: UtlSerialized.h:44
size_t getSize() const
Get size of stored data (in bytes).
Definition: UtlSerialized.h:169
#define FALSE
Definition: UtlDefs.h:21
UtlSerialized & operator=(const UtlSerialized &rhs)
Assignment operator.
Definition: UtlSerialized.h:145