Go to the documentation of this file.
23 #ifndef _cmnDataFunctionsEnumMacros_h
24 #define _cmnDataFunctionsEnumMacros_h
29 #define CMN_DATA_SPECIALIZATION_FOR_ENUM_USER_HUMAN_READABLE(_enum, _promotedType, _humanReadableFunction) \
31 class cmnData<_enum> { \
33 enum {IS_SPECIALIZED = 1}; \
34 typedef _enum DataType; \
35 static void Copy(DataType & data, const _enum & source) { \
38 static std::string HumanReadable(const DataType & data) { \
39 return _humanReadableFunction(data); \
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); \
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); \
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); \
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); \
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); \
72 static bool ScalarNumberIsFixed(const DataType & CMN_UNUSED(data)) { \
75 static size_t ScalarNumber(const DataType & CMN_UNUSED(data)) { \
78 static std::string ScalarDescription(const DataType & CMN_UNUSED(data), const size_t index, \
79 const std::string & userDescription) throw (std::out_of_range) { \
81 return (userDescription == "") ? #_enum : (userDescription + #_enum); \
83 cmnThrow(std::out_of_range("cmnDataScalarDescription: " #_enum " index out of range")); \
86 static double Scalar(const DataType & data, const size_t index) throw (std::out_of_range) { \
88 return static_cast<double>(data); \
90 cmnThrow(std::out_of_range("cmnDataScalar: " #_enum " index out of range")); \
96 #define CMN_DATA_SPECIALIZATION_FOR_ENUM(_enum, _promotedType) \
97 CMN_DATA_SPECIALIZATION_FOR_ENUM_USER_HUMAN_READABLE(_enum, _promotedType, cmnData<int>::HumanReadable)
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);
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); \
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); \
117 #endif // _cmnDataFunctionsEnumMacros_h