Skip to content

Commit 4e02630

Browse files
committed
Remove macros and prune some overloads. (#1066)
Combine is<T> tests and combine as<T> tests.
1 parent a2117e0 commit 4e02630

File tree

2 files changed

+72
-146
lines changed

2 files changed

+72
-146
lines changed

include/json/value.h

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -399,33 +399,6 @@ class JSON_API Value {
399399
double asDouble() const;
400400
bool asBool() const;
401401

402-
template <class T> struct asLookupHelper;
403-
404-
#define defAsLookupHelper(type, lookup) \
405-
template <> struct asLookupHelper<type> { \
406-
static type as(const Value& val) { return val.lookup(); } \
407-
}
408-
409-
defAsLookupHelper(const char*, asCString);
410-
defAsLookupHelper(String, asString);
411-
#ifdef JSON_USE_CPPTL
412-
defAsLookupHelper(CppTL::ConstString, asConstString);
413-
#endif
414-
defAsLookupHelper(Int, asInt);
415-
defAsLookupHelper(UInt, asUInt);
416-
#if defined(JSON_HAS_INT64)
417-
defAsLookupHelper(Int64, asInt64);
418-
defAsLookupHelper(UInt64, asUInt64);
419-
#endif // if defined(JSON_HAS_INT64)
420-
// (U)LargestInt is a type alias of int or int64 and thus cannot be defined
421-
defAsLookupHelper(float, asFloat);
422-
defAsLookupHelper(double, asDouble);
423-
defAsLookupHelper(bool, asBool);
424-
425-
#undef defAsLookupHelper
426-
427-
template <class T> T as() const { return asLookupHelper<T>::as(*this); }
428-
429402
bool isNull() const;
430403
bool isBool() const;
431404
bool isInt() const;
@@ -439,25 +412,42 @@ class JSON_API Value {
439412
bool isArray() const;
440413
bool isObject() const;
441414

442-
template <class T> struct isLookupHelper;
415+
/// The `as<T>` and `is<T>` member function templates and specializations.
416+
template <typename T> T as() const = delete;
417+
template <typename T> bool is() const = delete;
443418

444-
#define defIsLookupHelper(type, lookup) \
445-
template <> struct isLookupHelper<type> { \
446-
static bool is(const Value& val) { return val.lookup(); } \
447-
}
419+
template <> bool as<bool>() const { return asBool(); }
420+
template <> bool is<bool>() const { return isBool(); }
421+
422+
template <> Int as<Int>() const { return asInt(); }
423+
template <> bool is<Int>() const { return isInt(); }
424+
425+
template <> UInt as<UInt>() const { return asUInt(); }
426+
template <> bool is<UInt>() const { return isUInt(); }
427+
428+
#if defined(JSON_HAS_INT64)
429+
template <> Int64 as<Int64>() const { return asInt64(); }
430+
template <> bool is<Int64>() const { return isInt64(); }
448431

449-
defIsLookupHelper(bool, isBool);
450-
defIsLookupHelper(Int, isInt);
451-
defIsLookupHelper(Int64, isInt64);
452-
defIsLookupHelper(UInt, isUInt);
453-
defIsLookupHelper(UInt64, isUInt64);
454-
defIsLookupHelper(double, isDouble);
455-
defIsLookupHelper(const char*, isString);
456-
defIsLookupHelper(String, isString);
432+
template <> UInt64 as<UInt64>() const { return asUInt64(); }
433+
template <> bool is<UInt64>() const { return isUInt64(); }
434+
#endif
435+
436+
template <> double as<double>() const { return asDouble(); }
437+
template <> bool is<double>() const { return isDouble(); }
457438

458-
#undef defIsLookupHelper
439+
template <> String as<String>() const { return asString(); }
440+
template <> bool is<String>() const { return isString(); }
459441

460-
template <class T> bool is() const { return isLookupHelper<T>::is(*this); }
442+
/// These `as` specializations are type conversions, and do not have a
443+
/// corresponding `is`.
444+
template <> float as<float>() const { return asFloat(); }
445+
template <> const char* as<const char*>() const { return asCString(); }
446+
#ifdef JSON_USE_CPPTL
447+
template <> CppTL::ConstString as<CppTL::ConstString>() const {
448+
return asConstString();
449+
}
450+
#endif
461451

462452
bool isConvertibleTo(ValueType other) const;
463453

src/test_lib_json/main.cpp

Lines changed: 40 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -3361,115 +3361,51 @@ int main(int argc, const char* argv[]) {
33613361
return runner.runCommandLine(argc, argv);
33623362
}
33633363

3364-
struct TemplatedAs : JsonTest::TestCase {};
3365-
3366-
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorCString) {
3367-
Json::Value json = "hello world";
3368-
JSONTEST_ASSERT_STRING_EQUAL(json.asCString(), json.as<const char*>());
3369-
}
3370-
3371-
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorString) {
3372-
Json::Value json = "hello world";
3373-
JSONTEST_ASSERT_STRING_EQUAL(json.asString(), json.as<Json::String>());
3374-
}
3364+
struct MemberTemplateAs : JsonTest::TestCase {
3365+
template <typename T, typename F>
3366+
JsonTest::TestResult& EqEval(T v, F f) const {
3367+
const Json::Value j = v;
3368+
return JSONTEST_ASSERT_EQUAL(j.as<T>(), f(j));
3369+
}
3370+
};
33753371

3372+
JSONTEST_FIXTURE_LOCAL(MemberTemplateAs, BehavesSameAsNamedAs) {
3373+
const Json::Value jstr = "hello world";
3374+
JSONTEST_ASSERT_STRING_EQUAL(jstr.as<const char*>(), jstr.asCString());
3375+
JSONTEST_ASSERT_STRING_EQUAL(jstr.as<Json::String>(), jstr.asString());
33763376
#ifdef JSON_USE_CPPTL
3377-
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorConstString) {
3378-
Json::Value json = "hello world";
3379-
JSONTEST_ASSERT_STRING_EQUAL(json.asConstString(),
3380-
json.as<CppTL::ConstString>());
3381-
}
3377+
JSONTEST_ASSERT_STRING_EQUAL(js.as<CppTL::ConstString>(), js.asConstString());
33823378
#endif
3383-
3384-
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorInt) {
3385-
Json::Value json = Json::Int(64);
3386-
JSONTEST_ASSERT_EQUAL(json.asInt(), json.as<Json::Int>());
3387-
}
3388-
3389-
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorUInt) {
3390-
Json::Value json = Json::UInt(64);
3391-
JSONTEST_ASSERT_EQUAL(json.asUInt(), json.as<Json::UInt>());
3392-
}
3393-
3379+
EqEval(Json::Int(64), [](const Json::Value& j) { return j.asInt(); });
3380+
EqEval(Json::UInt(64), [](const Json::Value& j) { return j.asUInt(); });
33943381
#if defined(JSON_HAS_INT64)
3395-
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorInt64) {
3396-
Json::Value json = Json::Int64(64);
3397-
JSONTEST_ASSERT_EQUAL(json.asUInt64(), json.as<Json::Int64>());
3398-
}
3399-
3400-
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorUInt64) {
3401-
Json::Value json = Json::UInt64(64);
3402-
JSONTEST_ASSERT_EQUAL(json.asUInt64(), json.as<Json::UInt64>());
3403-
}
3382+
EqEval(Json::Int64(64), [](const Json::Value& j) { return j.asInt64(); });
3383+
EqEval(Json::UInt64(64), [](const Json::Value& j) { return j.asUInt64(); });
34043384
#endif // if defined(JSON_HAS_INT64)
3405-
3406-
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorLargestInt) {
3407-
Json::Value json = Json::LargestInt(64);
3408-
JSONTEST_ASSERT_EQUAL(json.asLargestInt(), json.as<Json::LargestInt>());
3409-
}
3410-
3411-
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorLargestUInt) {
3412-
Json::Value json = Json::LargestUInt(64);
3413-
JSONTEST_ASSERT_EQUAL(json.asLargestUInt(), json.as<Json::LargestUInt>());
3414-
}
3415-
3416-
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorFloat) {
3417-
Json::Value json = float(69.69);
3418-
JSONTEST_ASSERT_EQUAL(json.asFloat(), json.as<float>());
3419-
}
3420-
3421-
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorDouble) {
3422-
Json::Value json = double(69.69);
3423-
JSONTEST_ASSERT_EQUAL(json.asDouble(), json.as<double>());
3424-
}
3425-
3426-
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorBool) {
3427-
Json::Value jsonTrue = true;
3428-
Json::Value jsonFalse = false;
3429-
JSONTEST_ASSERT_EQUAL(jsonTrue.asBool(), jsonTrue.as<bool>());
3430-
JSONTEST_ASSERT_EQUAL(jsonFalse.asBool(), jsonFalse.as<bool>());
3431-
}
3432-
3433-
struct TemplatedIs : JsonTest::TestCase {};
3434-
3435-
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsBool) {
3436-
Json::Value json = true;
3437-
JSONTEST_ASSERT_EQUAL(json.isBool(), json.is<bool>());
3438-
}
3439-
3440-
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsInt) {
3441-
Json::Value json = 142;
3442-
JSONTEST_ASSERT_EQUAL(json.isInt(), json.is<Json::Int>());
3443-
}
3444-
3445-
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsInt64) {
3446-
Json::Value json = 142;
3447-
JSONTEST_ASSERT_EQUAL(json.isInt64(), json.is<Json::Int64>());
3448-
}
3449-
3450-
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsUInt) {
3451-
Json::Value json = 142;
3452-
JSONTEST_ASSERT_EQUAL(json.isUInt(), json.is<Json::UInt>());
3453-
}
3454-
3455-
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsUInt64) {
3456-
Json::Value json = 142;
3457-
JSONTEST_ASSERT_EQUAL(json.isUInt64(), json.is<Json::UInt64>());
3458-
}
3459-
3460-
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsDouble) {
3461-
Json::Value json = 40.63;
3462-
JSONTEST_ASSERT_EQUAL(json.isDouble(), json.is<double>());
3463-
}
3464-
3465-
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsCString) {
3466-
Json::Value json = "hello world";
3467-
JSONTEST_ASSERT_EQUAL(json.isString(), json.is<const char*>());
3468-
}
3469-
3470-
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsString) {
3471-
Json::Value json = "hello world";
3472-
JSONTEST_ASSERT_EQUAL(json.isString(), json.is<Json::String>());
3385+
EqEval(Json::LargestInt(64),
3386+
[](const Json::Value& j) { return j.asLargestInt(); });
3387+
EqEval(Json::LargestUInt(64),
3388+
[](const Json::Value& j) { return j.asLargestUInt(); });
3389+
3390+
EqEval(69.69f, [](const Json::Value& j) { return j.asFloat(); });
3391+
EqEval(69.69, [](const Json::Value& j) { return j.asDouble(); });
3392+
EqEval(false, [](const Json::Value& j) { return j.asBool(); });
3393+
EqEval(true, [](const Json::Value& j) { return j.asBool(); });
3394+
}
3395+
3396+
class MemberTemplateIs : public JsonTest::TestCase {};
3397+
3398+
JSONTEST_FIXTURE_LOCAL(MemberTemplateIs, BehavesSameAsNamedIs) {
3399+
const Json::Value values[] = {true, 142, 40.63, "hello world"};
3400+
for (const Json::Value& j : values) {
3401+
JSONTEST_ASSERT_EQUAL(j.is<bool>(), j.isBool());
3402+
JSONTEST_ASSERT_EQUAL(j.is<Json::Int>(), j.isInt());
3403+
JSONTEST_ASSERT_EQUAL(j.is<Json::Int64>(), j.isInt64());
3404+
JSONTEST_ASSERT_EQUAL(j.is<Json::UInt>(), j.isUInt());
3405+
JSONTEST_ASSERT_EQUAL(j.is<Json::UInt64>(), j.isUInt64());
3406+
JSONTEST_ASSERT_EQUAL(j.is<double>(), j.isDouble());
3407+
JSONTEST_ASSERT_EQUAL(j.is<Json::String>(), j.isString());
3408+
}
34733409
}
34743410

34753411
#if defined(__GNUC__)

0 commit comments

Comments
 (0)