cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmnDataFunctionsEnumMacros.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 _cmnDataFunctionsEnumMacros_h
24 #define _cmnDataFunctionsEnumMacros_h
25 
27 
28 
29 #define CMN_DATA_SPECIALIZATION_FOR_ENUM_USER_HUMAN_READABLE(_enum, _promotedType, _humanReadableFunction) \
30 template <> \
31 class cmnData<_enum> { \
32 public: \
33  enum {IS_SPECIALIZED = 1}; \
34  typedef _enum DataType; \
35  static void Copy(DataType & data, const _enum & source) { \
36  data = source; \
37  } \
38  static std::string HumanReadable(const DataType & data) { \
39  return _humanReadableFunction(data); \
40  } \
41  static void SerializeBinary(const DataType & data, \
42  std::ostream & outputStream) throw (std::runtime_error) { \
43  CMN_ASSERT(sizeof(_promotedType) >= sizeof(DataType)); \
44  const _promotedType dataPromoted = static_cast<_promotedType>(data); \
45  cmnData<_promotedType>::SerializeBinary(dataPromoted, outputStream); \
46  } \
47  static void DeSerializeBinary(DataType & data, std::istream & inputStream, \
48  const cmnDataFormat & localFormat, \
49  const cmnDataFormat & remoteFormat) throw (std::runtime_error) { \
50  CMN_ASSERT(sizeof(_promotedType) >= sizeof(DataType)); \
51  _promotedType dataPromoted; \
52  cmnData<_promotedType>::DeSerializeBinary(dataPromoted, inputStream, localFormat, remoteFormat); \
53  data = static_cast<DataType>(dataPromoted); \
54  } \
55  static void SerializeText(const DataType & data, std::ostream & outputStream, \
56  const char CMN_UNUSED(delimiter) = ',') throw (std::runtime_error) { \
57  CMN_ASSERT(sizeof(_promotedType) >= sizeof(DataType)); \
58  outputStream << static_cast<_promotedType>(data); \
59  } \
60  static std::string SerializeDescription(const DataType & CMN_UNUSED(data), \
61  const char CMN_UNUSED(delimiter) = ',', \
62  const std::string & userDescription = "") { \
63  return (userDescription == "") ? #_enum : (userDescription + #_enum); \
64  } \
65  static void DeSerializeText(DataType & data, std::istream & inputStream, \
66  const char CMN_UNUSED(delimiter) = ',') throw (std::runtime_error) { \
67  CMN_ASSERT(sizeof(_promotedType) >= sizeof(DataType)); \
68  _promotedType dataPromoted; \
69  inputStream >> dataPromoted; \
70  data = static_cast<DataType>(dataPromoted); \
71  } \
72  static bool ScalarNumberIsFixed(const DataType & CMN_UNUSED(data)) { \
73  return true; \
74  } \
75  static size_t ScalarNumber(const DataType & CMN_UNUSED(data)) { \
76  return 1; \
77  } \
78  static std::string ScalarDescription(const DataType & CMN_UNUSED(data), const size_t index, \
79  const std::string & userDescription) throw (std::out_of_range) { \
80  if (index == 0) { \
81  return (userDescription == "") ? #_enum : (userDescription + #_enum); \
82  } \
83  cmnThrow(std::out_of_range("cmnDataScalarDescription: " #_enum " index out of range")); \
84  return ""; \
85  } \
86  static double Scalar(const DataType & data, const size_t index) throw (std::out_of_range) { \
87  if (index == 0) { \
88  return static_cast<double>(data); \
89  } \
90  cmnThrow(std::out_of_range("cmnDataScalar: " #_enum " index out of range")); \
91  return 1.2345; \
92  } \
93 };
94 
95 
96 #define CMN_DATA_SPECIALIZATION_FOR_ENUM(_enum, _promotedType) \
97  CMN_DATA_SPECIALIZATION_FOR_ENUM_USER_HUMAN_READABLE(_enum, _promotedType, cmnData<int>::HumanReadable)
98 
99 
100 #define CMN_DECLARE_DATA_FUNCTIONS_JSON_FOR_ENUM(_enum) \
101  template <> void cmnDataJSON<_enum>::SerializeText(const _enum & data, Json::Value & jsonValue); \
102  template <> void cmnDataJSON<_enum>::DeSerializeText(_enum & data, const Json::Value & jsonValue) throw (std::runtime_error);
103 
104 #define CMN_IMPLEMENT_DATA_FUNCTIONS_JSON_FOR_ENUM(_enum, _promotedType) \
105  template <> void cmnDataJSON<_enum>::SerializeText(const _enum & data, Json::Value & jsonValue) { \
106  CMN_ASSERT(sizeof(_promotedType) >= sizeof(_enum)); \
107  const _promotedType dataPromoted = static_cast<_promotedType>(data); \
108  cmnDataJSON<_promotedType>::SerializeText(dataPromoted, jsonValue); \
109  } \
110  template <> void cmnDataJSON<_enum>::DeSerializeText(_enum & data, const Json::Value & jsonValue) throw (std::runtime_error) { \
111  CMN_ASSERT(sizeof(_promotedType) >= sizeof(_enum)); \
112  _promotedType dataPromoted; \
113  cmnDataJSON<_promotedType>::DeSerializeText(dataPromoted, jsonValue); \
114  data = static_cast<_enum>(dataPromoted); \
115  }
116 
117 #endif // _cmnDataFunctionsEnumMacros_h