cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmnDataFunctionsMacros.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Anton Deguet
7  Created on: 2011-06-27
8 
9  (C) Copyright 2011-2013 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 
20 */
21 
22 #pragma once
23 #ifndef _cmnDataFunctionsMacros_h
24 #define _cmnDataFunctionsMacros_h
25 
26 
33 #define CMN_DATA_COPY_USING_ASSIGN(_type) \
34  static void Copy(_type & data, \
35  const _type & source) { \
36  data = source; \
37  }
38 
39 
41 #define CMN_DATA_SERIALIZE_DESCRIPTION(_type, _description) \
42  static std::string SerializeDescription(const _type & CMN_UNUSED(data), \
43  const char CMN_UNUSED(delimiter) = ',', \
44  const std::string & userDescription = "") \
45  { \
46  return (userDescription == "" ? "{"#_description"}" : (userDescription + ":{"#_description"}")); \
47  }
48 
49 
50 #define CMN_DATA_SERIALIZE_BINARY_BYTE_SIZE_USING_SIZEOF(_type) \
51  static size_t SerializeBinaryByteSize(const _type & CMN_UNUSED(data)) \
52  { \
53  return sizeof(_type); \
54  }
55 
62 #define CMN_DATA_SERIALIZE_BINARY_BUFFER_USING_CAST_TO_CHAR(_type) \
63  static size_t SerializeBinary(const _type & data, \
64  char * buffer, size_t bufferSize) \
65  { \
66  const size_t sizeOfData = cmnData<_type>::SerializeBinaryByteSize(data); \
67  if (bufferSize < sizeOfData) { \
68  return 0; \
69  } \
70  *(reinterpret_cast<_type *>(buffer)) = data; \
71  bufferSize -= sizeOfData; \
72  return sizeOfData; \
73  }
74 
75 #define CMN_DATA_DE_SERIALIZE_BINARY_BUFFER_USING_CAST_TO_CHAR_NO_BYTE_SWAP(_type) \
76  static size_t DeSerializeBinary(_type & data, const char * buffer, size_t bufferSize, \
77  const cmnDataFormat & CMN_UNUSED(localFormat), \
78  const cmnDataFormat & CMN_UNUSED(remoteFormat)) \
79  { \
80  const size_t sizeOfData = cmnData<_type>::SerializeBinaryByteSize(data); \
81  if (bufferSize < sizeOfData) { \
82  return 0; \
83  } \
84  data = *(reinterpret_cast<const _type *>(buffer)); \
85  bufferSize -= sizeOfData; \
86  return sizeOfData; \
87  }
88 
89 #define CMN_DATA_DE_SERIALIZE_BINARY_BUFFER_USING_CAST_TO_CHAR_AND_BYTE_SWAP(_type) \
90  static size_t DeSerializeBinary(_type & data, const char * buffer, size_t bufferSize, \
91  const cmnDataFormat & localFormat, \
92  const cmnDataFormat & remoteFormat) \
93  { \
94  const size_t sizeOfData = cmnData<_type>::SerializeBinaryByteSize(data); \
95  if (bufferSize < sizeOfData) { \
96  return 0; \
97  } \
98  data = *(reinterpret_cast<const _type *>(buffer)); \
99  if (remoteFormat.GetEndianness() != localFormat.GetEndianness()) { \
100  cmnDataByteSwap(data); \
101  } \
102  bufferSize -= sizeOfData; \
103  return sizeOfData; \
104  }
105 
112 #define CMN_DATA_SERIALIZE_BINARY_STREAM_USING_CAST_TO_CHAR(_type) \
113  static void SerializeBinary(const _type & data, \
114  std::ostream & outputStream) \
115  throw (std::runtime_error) \
116  { \
117  outputStream.write(reinterpret_cast<const char *>(&data), \
118  sizeof(_type)); \
119  if (outputStream.fail()) { \
120  cmnThrow("cmnData::SerializeBinary(" #_type "): error occured with std::ostream::write"); \
121  } \
122  }
123 
124 
125 #define CMN_DATA_DE_SERIALIZE_BINARY_STREAM_USING_CAST_TO_CHAR_NO_BYTE_SWAP(_type) \
126  static void DeSerializeBinary(_type & data, \
127  std::istream & inputStream, \
128  const cmnDataFormat & CMN_UNUSED(localFormat), \
129  const cmnDataFormat & CMN_UNUSED(remoteFormat)) \
130  throw (std::runtime_error) \
131  { \
132  inputStream.read(reinterpret_cast<char *>(&data), \
133  sizeof(_type)); \
134  if (inputStream.fail()) { \
135  cmnThrow("cmnData::DeSerializeBinary(" #_type "): error occured with std::istream::read"); \
136  } \
137  }
138 
139 
140 #define CMN_DATA_DE_SERIALIZE_BINARY_STREAM_USING_CAST_TO_CHAR_AND_BYTE_SWAP(_type) \
141  static void DeSerializeBinary(_type & data, \
142  std::istream & inputStream, \
143  const cmnDataFormat & localFormat, \
144  const cmnDataFormat & remoteFormat) \
145  throw (std::runtime_error) \
146  { \
147  inputStream.read(reinterpret_cast<char *>(&data), \
148  sizeof(_type)); \
149  if (inputStream.fail()) { \
150  cmnThrow("cmnData::DeSerializeBinary(" #_type "): error occured with std::istream::read"); \
151  } \
152  if (remoteFormat.GetEndianness() != localFormat.GetEndianness()) { \
153  cmnDataByteSwap(data); \
154  } \
155  }
156 
157 
160 #define CMN_DATA_SERIALIZE_TEXT_USING_STREAM_OUT(_type) \
161  static void SerializeText(const _type & data, \
162  std::ostream & outputStream, \
163  const char CMN_UNUSED(delimiter) = ',') \
164  throw (std::runtime_error) \
165  { \
166  outputStream << data; \
167  if (outputStream.fail()) { \
168  cmnThrow("cmnData::SerializeText(" #_type "): error occured with std::ostream::write"); \
169  } \
170  }
171 
172 
175 #define CMN_DATA_DE_SERIALIZE_TEXT_USING_STREAM_IN(_type) \
176  static void DeSerializeText(_type & data, \
177  std::istream & inputStream, \
178  const char CMN_UNUSED(delimiter) = ',') \
179  throw (std::runtime_error) \
180  { \
181  inputStream >> data; \
182  if (inputStream.fail()) { \
183  cmnThrow("cmnData::DeSerializeText(" #_type "): error occured with std::istream::read"); \
184  } \
185  }
186 
187 
190 #define CMN_DATA_HUMAN_READABLE_USING_STREAM_OUT(_type) \
191  static std::string HumanReadable(const _type & data) \
192  { \
193  std::stringstream stringStream; \
194  stringStream << data; \
195  return stringStream.str(); \
196  }
197 
198 
202 #define CMN_DATA_SCALAR_DESCRIPTION(_type, _description) \
203  static std::string ScalarDescription(const _type & CMN_UNUSED(data), \
204  const size_t CMN_UNUSED(index), \
205  const std::string & userDescription = "") \
206  throw (std::out_of_range) { \
207  return (userDescription == "" ? "{"#_description"}" : (userDescription + ":{"#_description"}")); \
208  }
209 
211 #define CMN_DATA_SCALAR_USING_STATIC_CAST(_type) \
212  static double Scalar(const _type & data, const size_t CMN_UNUSED(index)) \
213  throw (std::out_of_range) { \
214  return static_cast<double>(data); \
215  }
216 
218 #define CMN_DATA_SCALAR_NUMBER_IS_ONE(_type) \
219  static size_t ScalarNumber(const _type & CMN_UNUSED(data)) { \
220  return 1; \
221  }
222 
223 #define CMN_DATA_SCALAR_NUMBER_IS_FIXED_TRUE(_type) \
224  static bool ScalarNumberIsFixed(const _type & CMN_UNUSED(data)) { \
225  return true; \
226  }
227 
228 #define CMN_DATA_IS_SPECIALIZED_TRUE(_type) \
229  enum {IS_SPECIALIZED = 1};
230 
231 #define CMN_DATA_SPECIALIZE_ALL_NO_BYTE_SWAP(type, description) \
232 template <> \
233 class cmnData<type> { \
234 public: \
235  CMN_DATA_IS_SPECIALIZED_TRUE(type) \
236  CMN_DATA_COPY_USING_ASSIGN(type) \
237  CMN_DATA_SERIALIZE_DESCRIPTION(type, description) \
238  CMN_DATA_SERIALIZE_BINARY_BYTE_SIZE_USING_SIZEOF(type) \
239  CMN_DATA_SERIALIZE_BINARY_BUFFER_USING_CAST_TO_CHAR(type) \
240  CMN_DATA_DE_SERIALIZE_BINARY_BUFFER_USING_CAST_TO_CHAR_NO_BYTE_SWAP(type) \
241  CMN_DATA_SERIALIZE_BINARY_STREAM_USING_CAST_TO_CHAR(type) \
242  CMN_DATA_DE_SERIALIZE_BINARY_STREAM_USING_CAST_TO_CHAR_NO_BYTE_SWAP(type) \
243  CMN_DATA_SERIALIZE_TEXT_USING_STREAM_OUT(type) \
244  CMN_DATA_DE_SERIALIZE_TEXT_USING_STREAM_IN(type) \
245  CMN_DATA_HUMAN_READABLE_USING_STREAM_OUT(type) \
246  CMN_DATA_SCALAR_DESCRIPTION(type, description) \
247  CMN_DATA_SCALAR_USING_STATIC_CAST(type) \
248  CMN_DATA_SCALAR_NUMBER_IS_ONE(type) \
249  CMN_DATA_SCALAR_NUMBER_IS_FIXED_TRUE(type) \
250  };
251 
252 #define CMN_DATA_SPECIALIZE_ALL_BYTE_SWAP(type, description) \
253 template <> \
254 class cmnData<type> { \
255 public: \
256  CMN_DATA_IS_SPECIALIZED_TRUE(type) \
257  CMN_DATA_COPY_USING_ASSIGN(type) \
258  CMN_DATA_SERIALIZE_DESCRIPTION(type, description) \
259  CMN_DATA_SERIALIZE_BINARY_BYTE_SIZE_USING_SIZEOF(type) \
260  CMN_DATA_SERIALIZE_BINARY_BUFFER_USING_CAST_TO_CHAR(type) \
261  CMN_DATA_DE_SERIALIZE_BINARY_BUFFER_USING_CAST_TO_CHAR_AND_BYTE_SWAP(type) \
262  CMN_DATA_SERIALIZE_BINARY_STREAM_USING_CAST_TO_CHAR(type) \
263  CMN_DATA_DE_SERIALIZE_BINARY_STREAM_USING_CAST_TO_CHAR_AND_BYTE_SWAP(type) \
264  CMN_DATA_SERIALIZE_TEXT_USING_STREAM_OUT(type) \
265  CMN_DATA_DE_SERIALIZE_TEXT_USING_STREAM_IN(type) \
266  CMN_DATA_HUMAN_READABLE_USING_STREAM_OUT(type) \
267  CMN_DATA_SCALAR_DESCRIPTION(type, description) \
268  CMN_DATA_SCALAR_USING_STATIC_CAST(type) \
269  CMN_DATA_SCALAR_NUMBER_IS_ONE(type) \
270  CMN_DATA_SCALAR_NUMBER_IS_FIXED_TRUE(type) \
271  };
272 
273 #endif // _cmnDataFunctionsMacros_h