cisst-saw
Loading...
Searching...
No Matches
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 Author(s): Anton Deguet
6 Created on: 2011-06-27
7
8 (C) Copyright 2011-2024 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17*/
18
19#pragma once
20#ifndef _cmnDataFunctionsEnumMacros_h
21#define _cmnDataFunctionsEnumMacros_h
22
25
26#define CMN_DATA_SPECIALIZATION_FOR_ENUM_USER_HUMAN_READABLE(_enum, _promotedType, _humanReadableFunction) \
27template <> \
28class cmnData<_enum> { \
29public: \
30 enum {IS_SPECIALIZED = 1}; \
31 typedef _enum DataType; \
32 static void Copy(DataType & data, const _enum & source) { \
33 data = source; \
34 } \
35 static std::string HumanReadable(const DataType & data) { \
36 return _humanReadableFunction(data); \
37 } \
38 static void SerializeBinary(const DataType & data, \
39 std::ostream & outputStream) CISST_THROW(std::runtime_error) { \
40 CMN_ASSERT(sizeof(_promotedType) >= sizeof(DataType)); \
41 const _promotedType dataPromoted = static_cast<_promotedType>(data); \
42 cmnData<_promotedType>::SerializeBinary(dataPromoted, outputStream); \
43 } \
44 static void DeSerializeBinary(DataType & data, std::istream & inputStream, \
45 const cmnDataFormat & localFormat, \
46 const cmnDataFormat & remoteFormat) CISST_THROW(std::runtime_error) { \
47 CMN_ASSERT(sizeof(_promotedType) >= sizeof(DataType)); \
48 _promotedType dataPromoted; \
49 cmnData<_promotedType>::DeSerializeBinary(dataPromoted, inputStream, localFormat, remoteFormat); \
50 data = static_cast<DataType>(dataPromoted); \
51 } \
52 static void SerializeText(const DataType & data, std::ostream & outputStream, \
53 const char CMN_UNUSED(delimiter) = ',') CISST_THROW(std::runtime_error) { \
54 CMN_ASSERT(sizeof(_promotedType) >= sizeof(DataType)); \
55 outputStream << static_cast<_promotedType>(data); \
56 } \
57 static std::string SerializeDescription(const DataType & CMN_UNUSED(data), \
58 const char CMN_UNUSED(delimiter) = ',', \
59 const std::string & userDescription = "") { \
60 return (userDescription == "") ? #_enum : (userDescription + #_enum); \
61 } \
62 static void DeSerializeText(DataType & data, std::istream & inputStream, \
63 const char CMN_UNUSED(delimiter) = ',') CISST_THROW(std::runtime_error) { \
64 CMN_ASSERT(sizeof(_promotedType) >= sizeof(DataType)); \
65 _promotedType dataPromoted; \
66 inputStream >> dataPromoted; \
67 data = static_cast<DataType>(dataPromoted); \
68 } \
69 static bool ScalarNumberIsFixed(const DataType & CMN_UNUSED(data)) { \
70 return true; \
71 } \
72 static size_t ScalarNumber(const DataType & CMN_UNUSED(data)) { \
73 return 1; \
74 } \
75 static std::string ScalarDescription(const DataType & CMN_UNUSED(data), const size_t index, \
76 const std::string & userDescription) CISST_THROW(std::out_of_range) { \
77 if (index == 0) { \
78 return (userDescription == "") ? #_enum : (userDescription + #_enum); \
79 } \
80 cmnThrow(std::out_of_range("cmnDataScalarDescription: " #_enum " index out of range")); \
81 return ""; \
82 } \
83 static double Scalar(const DataType & data, const size_t index) CISST_THROW(std::out_of_range) { \
84 if (index == 0) { \
85 return static_cast<double>(data); \
86 } \
87 cmnThrow(std::out_of_range("cmnDataScalar: " #_enum " index out of range")); \
88 return 1.2345; \
89 } \
90};
91
92
93#define CMN_DATA_SPECIALIZATION_FOR_ENUM(_enum, _promotedType) \
94 CMN_DATA_SPECIALIZATION_FOR_ENUM_USER_HUMAN_READABLE(_enum, _promotedType, cmnData<int>::HumanReadable)
95
96
97#define CMN_DECLARE_DATA_FUNCTIONS_JSON_FOR_ENUM(_enum) \
98 template <> void cmnDataJSON<_enum>::SerializeText(const _enum & data, Json::Value & jsonValue); \
99 template <> void cmnDataJSON<_enum>::DeSerializeText(_enum & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error);
100
101#define CMN_DECLARE_DATA_FUNCTIONS_JSON_FOR_ENUM_EXPORT(_enum) \
102 template <> void CISST_EXPORT cmnDataJSON<_enum>::SerializeText(const _enum & data, Json::Value & jsonValue); \
103 template <> void CISST_EXPORT cmnDataJSON<_enum>::DeSerializeText(_enum & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error);
104
105#define CMN_IMPLEMENT_DATA_FUNCTIONS_JSON_FOR_ENUM(_enum, _promotedType) \
106 template <> void cmnDataJSON<_enum>::SerializeText(const _enum & data, Json::Value & jsonValue) { \
107 CMN_ASSERT(sizeof(_promotedType) >= sizeof(_enum)); \
108 const _promotedType dataPromoted = static_cast<_promotedType>(data); \
109 cmnDataJSON<_promotedType>::SerializeText(dataPromoted, jsonValue); \
110 } \
111 template <> void cmnDataJSON<_enum>::DeSerializeText(_enum & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error) { \
112 CMN_ASSERT(sizeof(_promotedType) >= sizeof(_enum)); \
113 _promotedType dataPromoted; \
114 cmnDataJSON<_promotedType>::DeSerializeText(dataPromoted, jsonValue); \
115 data = static_cast<_enum>(dataPromoted); \
116 }
117
118#define CMN_IMPLEMENT_DATA_FUNCTIONS_JSON_FOR_ENUM_AS_STRING(_enum, _to_string, _from_string) \
119 template <> void cmnDataJSON<_enum>::SerializeText(const _enum & data, Json::Value & jsonValue) { \
120 const std::string _s = _to_string(data); \
121 cmnDataJSON<std::string>::SerializeText(_s, jsonValue); \
122 } \
123 template <> void cmnDataJSON<_enum>::DeSerializeText(_enum & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error) { \
124 std::string _s; \
125 cmnDataJSON<std::string>::DeSerializeText(_s, jsonValue); \
126 data = _from_string(_s); \
127 }
128
129#endif // _cmnDataFunctionsEnumMacros_h
Assert macros definitions.