Go to the documentation of this file.
   23 #ifndef _cmnDataFunctionsMacros_h 
   24 #define _cmnDataFunctionsMacros_h 
   33 #define CMN_DATA_COPY_USING_ASSIGN(_type)                               \ 
   34     static void Copy(_type & data,                                      \ 
   35                      const _type & source) {                            \ 
   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 = "") \ 
   46         return (userDescription == "" ? "{"#_description"}" : (userDescription + ":{"#_description"}")); \ 
   50 #define CMN_DATA_SERIALIZE_BINARY_BYTE_SIZE_USING_SIZEOF(_type)         \ 
   51     static size_t SerializeBinaryByteSize(const _type & CMN_UNUSED(data)) \ 
   53         return sizeof(_type);                                           \ 
   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)     \ 
   66         const size_t sizeOfData = cmnData<_type>::SerializeBinaryByteSize(data); \ 
   67         if (bufferSize < sizeOfData) {                                  \ 
   70         *(reinterpret_cast<_type *>(buffer)) = data;                    \ 
   71         bufferSize -= sizeOfData;                                       \ 
   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)) \ 
   80         const size_t sizeOfData = cmnData<_type>::SerializeBinaryByteSize(data); \ 
   81         if (bufferSize < sizeOfData) {                                  \ 
   84         data = *(reinterpret_cast<const _type *>(buffer));              \ 
   85         bufferSize -= sizeOfData;                                       \ 
   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) \ 
   94         const size_t sizeOfData = cmnData<_type>::SerializeBinaryByteSize(data); \ 
   95         if (bufferSize < sizeOfData) {                                  \ 
   98         data = *(reinterpret_cast<const _type *>(buffer));              \ 
   99         if (remoteFormat.GetEndianness() != localFormat.GetEndianness()) { \ 
  100             cmnDataByteSwap(data);                                      \ 
  102         bufferSize -= sizeOfData;                                       \ 
  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)                                      \ 
  117         outputStream.write(reinterpret_cast<const char *>(&data),       \ 
  119         if (outputStream.fail()) {                                      \ 
  120             cmnThrow("cmnData::SerializeBinary(" #_type "): error occured with std::ostream::write"); \ 
  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)                                      \ 
  132         inputStream.read(reinterpret_cast<char *>(&data),               \ 
  134         if (inputStream.fail()) {                                       \ 
  135             cmnThrow("cmnData::DeSerializeBinary(" #_type "): error occured with std::istream::read"); \ 
  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)                                      \ 
  147         inputStream.read(reinterpret_cast<char *>(&data),               \ 
  149         if (inputStream.fail()) {                                       \ 
  150             cmnThrow("cmnData::DeSerializeBinary(" #_type "): error occured with std::istream::read"); \ 
  152         if (remoteFormat.GetEndianness() != localFormat.GetEndianness()) { \ 
  153             cmnDataByteSwap(data);                                      \ 
  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)                                      \ 
  166         outputStream << data;                                           \ 
  167         if (outputStream.fail()) {                                      \ 
  168             cmnThrow("cmnData::SerializeText(" #_type "): error occured with std::ostream::write"); \ 
  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)                                      \ 
  181         inputStream >> data;                                            \ 
  182         if (inputStream.fail()) {                                       \ 
  183             cmnThrow("cmnData::DeSerializeText(" #_type "): error occured with std::istream::read"); \ 
  190 #define CMN_DATA_HUMAN_READABLE_USING_STREAM_OUT(_type)                 \ 
  191     static std::string HumanReadable(const _type & data)                \ 
  193         std::stringstream stringStream;                                 \ 
  194         stringStream << data;                                           \ 
  195         return stringStream.str();                                      \ 
  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"}")); \ 
  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);                               \ 
  218 #define CMN_DATA_SCALAR_NUMBER_IS_ONE(_type)                            \ 
  219     static size_t ScalarNumber(const _type & CMN_UNUSED(data)) {        \ 
  223 #define CMN_DATA_SCALAR_NUMBER_IS_FIXED_TRUE(_type)                     \ 
  224     static bool ScalarNumberIsFixed(const _type & CMN_UNUSED(data)) {   \ 
  228 #define CMN_DATA_IS_SPECIALIZED_TRUE(_type)     \ 
  229     enum {IS_SPECIALIZED = 1}; 
  231 #define CMN_DATA_SPECIALIZE_ALL_NO_BYTE_SWAP(type, description)         \ 
  233 class cmnData<type> {                                                   \ 
  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)                          \ 
  252 #define CMN_DATA_SPECIALIZE_ALL_BYTE_SWAP(type, description)            \ 
  254 class cmnData<type> {                                                   \ 
  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)                          \ 
  273 #endif // _cmnDataFunctionsMacros_h