Skip to content

Commit 74be278

Browse files
committed
De-macro helpers.
1 parent c23093e commit 74be278

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

libsolutil/JSON.h

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -73,36 +73,33 @@ namespace detail
7373
template<typename T>
7474
struct helper;
7575

76-
#define DEFINE_JSON_CONVERSION_HELPERS(TYPE, CHECK_TYPE_MEMBER, CONVERT_TYPE_MEMBER) \
77-
template<> \
78-
struct helper<TYPE> \
79-
{ \
80-
static bool isOfType(Json::Value const& _input) \
81-
{ \
82-
return _input.CHECK_TYPE_MEMBER(); \
83-
} \
84-
static TYPE get(Json::Value const& _input) \
85-
{ \
86-
return _input.CONVERT_TYPE_MEMBER(); \
87-
} \
88-
static TYPE getOrDefault(Json::Value const& _input, TYPE _default = {}) \
89-
{ \
90-
TYPE result = _default; \
91-
if (helper::isOfType(_input)) \
92-
result = _input.CONVERT_TYPE_MEMBER(); \
93-
return result; \
94-
} \
95-
};
96-
97-
DEFINE_JSON_CONVERSION_HELPERS(float, isDouble, asFloat)
98-
DEFINE_JSON_CONVERSION_HELPERS(double, isDouble, asDouble)
99-
DEFINE_JSON_CONVERSION_HELPERS(std::string, isString, asString)
100-
DEFINE_JSON_CONVERSION_HELPERS(Json::Int, isInt, asInt)
101-
DEFINE_JSON_CONVERSION_HELPERS(Json::Int64, isInt64, asInt64)
102-
DEFINE_JSON_CONVERSION_HELPERS(Json::UInt, isUInt, asUInt)
103-
DEFINE_JSON_CONVERSION_HELPERS(Json::UInt64, isUInt64, asUInt64)
76+
template<typename T, bool(Json::Value::*checkMember)() const, T(Json::Value::*convertMember)() const>
77+
struct helper_impl
78+
{
79+
static bool isOfType(Json::Value const& _input)
80+
{
81+
return (_input.*checkMember)();
82+
}
83+
static T get(Json::Value const& _input)
84+
{
85+
return (_input.*convertMember)();
86+
}
87+
static T getOrDefault(Json::Value const& _input, T _default = {})
88+
{
89+
T result = _default;
90+
if (isOfType(_input))
91+
result = (_input.*convertMember)();
92+
return result;
93+
}
94+
};
10495

105-
#undef DEFINE_JSON_CONVERSION_HELPERS
96+
template<> struct helper<float>: helper_impl<float, &Json::Value::isDouble, &Json::Value::asFloat> {};
97+
template<> struct helper<double>: helper_impl<double, &Json::Value::isDouble, &Json::Value::asDouble> {};
98+
template<> struct helper<std::string>: helper_impl<std::string, &Json::Value::isString, &Json::Value::asString> {};
99+
template<> struct helper<Json::Int>: helper_impl<Json::Int, &Json::Value::isInt, &Json::Value::asInt> {};
100+
template<> struct helper<Json::Int64>: helper_impl<Json::Int64, &Json::Value::isInt64, &Json::Value::asInt64> {};
101+
template<> struct helper<Json::UInt>: helper_impl<Json::UInt, &Json::Value::isUInt, &Json::Value::asUInt> {};
102+
template<> struct helper<Json::UInt64>: helper_impl<Json::UInt64, &Json::Value::isUInt64, &Json::Value::asUInt64> {};
106103

107104
} // namespace detail
108105

0 commit comments

Comments
 (0)