From 466b4ea11e1064e72b4866a62f46db4749ed06ea Mon Sep 17 00:00:00 2001 From: Julian van Doorn Date: Mon, 28 Oct 2019 17:23:35 +0100 Subject: [PATCH 1/6] Implemented as() and is() with accompanying tests --- include/json/value.h | 61 ++++++++++++++++++++ src/test_lib_json/main.cpp | 110 +++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) diff --git a/include/json/value.h b/include/json/value.h index d0af7116b..ce6bfcd56 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -399,6 +399,40 @@ class JSON_API Value { double asDouble() const; bool asBool() const; + template + struct asLookupHelper; + + #define defAsLookupHelper(type, lookup) \ + template<> \ + struct asLookupHelper< type > { \ + static type as(const Value& val) { \ + return val. lookup (); \ + } \ + } + + defAsLookupHelper(const char*, asCString); + defAsLookupHelper(String, asString); + #ifdef JSON_USE_CPPTL + defAsLookupHelper(CppTL::ConstString, asConstString); + #endif + defAsLookupHelper(Int, asInt); + defAsLookupHelper(UInt, asUInt); + #if defined(JSON_HAS_INT64) + defAsLookupHelper(Int64, asInt64); + defAsLookupHelper(UInt64, asUInt64); + #endif // if defined(JSON_HAS_INT64) + // (U)LargestInt is a type alias of int or int64 and thus cannot be defined + defAsLookupHelper(float, asFloat); + defAsLookupHelper(double, asDouble); + defAsLookupHelper(bool, asBool); + + #undef defAsLookupHelper + + template + T as() const { + return asLookupHelper::as(*this); + } + bool isNull() const; bool isBool() const; bool isInt() const; @@ -412,6 +446,33 @@ class JSON_API Value { bool isArray() const; bool isObject() const; + template + struct isLookupHelper; + + #define defIsLookupHelper(type, lookup) \ + template<> \ + struct isLookupHelper< type > { \ + static bool is(const Value& val) { \ + return val. lookup (); \ + } \ + } + + defIsLookupHelper(bool, isBool); + defIsLookupHelper(Int, isInt); + defIsLookupHelper(Int64, isInt64); + defIsLookupHelper(UInt, isUInt); + defIsLookupHelper(UInt64, isUInt64); + defIsLookupHelper(double, isDouble); + defIsLookupHelper(const char*, isString); + defIsLookupHelper(String, isString); + + #undef defIsLookupHelper + + template + bool is() const { + return isLookupHelper::is(*this); + } + bool isConvertibleTo(ValueType other) const; /// Number of values in array or object diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 1d636d8a0..e1749ce5e 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -3361,6 +3361,116 @@ int main(int argc, const char* argv[]) { return runner.runCommandLine(argc, argv); } +struct TemplatedAs : JsonTest::TestCase {}; + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorCString) { + Json::Value json = "hello world"; + JSONTEST_ASSERT_STRING_EQUAL(json.asCString(), json.as()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorString) { + Json::Value json = "hello world"; + JSONTEST_ASSERT_STRING_EQUAL(json.asString(), json.as()); +} + +#ifdef JSON_USE_CPPTL +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorConstString) { + Json::Value json = "hello world"; + JSONTEST_ASSERT_STRING_EQUAL(json.asConstString(), json.as()); +} +#endif + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorInt) { + Json::Value json = Json::Int(64); + JSONTEST_ASSERT_EQUAL(json.asInt(), json.as()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorUInt) { + Json::Value json = Json::UInt(64); + JSONTEST_ASSERT_EQUAL(json.asUInt(), json.as()); +} + +#if defined(JSON_HAS_INT64) +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorInt64) { + Json::Value json = Json::Int64(64); + JSONTEST_ASSERT_EQUAL(json.asUInt64(), json.as()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorUInt64) { + Json::Value json = Json::UInt64(64); + JSONTEST_ASSERT_EQUAL(json.asUInt64(), json.as()); +} +#endif // if defined(JSON_HAS_INT64) + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorLargestInt) { + Json::Value json = Json::LargestInt(64); + JSONTEST_ASSERT_EQUAL(json.asLargestInt(), json.as()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorLargestUInt) { + Json::Value json = Json::LargestUInt(64); + JSONTEST_ASSERT_EQUAL(json.asLargestUInt(), json.as()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorFloat) { + Json::Value json = float(69.69); + JSONTEST_ASSERT_EQUAL(json.asFloat(), json.as()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorDouble) { + Json::Value json = double(69.69); + JSONTEST_ASSERT_EQUAL(json.asDouble(), json.as()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorBool) { + Json::Value jsonTrue = true; + Json::Value jsonFalse = false; + JSONTEST_ASSERT_EQUAL(jsonTrue.asBool(), jsonTrue.as()); + JSONTEST_ASSERT_EQUAL(jsonFalse.asBool(), jsonFalse.as()); +} + +struct TemplatedIs : JsonTest::TestCase {}; + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsBool) { + Json::Value json = true; + JSONTEST_ASSERT_EQUAL(json.isBool(), json.is()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsInt) { + Json::Value json = 142; + JSONTEST_ASSERT_EQUAL(json.isInt(), json.is()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsInt64) { + Json::Value json = 142; + JSONTEST_ASSERT_EQUAL(json.isInt64(), json.is()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsUInt) { + Json::Value json = 142; + JSONTEST_ASSERT_EQUAL(json.isUInt(), json.is()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsUInt64) { + Json::Value json = 142; + JSONTEST_ASSERT_EQUAL(json.isUInt64(), json.is()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsDouble) { + Json::Value json = 40.63; + JSONTEST_ASSERT_EQUAL(json.isDouble(), json.is()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsCString) { + Json::Value json = "hello world"; + JSONTEST_ASSERT_EQUAL(json.isString(), json.is()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsString) { + Json::Value json = "hello world"; + JSONTEST_ASSERT_EQUAL(json.isString(), json.is()); +} + #if defined(__GNUC__) #pragma GCC diagnostic pop #endif From 435a32a5e3966745a9e1b3bfcd78c1eddcd733dd Mon Sep 17 00:00:00 2001 From: Julian van Doorn Date: Tue, 29 Oct 2019 10:54:52 +0100 Subject: [PATCH 2/6] Formatted value.h --- include/json/value.h | 48 ++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index ce6bfcd56..4d74b008b 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -399,39 +399,32 @@ class JSON_API Value { double asDouble() const; bool asBool() const; - template - struct asLookupHelper; - - #define defAsLookupHelper(type, lookup) \ - template<> \ - struct asLookupHelper< type > { \ - static type as(const Value& val) { \ - return val. lookup (); \ - } \ + template struct asLookupHelper; + +#define defAsLookupHelper(type, lookup) \ + template <> struct asLookupHelper { \ + static type as(const Value& val) { return val.lookup(); } \ } defAsLookupHelper(const char*, asCString); defAsLookupHelper(String, asString); - #ifdef JSON_USE_CPPTL +#ifdef JSON_USE_CPPTL defAsLookupHelper(CppTL::ConstString, asConstString); - #endif +#endif defAsLookupHelper(Int, asInt); defAsLookupHelper(UInt, asUInt); - #if defined(JSON_HAS_INT64) +#if defined(JSON_HAS_INT64) defAsLookupHelper(Int64, asInt64); defAsLookupHelper(UInt64, asUInt64); - #endif // if defined(JSON_HAS_INT64) +#endif // if defined(JSON_HAS_INT64) // (U)LargestInt is a type alias of int or int64 and thus cannot be defined defAsLookupHelper(float, asFloat); defAsLookupHelper(double, asDouble); defAsLookupHelper(bool, asBool); - #undef defAsLookupHelper +#undef defAsLookupHelper - template - T as() const { - return asLookupHelper::as(*this); - } + template T as() const { return asLookupHelper::as(*this); } bool isNull() const; bool isBool() const; @@ -446,15 +439,11 @@ class JSON_API Value { bool isArray() const; bool isObject() const; - template - struct isLookupHelper; + template struct isLookupHelper; - #define defIsLookupHelper(type, lookup) \ - template<> \ - struct isLookupHelper< type > { \ - static bool is(const Value& val) { \ - return val. lookup (); \ - } \ +#define defIsLookupHelper(type, lookup) \ + template <> struct isLookupHelper { \ + static bool is(const Value& val) { return val.lookup(); } \ } defIsLookupHelper(bool, isBool); @@ -466,12 +455,9 @@ class JSON_API Value { defIsLookupHelper(const char*, isString); defIsLookupHelper(String, isString); - #undef defIsLookupHelper +#undef defIsLookupHelper - template - bool is() const { - return isLookupHelper::is(*this); - } + template bool is() const { return isLookupHelper::is(*this); } bool isConvertibleTo(ValueType other) const; From a2117e01d3e527a1421deddde3e7125b21a9a008 Mon Sep 17 00:00:00 2001 From: Julian van Doorn Date: Tue, 29 Oct 2019 11:01:20 +0100 Subject: [PATCH 3/6] Formatted tests --- src/test_lib_json/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index e1749ce5e..b5f0e604b 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -3376,7 +3376,8 @@ JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorString) { #ifdef JSON_USE_CPPTL JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorConstString) { Json::Value json = "hello world"; - JSONTEST_ASSERT_STRING_EQUAL(json.asConstString(), json.as()); + JSONTEST_ASSERT_STRING_EQUAL(json.asConstString(), + json.as()); } #endif From 4e02630e32b17be32fa19ec147c0c25bf5cd75c3 Mon Sep 17 00:00:00 2001 From: Billy Donahue Date: Sun, 3 Nov 2019 11:40:34 -0500 Subject: [PATCH 4/6] Remove macros and prune some overloads. (#1066) Combine is tests and combine as tests. --- include/json/value.h | 74 +++++++++---------- src/test_lib_json/main.cpp | 144 +++++++++++-------------------------- 2 files changed, 72 insertions(+), 146 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index 4d74b008b..2f5bfc5e3 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -399,33 +399,6 @@ class JSON_API Value { double asDouble() const; bool asBool() const; - template struct asLookupHelper; - -#define defAsLookupHelper(type, lookup) \ - template <> struct asLookupHelper { \ - static type as(const Value& val) { return val.lookup(); } \ - } - - defAsLookupHelper(const char*, asCString); - defAsLookupHelper(String, asString); -#ifdef JSON_USE_CPPTL - defAsLookupHelper(CppTL::ConstString, asConstString); -#endif - defAsLookupHelper(Int, asInt); - defAsLookupHelper(UInt, asUInt); -#if defined(JSON_HAS_INT64) - defAsLookupHelper(Int64, asInt64); - defAsLookupHelper(UInt64, asUInt64); -#endif // if defined(JSON_HAS_INT64) - // (U)LargestInt is a type alias of int or int64 and thus cannot be defined - defAsLookupHelper(float, asFloat); - defAsLookupHelper(double, asDouble); - defAsLookupHelper(bool, asBool); - -#undef defAsLookupHelper - - template T as() const { return asLookupHelper::as(*this); } - bool isNull() const; bool isBool() const; bool isInt() const; @@ -439,25 +412,42 @@ class JSON_API Value { bool isArray() const; bool isObject() const; - template struct isLookupHelper; + /// The `as` and `is` member function templates and specializations. + template T as() const = delete; + template bool is() const = delete; -#define defIsLookupHelper(type, lookup) \ - template <> struct isLookupHelper { \ - static bool is(const Value& val) { return val.lookup(); } \ - } + template <> bool as() const { return asBool(); } + template <> bool is() const { return isBool(); } + + template <> Int as() const { return asInt(); } + template <> bool is() const { return isInt(); } + + template <> UInt as() const { return asUInt(); } + template <> bool is() const { return isUInt(); } + +#if defined(JSON_HAS_INT64) + template <> Int64 as() const { return asInt64(); } + template <> bool is() const { return isInt64(); } - defIsLookupHelper(bool, isBool); - defIsLookupHelper(Int, isInt); - defIsLookupHelper(Int64, isInt64); - defIsLookupHelper(UInt, isUInt); - defIsLookupHelper(UInt64, isUInt64); - defIsLookupHelper(double, isDouble); - defIsLookupHelper(const char*, isString); - defIsLookupHelper(String, isString); + template <> UInt64 as() const { return asUInt64(); } + template <> bool is() const { return isUInt64(); } +#endif + + template <> double as() const { return asDouble(); } + template <> bool is() const { return isDouble(); } -#undef defIsLookupHelper + template <> String as() const { return asString(); } + template <> bool is() const { return isString(); } - template bool is() const { return isLookupHelper::is(*this); } + /// These `as` specializations are type conversions, and do not have a + /// corresponding `is`. + template <> float as() const { return asFloat(); } + template <> const char* as() const { return asCString(); } +#ifdef JSON_USE_CPPTL + template <> CppTL::ConstString as() const { + return asConstString(); + } +#endif bool isConvertibleTo(ValueType other) const; diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index b5f0e604b..146aa9abc 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -3361,115 +3361,51 @@ int main(int argc, const char* argv[]) { return runner.runCommandLine(argc, argv); } -struct TemplatedAs : JsonTest::TestCase {}; - -JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorCString) { - Json::Value json = "hello world"; - JSONTEST_ASSERT_STRING_EQUAL(json.asCString(), json.as()); -} - -JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorString) { - Json::Value json = "hello world"; - JSONTEST_ASSERT_STRING_EQUAL(json.asString(), json.as()); -} +struct MemberTemplateAs : JsonTest::TestCase { + template + JsonTest::TestResult& EqEval(T v, F f) const { + const Json::Value j = v; + return JSONTEST_ASSERT_EQUAL(j.as(), f(j)); + } +}; +JSONTEST_FIXTURE_LOCAL(MemberTemplateAs, BehavesSameAsNamedAs) { + const Json::Value jstr = "hello world"; + JSONTEST_ASSERT_STRING_EQUAL(jstr.as(), jstr.asCString()); + JSONTEST_ASSERT_STRING_EQUAL(jstr.as(), jstr.asString()); #ifdef JSON_USE_CPPTL -JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorConstString) { - Json::Value json = "hello world"; - JSONTEST_ASSERT_STRING_EQUAL(json.asConstString(), - json.as()); -} + JSONTEST_ASSERT_STRING_EQUAL(js.as(), js.asConstString()); #endif - -JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorInt) { - Json::Value json = Json::Int(64); - JSONTEST_ASSERT_EQUAL(json.asInt(), json.as()); -} - -JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorUInt) { - Json::Value json = Json::UInt(64); - JSONTEST_ASSERT_EQUAL(json.asUInt(), json.as()); -} - + EqEval(Json::Int(64), [](const Json::Value& j) { return j.asInt(); }); + EqEval(Json::UInt(64), [](const Json::Value& j) { return j.asUInt(); }); #if defined(JSON_HAS_INT64) -JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorInt64) { - Json::Value json = Json::Int64(64); - JSONTEST_ASSERT_EQUAL(json.asUInt64(), json.as()); -} - -JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorUInt64) { - Json::Value json = Json::UInt64(64); - JSONTEST_ASSERT_EQUAL(json.asUInt64(), json.as()); -} + EqEval(Json::Int64(64), [](const Json::Value& j) { return j.asInt64(); }); + EqEval(Json::UInt64(64), [](const Json::Value& j) { return j.asUInt64(); }); #endif // if defined(JSON_HAS_INT64) - -JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorLargestInt) { - Json::Value json = Json::LargestInt(64); - JSONTEST_ASSERT_EQUAL(json.asLargestInt(), json.as()); -} - -JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorLargestUInt) { - Json::Value json = Json::LargestUInt(64); - JSONTEST_ASSERT_EQUAL(json.asLargestUInt(), json.as()); -} - -JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorFloat) { - Json::Value json = float(69.69); - JSONTEST_ASSERT_EQUAL(json.asFloat(), json.as()); -} - -JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorDouble) { - Json::Value json = double(69.69); - JSONTEST_ASSERT_EQUAL(json.asDouble(), json.as()); -} - -JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorBool) { - Json::Value jsonTrue = true; - Json::Value jsonFalse = false; - JSONTEST_ASSERT_EQUAL(jsonTrue.asBool(), jsonTrue.as()); - JSONTEST_ASSERT_EQUAL(jsonFalse.asBool(), jsonFalse.as()); -} - -struct TemplatedIs : JsonTest::TestCase {}; - -JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsBool) { - Json::Value json = true; - JSONTEST_ASSERT_EQUAL(json.isBool(), json.is()); -} - -JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsInt) { - Json::Value json = 142; - JSONTEST_ASSERT_EQUAL(json.isInt(), json.is()); -} - -JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsInt64) { - Json::Value json = 142; - JSONTEST_ASSERT_EQUAL(json.isInt64(), json.is()); -} - -JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsUInt) { - Json::Value json = 142; - JSONTEST_ASSERT_EQUAL(json.isUInt(), json.is()); -} - -JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsUInt64) { - Json::Value json = 142; - JSONTEST_ASSERT_EQUAL(json.isUInt64(), json.is()); -} - -JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsDouble) { - Json::Value json = 40.63; - JSONTEST_ASSERT_EQUAL(json.isDouble(), json.is()); -} - -JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsCString) { - Json::Value json = "hello world"; - JSONTEST_ASSERT_EQUAL(json.isString(), json.is()); -} - -JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsString) { - Json::Value json = "hello world"; - JSONTEST_ASSERT_EQUAL(json.isString(), json.is()); + EqEval(Json::LargestInt(64), + [](const Json::Value& j) { return j.asLargestInt(); }); + EqEval(Json::LargestUInt(64), + [](const Json::Value& j) { return j.asLargestUInt(); }); + + EqEval(69.69f, [](const Json::Value& j) { return j.asFloat(); }); + EqEval(69.69, [](const Json::Value& j) { return j.asDouble(); }); + EqEval(false, [](const Json::Value& j) { return j.asBool(); }); + EqEval(true, [](const Json::Value& j) { return j.asBool(); }); +} + +class MemberTemplateIs : public JsonTest::TestCase {}; + +JSONTEST_FIXTURE_LOCAL(MemberTemplateIs, BehavesSameAsNamedIs) { + const Json::Value values[] = {true, 142, 40.63, "hello world"}; + for (const Json::Value& j : values) { + JSONTEST_ASSERT_EQUAL(j.is(), j.isBool()); + JSONTEST_ASSERT_EQUAL(j.is(), j.isInt()); + JSONTEST_ASSERT_EQUAL(j.is(), j.isInt64()); + JSONTEST_ASSERT_EQUAL(j.is(), j.isUInt()); + JSONTEST_ASSERT_EQUAL(j.is(), j.isUInt64()); + JSONTEST_ASSERT_EQUAL(j.is(), j.isDouble()); + JSONTEST_ASSERT_EQUAL(j.is(), j.isString()); + } } #if defined(__GNUC__) From 826228e738660d2d959b9b1422644bc201358a66 Mon Sep 17 00:00:00 2001 From: Billy Donahue Date: Sat, 9 Nov 2019 12:00:35 -0500 Subject: [PATCH 5/6] move template specializations out of class scope --- include/json/value.h | 67 ++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index 2f5bfc5e3..3435a18cb 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -416,39 +416,6 @@ class JSON_API Value { template T as() const = delete; template bool is() const = delete; - template <> bool as() const { return asBool(); } - template <> bool is() const { return isBool(); } - - template <> Int as() const { return asInt(); } - template <> bool is() const { return isInt(); } - - template <> UInt as() const { return asUInt(); } - template <> bool is() const { return isUInt(); } - -#if defined(JSON_HAS_INT64) - template <> Int64 as() const { return asInt64(); } - template <> bool is() const { return isInt64(); } - - template <> UInt64 as() const { return asUInt64(); } - template <> bool is() const { return isUInt64(); } -#endif - - template <> double as() const { return asDouble(); } - template <> bool is() const { return isDouble(); } - - template <> String as() const { return asString(); } - template <> bool is() const { return isString(); } - - /// These `as` specializations are type conversions, and do not have a - /// corresponding `is`. - template <> float as() const { return asFloat(); } - template <> const char* as() const { return asCString(); } -#ifdef JSON_USE_CPPTL - template <> CppTL::ConstString as() const { - return asConstString(); - } -#endif - bool isConvertibleTo(ValueType other) const; /// Number of values in array or object @@ -710,6 +677,40 @@ class JSON_API Value { ptrdiff_t limit_; }; +template <> inline bool Value::as() const { return asBool(); } +template <> inline bool Value::is() const { return isBool(); } + +template <> inline Int Value::as() const { return asInt(); } +template <> inline bool Value::is() const { return isInt(); } + +template <> inline UInt Value::as() const { return asUInt(); } +template <> inline bool Value::is() const { return isUInt(); } + +#if defined(JSON_HAS_INT64) +template <> inline Int64 Value::as() const { return asInt64(); } +template <> inline bool Value::is() const { return isInt64(); } + +template <> inline UInt64 Value::as() const { return asUInt64(); } +template <> inline bool Value::is() const { return isUInt64(); } +#endif + +template <> inline double Value::as() const { return asDouble(); } +template <> inline bool Value::is() const { return isDouble(); } + +template <> inline String Value::as() const { return asString(); } +template <> inline bool Value::is() const { return isString(); } + +/// These `as` specializations are type conversions, and do not have a +/// corresponding `is`. +template <> inline float Value::as() const { return asFloat(); } +template <> inline const char* Value::as() const { return asCString(); } +#ifdef JSON_USE_CPPTL +template <> inline CppTL::ConstString Value::as() const { + return asConstString(); +} +#endif + + /** \brief Experimental and untested: represents an element of the "path" to * access a node. */ From 15319ad9fd7d4fd1077c2072d4a611a3505a04fe Mon Sep 17 00:00:00 2001 From: Billy Donahue Date: Sat, 9 Nov 2019 12:08:57 -0500 Subject: [PATCH 6/6] format --- include/json/value.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index 3435a18cb..bf4f9c443 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -703,14 +703,15 @@ template <> inline bool Value::is() const { return isString(); } /// These `as` specializations are type conversions, and do not have a /// corresponding `is`. template <> inline float Value::as() const { return asFloat(); } -template <> inline const char* Value::as() const { return asCString(); } +template <> inline const char* Value::as() const { + return asCString(); +} #ifdef JSON_USE_CPPTL template <> inline CppTL::ConstString Value::as() const { return asConstString(); } #endif - /** \brief Experimental and untested: represents an element of the "path" to * access a node. */