From 4a06d92dd26636d15acb084181250da855f5def3 Mon Sep 17 00:00:00 2001 From: mrabine Date: Sun, 18 Oct 2026 00:26:58 +0200 Subject: [PATCH 1/2] Use composition in value instead of inheritance --- core/tests/variant_test.cpp | 7 + data/include/join/document.hpp | 51 +- data/include/join/json.hpp | 2 +- data/include/join/pack.hpp | 60 +- data/include/join/value.hpp | 966 ++++++++++++++++++++------------ data/tests/document_test.cpp | 16 +- data/tests/pack_writer_test.cpp | 6 +- data/tests/value_test.cpp | 720 +++++++----------------- 8 files changed, 882 insertions(+), 946 deletions(-) diff --git a/core/tests/variant_test.cpp b/core/tests/variant_test.cpp index 695b2cc6..37003cc2 100644 --- a/core/tests/variant_test.cpp +++ b/core/tests/variant_test.cpp @@ -828,6 +828,13 @@ TEST (Variant, get) EXPECT_THROW (var.get<2> (), std::bad_cast); EXPECT_THROW (var.get<3> (), std::bad_cast); EXPECT_NO_THROW (var.get<4> ()); + + var.set (0.5); + const Variant& constVar = var; + EXPECT_NO_THROW (constVar.get ()); + EXPECT_THROW (constVar.get (), std::bad_cast); + EXPECT_NO_THROW (constVar.get<1> ()); + EXPECT_THROW (constVar.get<0> (), std::bad_cast); } /** diff --git a/data/include/join/document.hpp b/data/include/join/document.hpp index 8ac172f0..8a555972 100644 --- a/data/include/join/document.hpp +++ b/data/include/join/document.hpp @@ -131,7 +131,7 @@ namespace join */ int setNull () { - return emplaceValue (in_place_index_t{}, nullptr); + return emplaceValue (nullptr); } /** @@ -141,7 +141,7 @@ namespace join */ int setBool (bool value) { - return emplaceValue (in_place_index_t{}, value); + return emplaceValue (value); } /** @@ -151,7 +151,7 @@ namespace join */ int setInt (int32_t value) { - return emplaceValue (in_place_index_t{}, value); + return emplaceValue (value); } /** @@ -161,7 +161,7 @@ namespace join */ int setUint (uint32_t value) { - return emplaceValue (in_place_index_t{}, value); + return emplaceValue (value); } /** @@ -171,7 +171,7 @@ namespace join */ int setInt64 (int64_t value) { - return emplaceValue (in_place_index_t{}, value); + return emplaceValue (value); } /** @@ -181,7 +181,7 @@ namespace join */ int setUint64 (uint64_t value) { - return emplaceValue (in_place_index_t{}, value); + return emplaceValue (value); } /** @@ -191,7 +191,7 @@ namespace join */ int setDouble (double value) { - return emplaceValue (in_place_index_t{}, value); + return emplaceValue (value); } /** @@ -201,7 +201,7 @@ namespace join */ int setString (const std::string& value) { - return emplaceValue (in_place_index_t{}, value); + return emplaceValue (value); } /** @@ -211,7 +211,7 @@ namespace join */ int setString (std::string&& value) { - return emplaceValue (in_place_index_t{}, std::move (value)); + return emplaceValue (std::move (value)); } /** @@ -258,7 +258,7 @@ namespace join return 0; } - _stack.emplace_back (in_place_index_t{}, std::move (array)); + _stack.emplace_back (std::move (array)); return 0; } @@ -362,7 +362,7 @@ namespace join return 0; } - _stack.emplace_back (in_place_index_t{}, std::move (object)); + _stack.emplace_back (std::move (object)); return 0; } @@ -389,31 +389,31 @@ namespace join switch (value.index ()) { case Value::Boolean: - return handler.setBool (value.get ()); + return handler.setBool (value.getBool ()); case Value::Integer: - return handler.setInt (value.get ()); + return handler.setInt (value.getInt ()); case Value::Unsigned: - return handler.setUint (value.get ()); + return handler.setUint (value.getUint ()); case Value::Integer64: - return handler.setInt64 (value.get ()); + return handler.setInt64 (value.getInt64 ()); case Value::Unsigned64: - return handler.setUint64 (value.get ()); + return handler.setUint64 (value.getUint64 ()); case Value::Real: - return handler.setDouble (value.get ()); + return handler.setDouble (value.getDouble ()); case Value::String: - return handler.setString (value.get ()); + return handler.setString (value.getString ()); case Value::ArrayValue: - return setArray (value.get (), handler); + return setArray (value.getArray (), handler); case Value::ObjectValue: - return setObject (value.get (), handler); + return setObject (value.getObject (), handler); default: return handler.setNull (); @@ -477,20 +477,19 @@ namespace join /** * @brief construct a value in place in the current container, or as root. - * @param index index of the alternative to construct. - * @param args arguments forwarded to the value constructor. + * @param value value of the alternative to construct. * @return 0 on success, -1 otherwise. */ - template - int emplaceValue (in_place_index_t index, Args&&... args) + template + int emplaceValue (T&& value) { if (JOIN_UNLIKELY (_depth == 0)) { - _root = Value (index, std::forward (args)...); + _root = Value (std::forward (value)); return 0; } - _stack.emplace_back (index, std::forward (args)...); + _stack.emplace_back (std::forward (value)); return 0; } diff --git a/data/include/join/json.hpp b/data/include/join/json.hpp index 39f1f52e..014f2662 100644 --- a/data/include/join/json.hpp +++ b/data/include/join/json.hpp @@ -1810,7 +1810,7 @@ namespace join template int readString (ViewType& document, Handler& handler, bool isKey = false) { - thread_local Value output (in_place_index_t{}); + thread_local Value output{std::string ()}; output.clear (); output.reserve (64); diff --git a/data/include/join/pack.hpp b/data/include/join/pack.hpp index 736f1269..7ebb523c 100644 --- a/data/include/join/pack.hpp +++ b/data/include/join/pack.hpp @@ -434,25 +434,25 @@ namespace join switch (key.index ()) { case Value::Boolean: - return setBool (key.get ()); + return setBool (key.getBool ()); case Value::Integer: - return setInt (key.get ()); + return setInt (key.getInt ()); case Value::Unsigned: - return setUint (key.get ()); + return setUint (key.getUint ()); case Value::Integer64: - return setInt64 (key.get ()); + return setInt64 (key.getInt64 ()); case Value::Unsigned64: - return setUint64 (key.get ()); + return setUint64 (key.getUint64 ()); case Value::Real: - return setDouble (key.get ()); + return setDouble (key.getDouble ()); case Value::String: - return setString (key.get ()); + return setString (key.getString ()); default: return setNull (); @@ -826,7 +826,7 @@ namespace join int readNull (ViewType& document, Handler& handler, bool isKey = false) { document.get (); - return (isKey) ? handler.setKey (Value (in_place_index_t{}, nullptr)) : handler.setNull (); + return (isKey) ? handler.setKey (Value (nullptr)) : handler.setNull (); } /** @@ -840,8 +840,7 @@ namespace join int readFalse (ViewType& document, Handler& handler, bool isKey = false) { document.get (); - return (isKey) ? handler.setKey (Value (in_place_index_t{}, false)) - : handler.setBool (false); + return (isKey) ? handler.setKey (Value (false)) : handler.setBool (false); } /** @@ -855,7 +854,7 @@ namespace join int readTrue (ViewType& document, Handler& handler, bool isKey = false) { document.get (); - return (isKey) ? handler.setKey (Value (in_place_index_t{}, true)) : handler.setBool (true); + return (isKey) ? handler.setKey (Value (true)) : handler.setBool (true); } /** @@ -1062,8 +1061,7 @@ namespace join return -1; } - return isKey ? handler.setKey (Value (in_place_index_t{}, std::move (output))) - : handler.setString (std::move (output)); + return isKey ? handler.setKey (Value (std::move (output))) : handler.setString (std::move (output)); } /** @@ -1123,14 +1121,12 @@ namespace join if (head <= 0x7f) { auto number = uint32_t (head); - return isKey ? handler.setKey (Value (in_place_index_t{}, number)) - : handler.setUint (number); + return isKey ? handler.setKey (Value (number)) : handler.setUint (number); } else if (head >= 0xe0) { auto number = int32_t (static_cast (head)); - return isKey ? handler.setKey (Value (in_place_index_t{}, number)) - : handler.setInt (number); + return isKey ? handler.setKey (Value (number)) : handler.setInt (number); } else if (head == 0xd3) { @@ -1139,8 +1135,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (in_place_index_t{}, number)) - : handler.setInt64 (number); + return isKey ? handler.setKey (Value (number)) : handler.setInt64 (number); } else if (head == 0xcf) { @@ -1149,8 +1144,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (in_place_index_t{}, number)) - : handler.setUint64 (number); + return isKey ? handler.setKey (Value (number)) : handler.setUint64 (number); } else if (head == 0xcb) { @@ -1159,8 +1153,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (in_place_index_t{}, number)) - : handler.setDouble (number); + return isKey ? handler.setKey (Value (number)) : handler.setDouble (number); } else if (head == 0xd2) { @@ -1169,8 +1162,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (in_place_index_t{}, number)) - : handler.setInt (number); + return isKey ? handler.setKey (Value (number)) : handler.setInt (number); } else if (head == 0xce) { @@ -1179,8 +1171,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (in_place_index_t{}, number)) - : handler.setUint (number); + return isKey ? handler.setKey (Value (number)) : handler.setUint (number); } else if (head == 0xca) { @@ -1189,8 +1180,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (in_place_index_t{}, number)) - : handler.setDouble (number); + return isKey ? handler.setKey (Value (number)) : handler.setDouble (number); } else if (head == 0xd1) { @@ -1199,8 +1189,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (in_place_index_t{}, number)) - : handler.setInt (number); + return isKey ? handler.setKey (Value (number)) : handler.setInt (number); } else if (head == 0xcd) { @@ -1209,8 +1198,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (in_place_index_t{}, number)) - : handler.setUint (number); + return isKey ? handler.setKey (Value (number)) : handler.setUint (number); } else if (head == 0xd0) { @@ -1219,8 +1207,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (in_place_index_t{}, number)) - : handler.setInt (number); + return isKey ? handler.setKey (Value (number)) : handler.setInt (number); } else if (head == 0xcc) { @@ -1229,8 +1216,7 @@ namespace join { return -1; } - return isKey ? handler.setKey (Value (in_place_index_t{}, number)) - : handler.setUint (number); + return isKey ? handler.setKey (Value (number)) : handler.setUint (number); } else { diff --git a/data/include/join/value.hpp b/data/include/join/value.hpp index ffd40bb9..8ca476d5 100644 --- a/data/include/join/value.hpp +++ b/data/include/join/value.hpp @@ -56,8 +56,11 @@ namespace join * @brief value class. */ class Value - : public Variant { + /// variant type holding the value. + using Data = + Variant; + public: /** * @brief nested value type index. @@ -76,38 +79,135 @@ namespace join ObjectValue = 9, /**< index of the Object alternative. */ }; - /// pointer. - using Ptr = Value*; + /** + * @brief default constructor. + */ + constexpr Value () = default; - /// inherits parent's constructors. - using Variant::Variant; + /** + * @brief constructs a null value. + * @param value null value. + */ + constexpr Value (std::nullptr_t value) + : _data (in_place_index_t{}, value) + { + } - /// inherits parent's assignment operators. - using Variant::operator=; + /** + * @brief constructs a boolean value. + * @param value boolean value. + */ + constexpr Value (bool value) + : _data (in_place_index_t{}, value) + { + } /** - * @brief default constructor. + * @brief constructs an integer value. + * @param value integer value. */ - constexpr Value () = default; + constexpr Value (int32_t value) + : _data (in_place_index_t{}, value) + { + } + + /** + * @brief constructs an unsigned integer value. + * @param value unsigned integer value. + */ + constexpr Value (uint32_t value) + : _data (in_place_index_t{}, value) + { + } + + /** + * @brief constructs a 64 bits integer value. + * @param value 64 bits integer value. + */ + constexpr Value (int64_t value) + : _data (in_place_index_t{}, value) + { + } + + /** + * @brief constructs a 64 bits unsigned integer value. + * @param value 64 bits unsigned integer value. + */ + constexpr Value (uint64_t value) + : _data (in_place_index_t{}, value) + { + } + + /** + * @brief constructs a real value. + * @param value real value. + */ + constexpr Value (double value) + : _data (in_place_index_t{}, value) + { + } + + /** + * @brief constructs a string value from a C-style string. + * @param value string value. + */ + Value (const char* value) + : _data (in_place_index_t{}, value) + { + } + + /** + * @brief constructs a string value. + * @param value string value. + */ + Value (const std::string& value) + : _data (in_place_index_t{}, value) + { + } + + /** + * @brief constructs a string value. + * @param value string value. + */ + Value (std::string&& value) + : _data (in_place_index_t{}, std::move (value)) + { + } + + /** + * @brief constructs an array value. + * @param value array value. + */ + Value (const Array& value) + : _data (in_place_index_t{}, value) + { + } + + /** + * @brief constructs an array value. + * @param value array value. + */ + Value (Array&& value) + : _data (in_place_index_t{}, std::move (value)) + { + } /** - * @brief constructs the value with the copy of the contents of a C-style string. - * @param other c-style string to use as data source. + * @brief constructs an object value. + * @param value object value. */ - constexpr Value (const char* other) - : Variant (in_place_index_t{}, other) + Value (const Object& value) + : _data (in_place_index_t{}, value) { } /** - * @brief replaces the contents with a copy of a C-style string. - * @param other c-style string to use as data source. - * @return a reference of the current value. + * @brief constructs an object value. + * @param value object value. */ - Value& operator= (const char* other) + Value (Object&& value) + : _data (in_place_index_t{}, std::move (value)) { - Variant::operator= (Value (in_place_index_t{}, other)); - return *this; } /** @@ -141,13 +241,22 @@ namespace join */ ~Value () = default; + /** + * @brief get the index of the alternative in use. + * @return index of the alternative in use. + */ + constexpr std::size_t index () const noexcept + { + return _data.index (); + } + /** * @brief check if the variable held by value is a null value. * @return true if the variable held by value is null, false otherwise. */ constexpr bool isNull () const { - return index () == Null; + return _data.index () == Null; } /** @@ -156,7 +265,7 @@ namespace join */ constexpr bool isBool () const { - return index () == Boolean; + return _data.index () == Boolean; } /** @@ -166,34 +275,12 @@ namespace join */ constexpr bool getBool () const { - switch (index ()) + if (_data.index () == Boolean) { - case Null: - return false; - - case Boolean: - return get (); - - case Integer: - return get (); - - case Unsigned: - return get (); - - case Integer64: - return get (); - - case Unsigned64: - return get (); - - case Real: - return get (); - - default: - break; + return _data.get (); } - throw std::bad_cast (); + return convertBool (); } /** @@ -232,8 +319,8 @@ namespace join */ constexpr bool isNumber () const { - return index () == Integer || index () == Unsigned || index () == Integer64 || index () == Unsigned64 || - index () == Real; + return _data.index () == Integer || _data.index () == Unsigned || _data.index () == Integer64 || + _data.index () == Unsigned64 || _data.index () == Real; } /** @@ -242,26 +329,26 @@ namespace join */ constexpr bool isInt8 () const { - switch (index ()) + switch (_data.index ()) { case Integer: - return get () >= static_cast (std::numeric_limits::min ()) && - get () <= static_cast (std::numeric_limits::max ()); + return _data.get () >= static_cast (std::numeric_limits::min ()) && + _data.get () <= static_cast (std::numeric_limits::max ()); case Unsigned: - return get () <= static_cast (std::numeric_limits::max ()); + return _data.get () <= static_cast (std::numeric_limits::max ()); case Integer64: - return get () >= static_cast (std::numeric_limits::min ()) && - get () <= static_cast (std::numeric_limits::max ()); + return _data.get () >= static_cast (std::numeric_limits::min ()) && + _data.get () <= static_cast (std::numeric_limits::max ()); case Unsigned64: - return get () <= static_cast (std::numeric_limits::max ()); + return _data.get () <= static_cast (std::numeric_limits::max ()); case Real: - return std::trunc (get ()) == get () && - get () >= static_cast (std::numeric_limits::min ()) && - get () <= static_cast (std::numeric_limits::max ()); + return std::trunc (_data.get ()) == _data.get () && + _data.get () >= static_cast (std::numeric_limits::min ()) && + _data.get () <= static_cast (std::numeric_limits::max ()); default: break; @@ -277,28 +364,14 @@ namespace join */ constexpr int8_t getInt8 () const { - if (isInt8 ()) + if (_data.index () == Integer && + _data.get () >= static_cast (std::numeric_limits::min ()) && + _data.get () <= static_cast (std::numeric_limits::max ())) { - switch (index ()) - { - case Integer: - return static_cast (get ()); - - case Unsigned: - return static_cast (get ()); - - case Integer64: - return static_cast (get ()); - - case Unsigned64: - return static_cast (get ()); - - default: - return static_cast (get ()); - } + return static_cast (_data.get ()); } - throw std::bad_cast (); + return convertInt8 (); } /** @@ -317,25 +390,25 @@ namespace join */ constexpr bool isUint8 () const { - switch (index ()) + switch (_data.index ()) { case Integer: - return get () >= 0 && - get () <= static_cast (std::numeric_limits::max ()); + return _data.get () >= 0 && + _data.get () <= static_cast (std::numeric_limits::max ()); case Unsigned: - return get () <= static_cast (std::numeric_limits::max ()); + return _data.get () <= static_cast (std::numeric_limits::max ()); case Integer64: - return get () >= 0 && - get () <= static_cast (std::numeric_limits::max ()); + return _data.get () >= 0 && + _data.get () <= static_cast (std::numeric_limits::max ()); case Unsigned64: - return get () <= static_cast (std::numeric_limits::max ()); + return _data.get () <= static_cast (std::numeric_limits::max ()); case Real: - return std::trunc (get ()) == get () && get () >= 0 && - get () <= static_cast (std::numeric_limits::max ()); + return std::trunc (_data.get ()) == _data.get () && _data.get () >= 0 && + _data.get () <= static_cast (std::numeric_limits::max ()); default: break; @@ -351,28 +424,13 @@ namespace join */ constexpr uint8_t getUint8 () const { - if (isUint8 ()) + if (_data.index () == Integer && _data.get () >= 0 && + _data.get () <= static_cast (std::numeric_limits::max ())) { - switch (index ()) - { - case Integer: - return static_cast (get ()); - - case Unsigned: - return static_cast (get ()); - - case Integer64: - return static_cast (get ()); - - case Unsigned64: - return static_cast (get ()); - - default: - return static_cast (get ()); - } + return static_cast (_data.get ()); } - throw std::bad_cast (); + return convertUint8 (); } /** @@ -391,26 +449,26 @@ namespace join */ constexpr bool isInt16 () const { - switch (index ()) + switch (_data.index ()) { case Integer: - return get () >= static_cast (std::numeric_limits::min ()) && - get () <= static_cast (std::numeric_limits::max ()); + return _data.get () >= static_cast (std::numeric_limits::min ()) && + _data.get () <= static_cast (std::numeric_limits::max ()); case Unsigned: - return get () <= static_cast (std::numeric_limits::max ()); + return _data.get () <= static_cast (std::numeric_limits::max ()); case Integer64: - return get () >= static_cast (std::numeric_limits::min ()) && - get () <= static_cast (std::numeric_limits::max ()); + return _data.get () >= static_cast (std::numeric_limits::min ()) && + _data.get () <= static_cast (std::numeric_limits::max ()); case Unsigned64: - return get () <= static_cast (std::numeric_limits::max ()); + return _data.get () <= static_cast (std::numeric_limits::max ()); case Real: - return std::trunc (get ()) == get () && - get () >= static_cast (std::numeric_limits::min ()) && - get () <= static_cast (std::numeric_limits::max ()); + return std::trunc (_data.get ()) == _data.get () && + _data.get () >= static_cast (std::numeric_limits::min ()) && + _data.get () <= static_cast (std::numeric_limits::max ()); default: break; @@ -426,28 +484,14 @@ namespace join */ constexpr int16_t getInt16 () const { - if (isInt16 ()) + if (_data.index () == Integer && + _data.get () >= static_cast (std::numeric_limits::min ()) && + _data.get () <= static_cast (std::numeric_limits::max ())) { - switch (index ()) - { - case Integer: - return static_cast (get ()); - - case Unsigned: - return static_cast (get ()); - - case Integer64: - return static_cast (get ()); - - case Unsigned64: - return static_cast (get ()); - - default: - return static_cast (get ()); - } + return static_cast (_data.get ()); } - throw std::bad_cast (); + return convertInt16 (); } /** @@ -466,25 +510,25 @@ namespace join */ constexpr bool isUint16 () const { - switch (index ()) + switch (_data.index ()) { case Integer: - return get () >= 0 && - get () <= static_cast (std::numeric_limits::max ()); + return _data.get () >= 0 && + _data.get () <= static_cast (std::numeric_limits::max ()); case Unsigned: - return get () <= static_cast (std::numeric_limits::max ()); + return _data.get () <= static_cast (std::numeric_limits::max ()); case Integer64: - return get () >= 0 && - get () <= static_cast (std::numeric_limits::max ()); + return _data.get () >= 0 && + _data.get () <= static_cast (std::numeric_limits::max ()); case Unsigned64: - return get () <= static_cast (std::numeric_limits::max ()); + return _data.get () <= static_cast (std::numeric_limits::max ()); case Real: - return std::trunc (get ()) == get () && get () >= 0 && - get () <= static_cast (std::numeric_limits::max ()); + return std::trunc (_data.get ()) == _data.get () && _data.get () >= 0 && + _data.get () <= static_cast (std::numeric_limits::max ()); default: break; @@ -500,28 +544,13 @@ namespace join */ constexpr uint16_t getUint16 () const { - if (isUint16 ()) + if (_data.index () == Integer && _data.get () >= 0 && + _data.get () <= static_cast (std::numeric_limits::max ())) { - switch (index ()) - { - case Integer: - return static_cast (get ()); - - case Unsigned: - return static_cast (get ()); - - case Integer64: - return static_cast (get ()); - - case Unsigned64: - return static_cast (get ()); - - default: - return static_cast (get ()); - } + return static_cast (_data.get ()); } - throw std::bad_cast (); + return convertUint16 (); } /** @@ -540,25 +569,25 @@ namespace join */ constexpr bool isInt () const { - switch (index ()) + switch (_data.index ()) { case Integer: return true; case Unsigned: - return get () <= static_cast (std::numeric_limits::max ()); + return _data.get () <= static_cast (std::numeric_limits::max ()); case Integer64: - return get () >= static_cast (std::numeric_limits::min ()) && - get () <= static_cast (std::numeric_limits::max ()); + return _data.get () >= static_cast (std::numeric_limits::min ()) && + _data.get () <= static_cast (std::numeric_limits::max ()); case Unsigned64: - return get () <= static_cast (std::numeric_limits::max ()); + return _data.get () <= static_cast (std::numeric_limits::max ()); case Real: - return std::trunc (get ()) == get () && - get () >= static_cast (std::numeric_limits::min ()) && - get () <= static_cast (std::numeric_limits::max ()); + return std::trunc (_data.get ()) == _data.get () && + _data.get () >= static_cast (std::numeric_limits::min ()) && + _data.get () <= static_cast (std::numeric_limits::max ()); default: break; @@ -574,28 +603,12 @@ namespace join */ constexpr int32_t getInt () const { - if (isInt ()) + if (_data.index () == Integer) { - switch (index ()) - { - case Integer: - return get (); - - case Unsigned: - return static_cast (get ()); - - case Integer64: - return static_cast (get ()); - - case Unsigned64: - return static_cast (get ()); - - default: - return static_cast (get ()); - } + return _data.get (); } - throw std::bad_cast (); + return convertInt (); } /** @@ -614,24 +627,24 @@ namespace join */ constexpr bool isUint () const { - switch (index ()) + switch (_data.index ()) { case Integer: - return get () >= 0; + return _data.get () >= 0; case Unsigned: return true; case Integer64: - return get () >= 0 && - get () <= static_cast (std::numeric_limits::max ()); + return _data.get () >= 0 && + _data.get () <= static_cast (std::numeric_limits::max ()); case Unsigned64: - return get () <= static_cast (std::numeric_limits::max ()); + return _data.get () <= static_cast (std::numeric_limits::max ()); case Real: - return std::trunc (get ()) == get () && get () >= 0 && - get () <= static_cast (std::numeric_limits::max ()); + return std::trunc (_data.get ()) == _data.get () && _data.get () >= 0 && + _data.get () <= static_cast (std::numeric_limits::max ()); default: break; @@ -647,28 +660,12 @@ namespace join */ constexpr uint32_t getUint () const { - if (isUint ()) + if (_data.index () == Unsigned) { - switch (index ()) - { - case Integer: - return static_cast (get ()); - - case Unsigned: - return get (); - - case Integer64: - return static_cast (get ()); - - case Unsigned64: - return static_cast (get ()); - - default: - return static_cast (get ()); - } + return _data.get (); } - throw std::bad_cast (); + return convertUint (); } /** @@ -687,7 +684,7 @@ namespace join */ constexpr bool isInt64 () const { - switch (index ()) + switch (_data.index ()) { case Integer: case Unsigned: @@ -695,12 +692,12 @@ namespace join return true; case Unsigned64: - return get () <= static_cast (std::numeric_limits::max ()); + return _data.get () <= static_cast (std::numeric_limits::max ()); case Real: - return std::trunc (get ()) == get () && - get () >= static_cast (std::numeric_limits::min ()) && - get () < static_cast (std::numeric_limits::max ()); + return std::trunc (_data.get ()) == _data.get () && + _data.get () >= static_cast (std::numeric_limits::min ()) && + _data.get () < static_cast (std::numeric_limits::max ()); default: break; @@ -716,28 +713,12 @@ namespace join */ constexpr int64_t getInt64 () const { - if (isInt64 ()) + if (_data.index () == Integer64) { - switch (index ()) - { - case Integer: - return static_cast (get ()); - - case Unsigned: - return static_cast (get ()); - - case Integer64: - return get (); - - case Unsigned64: - return static_cast (get ()); - - default: - return static_cast (get ()); - } + return _data.get (); } - throw std::bad_cast (); + return convertInt64 (); } /** @@ -756,23 +737,23 @@ namespace join */ constexpr bool isUint64 () const { - switch (index ()) + switch (_data.index ()) { case Integer: - return get () >= 0; + return _data.get () >= 0; case Unsigned: return true; case Integer64: - return get () >= 0; + return _data.get () >= 0; case Unsigned64: return true; case Real: - return std::trunc (get ()) == get () && get () >= 0 && - get () < static_cast (std::numeric_limits::max ()); + return std::trunc (_data.get ()) == _data.get () && _data.get () >= 0 && + _data.get () < static_cast (std::numeric_limits::max ()); default: break; @@ -788,28 +769,12 @@ namespace join */ constexpr uint64_t getUint64 () const { - if (isUint64 ()) + if (_data.index () == Unsigned64) { - switch (index ()) - { - case Integer: - return static_cast (get ()); - - case Unsigned: - return static_cast (get ()); - - case Integer64: - return static_cast (get ()); - - case Unsigned64: - return get (); - - default: - return static_cast (get ()); - } + return _data.get (); } - throw std::bad_cast (); + return convertUint64 (); } /** @@ -838,28 +803,12 @@ namespace join */ constexpr float getFloat () const { - switch (index ()) + if (_data.index () == Real) { - case Integer: - return static_cast (get ()); - - case Unsigned: - return static_cast (get ()); - - case Integer64: - return static_cast (get ()); - - case Unsigned64: - return static_cast (get ()); - - case Real: - return static_cast (get ()); - - default: - break; + return static_cast (_data.get ()); } - throw std::bad_cast (); + return convertFloat (); } /** @@ -888,28 +837,12 @@ namespace join */ constexpr double getDouble () const { - switch (index ()) + if (_data.index () == Real) { - case Integer: - return static_cast (get ()); - - case Unsigned: - return static_cast (get ()); - - case Integer64: - return static_cast (get ()); - - case Unsigned64: - return static_cast (get ()); - - case Real: - return get (); - - default: - break; + return _data.get (); } - throw std::bad_cast (); + return convertDouble (); } /** @@ -928,7 +861,7 @@ namespace join */ constexpr bool isString () const { - return index () == String; + return _data.index () == String; } /** @@ -938,7 +871,7 @@ namespace join */ constexpr const std::string& getString () const { - return get (); + return _data.get (); } /** @@ -948,7 +881,7 @@ namespace join */ constexpr std::string& getString () { - return get (); + return _data.get (); } /** @@ -967,7 +900,7 @@ namespace join */ constexpr bool isArray () const { - return index () == ArrayValue; + return _data.index () == ArrayValue; } /** @@ -977,7 +910,7 @@ namespace join */ constexpr const Array& getArray () const { - return get (); + return _data.get (); } /** @@ -987,7 +920,7 @@ namespace join */ constexpr Array& getArray () { - return get (); + return _data.get (); } /** @@ -996,7 +929,7 @@ namespace join */ constexpr bool isObject () const { - return index () == ObjectValue; + return _data.index () == ObjectValue; } /** @@ -1006,7 +939,7 @@ namespace join */ constexpr const Object& getObject () const { - return get (); + return _data.get (); } /** @@ -1016,7 +949,7 @@ namespace join */ constexpr Object& getObject () { - return get (); + return _data.get (); } /** @@ -1027,12 +960,12 @@ namespace join */ Value& at (const Value& key) { - if (index () == ArrayValue) + if (_data.index () == ArrayValue) { - return get ().at (key.getUint64 ()); + return _data.get ().at (key.getUint64 ()); } - for (auto& member : get ()) + for (auto& member : _data.get ()) { if (member.first == key) { @@ -1051,12 +984,12 @@ namespace join */ const Value& at (const Value& key) const { - if (index () == ArrayValue) + if (_data.index () == ArrayValue) { - return get ().at (key.getUint64 ()); + return _data.get ().at (key.getUint64 ()); } - for (auto const& member : get ()) + for (auto const& member : _data.get ()) { if (member.first == key) { @@ -1075,17 +1008,17 @@ namespace join */ Value& operator[] (const Value& key) { - if (index () == ArrayValue) + if (_data.index () == ArrayValue) { - return get ()[key.getUint64 ()]; + return _data.get ()[key.getUint64 ()]; } - if (index () == Null) + if (_data.index () == Null) { - set (); + _data.set (); } - for (auto& member : get ()) + for (auto& member : _data.get ()) { if (member.first == key) { @@ -1093,9 +1026,9 @@ namespace join } } - get ().emplace_back (key, nullptr); + _data.get ().emplace_back (key, nullptr); - return get ().back ().second; + return _data.get ().back ().second; } /** @@ -1105,16 +1038,16 @@ namespace join */ bool empty () const { - switch (index ()) + switch (_data.index ()) { case String: - return get ().empty (); + return _data.get ().empty (); case ArrayValue: - return get ().empty (); + return _data.get ().empty (); case ObjectValue: - return get ().empty (); + return _data.get ().empty (); default: break; @@ -1130,16 +1063,16 @@ namespace join */ size_t size () const { - switch (index ()) + switch (_data.index ()) { case String: - return get ().size (); + return _data.get ().size (); case ArrayValue: - return get ().size (); + return _data.get ().size (); case ObjectValue: - return get ().size (); + return _data.get ().size (); default: break; @@ -1155,18 +1088,18 @@ namespace join */ void reserve (size_t cap) { - switch (index ()) + switch (_data.index ()) { case String: - get ().reserve (cap); + _data.get ().reserve (cap); return; case ArrayValue: - get ().reserve (cap); + _data.get ().reserve (cap); return; case ObjectValue: - get ().reserve (cap); + _data.get ().reserve (cap); return; default: @@ -1182,18 +1115,18 @@ namespace join */ void clear () { - switch (index ()) + switch (_data.index ()) { case String: - get ().clear (); + _data.get ().clear (); return; case ArrayValue: - get ().clear (); + _data.get ().clear (); return; case ObjectValue: - get ().clear (); + _data.get ().clear (); return; default: @@ -1211,8 +1144,8 @@ namespace join */ Value& insert (const Member& member) { - get ().push_back (member); - return get ().back ().second; + _data.get ().push_back (member); + return _data.get ().back ().second; } /** @@ -1223,8 +1156,8 @@ namespace join */ Value& insert (Member&& member) { - get ().emplace_back (std::move (member)); - return get ().back ().second; + _data.get ().emplace_back (std::move (member)); + return _data.get ().back ().second; } /** @@ -1235,14 +1168,14 @@ namespace join */ size_t erase (const Value& key) { - auto beg = get ().begin (); - auto end = get ().end (); + auto beg = _data.get ().begin (); + auto end = _data.get ().end (); for (auto it = beg; it != end; ++it) { if (it->first == key) { - get ().erase (it); + _data.get ().erase (it); return 1; } } @@ -1259,11 +1192,11 @@ namespace join template Value& emplaceBack (Args&&... args) { - if (index () == Null) + if (_data.index () == Null) { - set (); + _data.set (); } - return get ().emplace_back (std::forward (args)...); + return _data.get ().emplace_back (std::forward (args)...); } /** @@ -1274,12 +1207,12 @@ namespace join */ Value& pushBack (const Value& value) { - if (index () == Null) + if (_data.index () == Null) { - set (); + _data.set (); } - get ().push_back (value); - return get ().back (); + _data.get ().push_back (value); + return _data.get ().back (); } /** @@ -1290,12 +1223,12 @@ namespace join */ Value& pushBack (Value&& value) { - if (index () == Null) + if (_data.index () == Null) { - set (); + _data.set (); } - get ().emplace_back (std::move (value)); - return get ().back (); + _data.get ().emplace_back (std::move (value)); + return _data.get ().back (); } /** @@ -1304,7 +1237,7 @@ namespace join */ void popBack () { - get ().pop_back (); + _data.get ().pop_back (); } /** @@ -1314,12 +1247,12 @@ namespace join */ bool contains (const Value& key) const { - if (index () == ArrayValue) + if (_data.index () == ArrayValue) { - return get ().size () > key.getUint64 (); + return _data.get ().size () > key.getUint64 (); } - for (auto const& member : get ()) + for (auto const& member : _data.get ()) { if (member.first == key) { @@ -1346,6 +1279,313 @@ namespace join // friendship with lower operator. friend constexpr bool operator< (const Value& lhs, const Value& rhs); + + private: + /** + * @brief convert the value held by the variant to int8_t. + * @return converted value. + */ + int8_t convertInt8 () const + { + if (isInt8 ()) + { + switch (_data.index ()) + { + case Unsigned: + return static_cast (_data.get ()); + + case Integer64: + return static_cast (_data.get ()); + + case Unsigned64: + return static_cast (_data.get ()); + + default: + return static_cast (_data.get ()); + } + } + + throw std::bad_cast (); + } + + /** + * @brief convert the value held by the variant to uint8_t. + * @return converted value. + */ + uint8_t convertUint8 () const + { + if (isUint8 ()) + { + switch (_data.index ()) + { + case Unsigned: + return static_cast (_data.get ()); + + case Integer64: + return static_cast (_data.get ()); + + case Unsigned64: + return static_cast (_data.get ()); + + default: + return static_cast (_data.get ()); + } + } + + throw std::bad_cast (); + } + + /** + * @brief convert the value held by the variant to int16_t. + * @return converted value. + */ + int16_t convertInt16 () const + { + if (isInt16 ()) + { + switch (_data.index ()) + { + case Unsigned: + return static_cast (_data.get ()); + + case Integer64: + return static_cast (_data.get ()); + + case Unsigned64: + return static_cast (_data.get ()); + + default: + return static_cast (_data.get ()); + } + } + + throw std::bad_cast (); + } + + /** + * @brief convert the value held by the variant to uint16_t. + * @return converted value. + */ + uint16_t convertUint16 () const + { + if (isUint16 ()) + { + switch (_data.index ()) + { + case Unsigned: + return static_cast (_data.get ()); + + case Integer64: + return static_cast (_data.get ()); + + case Unsigned64: + return static_cast (_data.get ()); + + default: + return static_cast (_data.get ()); + } + } + + throw std::bad_cast (); + } + + /** + * @brief convert the value held by the variant to float. + * @return converted value. + */ + float convertFloat () const + { + switch (_data.index ()) + { + case Integer: + return static_cast (_data.get ()); + + case Unsigned: + return static_cast (_data.get ()); + + case Integer64: + return static_cast (_data.get ()); + + case Unsigned64: + return static_cast (_data.get ()); + + default: + break; + } + + throw std::bad_cast (); + } + + /** + * @brief convert the value held by the variant to bool. + * @return converted value. + */ + bool convertBool () const + { + switch (_data.index ()) + { + case Null: + return false; + + case Integer: + return _data.get (); + + case Unsigned: + return _data.get (); + + case Integer64: + return _data.get (); + + case Unsigned64: + return _data.get (); + + case Real: + return _data.get (); + + default: + break; + } + + throw std::bad_cast (); + } + + /** + * @brief convert the value held by the variant to int32_t. + * @return converted value. + */ + int32_t convertInt () const + { + if (isInt ()) + { + switch (_data.index ()) + { + case Unsigned: + return static_cast (_data.get ()); + + case Integer64: + return static_cast (_data.get ()); + + case Unsigned64: + return static_cast (_data.get ()); + + default: + return static_cast (_data.get ()); + } + } + + throw std::bad_cast (); + } + + /** + * @brief convert the value held by the variant to uint32_t. + * @return converted value. + */ + uint32_t convertUint () const + { + if (isUint ()) + { + switch (_data.index ()) + { + case Integer: + return static_cast (_data.get ()); + + case Integer64: + return static_cast (_data.get ()); + + case Unsigned64: + return static_cast (_data.get ()); + + default: + return static_cast (_data.get ()); + } + } + + throw std::bad_cast (); + } + + /** + * @brief convert the value held by the variant to int64_t. + * @return converted value. + */ + int64_t convertInt64 () const + { + if (isInt64 ()) + { + switch (_data.index ()) + { + case Integer: + return static_cast (_data.get ()); + + case Unsigned: + return static_cast (_data.get ()); + + case Unsigned64: + return static_cast (_data.get ()); + + default: + return static_cast (_data.get ()); + } + } + + throw std::bad_cast (); + } + + /** + * @brief convert the value held by the variant to uint64_t. + * @return converted value. + */ + uint64_t convertUint64 () const + { + if (isUint64 ()) + { + switch (_data.index ()) + { + case Integer: + return static_cast (_data.get ()); + + case Unsigned: + return static_cast (_data.get ()); + + case Integer64: + return static_cast (_data.get ()); + + default: + return static_cast (_data.get ()); + } + } + + throw std::bad_cast (); + } + + /** + * @brief convert the value held by the variant to double. + * @return converted value. + */ + double convertDouble () const + { + switch (_data.index ()) + { + case Integer: + return static_cast (_data.get ()); + + case Unsigned: + return static_cast (_data.get ()); + + case Integer64: + return static_cast (_data.get ()); + + case Unsigned64: + return static_cast (_data.get ()); + + default: + break; + } + + throw std::bad_cast (); + } + + /// variant holding the value. + Data _data; }; /** @@ -1372,7 +1612,7 @@ namespace join } } - return lhs.equal (rhs); + return lhs._data == rhs._data; } /** @@ -1410,7 +1650,7 @@ namespace join } } - return lhs.lower (rhs); + return lhs._data < rhs._data; } /** diff --git a/data/tests/document_test.cpp b/data/tests/document_test.cpp index 425cc28b..5e5eb2bf 100644 --- a/data/tests/document_test.cpp +++ b/data/tests/document_test.cpp @@ -240,7 +240,7 @@ TEST (Document, setInt) { Document document; ASSERT_EQ (document.setInt (-1), 0); - ASSERT_TRUE (document.root ().is ()); + ASSERT_EQ (document.root ().index (), Value::Integer); ASSERT_EQ (document.root (), -1); ASSERT_EQ (JsonReader ().deserialize (std::string ("42"), document), 0) << join::lastError.message (); ASSERT_EQ (document.root (), 42); @@ -248,7 +248,7 @@ TEST (Document, setInt) ASSERT_EQ (document.startArray (), 0); ASSERT_EQ (document.setInt (-1), 0); ASSERT_EQ (document.stopArray (), 0); - ASSERT_TRUE (document.root ()[0].is ()); + ASSERT_EQ (document.root ()[0].index (), Value::Integer); ASSERT_EQ (document.root ()[0], -1); } @@ -259,12 +259,12 @@ TEST (Document, setUint) { Document document; ASSERT_EQ (document.setUint (1), 0); - ASSERT_TRUE (document.root ().is ()); + ASSERT_EQ (document.root ().index (), Value::Unsigned); ASSERT_EQ (document.startArray (), 0); ASSERT_EQ (document.setUint (1), 0); ASSERT_EQ (document.stopArray (), 0); - ASSERT_TRUE (document.root ()[0].is ()); + ASSERT_EQ (document.root ()[0].index (), Value::Unsigned); ASSERT_EQ (document.root ()[0], 1U); } @@ -275,12 +275,12 @@ TEST (Document, setInt64) { Document document; ASSERT_EQ (document.setInt64 (-1), 0); - ASSERT_TRUE (document.root ().is ()); + ASSERT_EQ (document.root ().index (), Value::Integer64); ASSERT_EQ (document.startArray (), 0); ASSERT_EQ (document.setInt64 (-1), 0); ASSERT_EQ (document.stopArray (), 0); - ASSERT_TRUE (document.root ()[0].is ()); + ASSERT_EQ (document.root ()[0].index (), Value::Integer64); ASSERT_EQ (document.root ()[0], int64_t (-1)); } @@ -291,12 +291,12 @@ TEST (Document, setUint64) { Document document; ASSERT_EQ (document.setUint64 (1), 0); - ASSERT_TRUE (document.root ().is ()); + ASSERT_EQ (document.root ().index (), Value::Unsigned64); ASSERT_EQ (document.startArray (), 0); ASSERT_EQ (document.setUint64 (1), 0); ASSERT_EQ (document.stopArray (), 0); - ASSERT_TRUE (document.root ()[0].is ()); + ASSERT_EQ (document.root ()[0].index (), Value::Unsigned64); ASSERT_EQ (document.root ()[0], uint64_t (1)); } diff --git a/data/tests/pack_writer_test.cpp b/data/tests/pack_writer_test.cpp index 048cdd52..2aa1d131 100644 --- a/data/tests/pack_writer_test.cpp +++ b/data/tests/pack_writer_test.cpp @@ -547,13 +547,13 @@ TEST (PackWriter, setKey) EXPECT_EQ (key (std::string (65536, 'x')), 0); EXPECT_EQ (stream.str ().compare (1, 6, std::string ({'\xDB', '\x00', '\x01', '\x00', '\x00', '\x78'})), 0); - EXPECT_EQ (key (Value (join::in_place_index_t{})), -1); + EXPECT_EQ (key (Value (Array{})), -1); - EXPECT_EQ (key (Value (join::in_place_index_t{})), -1); + EXPECT_EQ (key (Value (Object{})), -1); stream.str (""); EXPECT_EQ (packWriter.startObject (1), 0); - EXPECT_EQ (packWriter.setKey (Value (join::in_place_index_t{})), -1); + EXPECT_EQ (packWriter.setKey (Value (Array{})), -1); EXPECT_EQ (join::lastError, SaxErrc::InvalidKey); EXPECT_EQ (packWriter.setNull (), 0); EXPECT_EQ (stream.str (), std::string ({'\xC0'})); diff --git a/data/tests/value_test.cpp b/data/tests/value_test.cpp index 861da0ce..a63c3d7e 100644 --- a/data/tests/value_test.cpp +++ b/data/tests/value_test.cpp @@ -107,11 +107,40 @@ TEST (Value, create) Value stringValue ("foo"); ASSERT_TRUE (stringValue.isString ()); + char buffer[] = "bar"; + Value bufferValue (buffer); + ASSERT_TRUE (bufferValue.isString ()); + ASSERT_EQ (bufferValue.getString (), "bar"); + + char* pointer = buffer; + Value pointerValue (pointer); + ASSERT_TRUE (pointerValue.isString ()); + ASSERT_EQ (pointerValue.getString (), "bar"); + + const std::string string ("baz"); + Value copiedStringValue (string); + ASSERT_TRUE (copiedStringValue.isString ()); + ASSERT_EQ (copiedStringValue.getString (), "baz"); + + Value movedStringValue (std::string ("qux")); + ASSERT_TRUE (movedStringValue.isString ()); + ASSERT_EQ (movedStringValue.getString (), "qux"); + Value arrayValue (Array{1}); ASSERT_TRUE (arrayValue.isArray ()); + const Array array{1, 2}; + Value copiedArrayValue (array); + ASSERT_TRUE (copiedArrayValue.isArray ()); + ASSERT_EQ (copiedArrayValue.size (), 2U); + Value objectValue (Object ({{"i", 1}})); ASSERT_TRUE (objectValue.isObject ()); + + const Object object{{"a", 1}}; + Value copiedObjectValue (object); + ASSERT_TRUE (copiedObjectValue.isObject ()); + ASSERT_EQ (copiedObjectValue["a"], 1); } /** @@ -205,7 +234,7 @@ TEST (Value, copy) Value arrayValue (value); ASSERT_TRUE (arrayValue.isArray ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; Value objectValue (value); ASSERT_TRUE (objectValue.isObject ()); } @@ -301,139 +330,11 @@ TEST (Value, move) Value arrayValue (std::move (value)); ASSERT_TRUE (arrayValue.isArray ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; Value objectValue (std::move (value)); ASSERT_TRUE (objectValue.isObject ()); } -/** - * @brief Test is method. - */ -TEST (Value, is) -{ - Value value; - - value.set (nullptr); - ASSERT_TRUE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - - value.set (std::numeric_limits::min ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_TRUE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - - value.set (std::numeric_limits::max ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_TRUE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - - value.set (std::numeric_limits::min ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_TRUE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - - value.set (std::numeric_limits::max ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_TRUE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - - value.set (std::numeric_limits::min ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_TRUE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - - value.set ("foobar"); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_TRUE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - - value.set (true); - ASSERT_FALSE (value.is ()); - ASSERT_TRUE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - - value.set ({1}); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_TRUE (value.is ()); - ASSERT_FALSE (value.is ()); - - value.set ({{"i", 1}}); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_FALSE (value.is ()); - ASSERT_TRUE (value.is ()); -} - /** * @brief Test isNull method. */ @@ -526,7 +427,7 @@ TEST (Value, isNull) value = Array{1}; ASSERT_FALSE (value.isNull ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isNull ()); } @@ -622,7 +523,7 @@ TEST (Value, isBool) value = Array{1}; ASSERT_FALSE (value.isBool ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isBool ()); } @@ -718,7 +619,7 @@ TEST (Value, isTrue) value = Array{1}; ASSERT_THROW (value.isTrue (), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.isTrue (), std::bad_cast); } @@ -814,7 +715,7 @@ TEST (Value, isFalse) value = Array{1}; ASSERT_THROW (value.isFalse (), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.isFalse (), std::bad_cast); } @@ -910,7 +811,7 @@ TEST (Value, isNumber) value = Array{1}; ASSERT_FALSE (value.isNumber ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isNumber ()); } @@ -1006,7 +907,7 @@ TEST (Value, isInt8) value = Array{1}; ASSERT_FALSE (value.isInt8 ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isInt8 ()); } @@ -1102,7 +1003,7 @@ TEST (Value, isUint8) value = Array{1}; ASSERT_FALSE (value.isUint8 ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isUint8 ()); } @@ -1198,7 +1099,7 @@ TEST (Value, isInt16) value = Array{1}; ASSERT_FALSE (value.isInt16 ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isInt16 ()); } @@ -1294,7 +1195,7 @@ TEST (Value, isUint16) value = Array{1}; ASSERT_FALSE (value.isUint16 ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isUint16 ()); } @@ -1390,7 +1291,7 @@ TEST (Value, isInt) value = Array{1}; ASSERT_FALSE (value.isInt ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isInt ()); } @@ -1486,7 +1387,7 @@ TEST (Value, isUint) value = Array{1}; ASSERT_FALSE (value.isUint ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isUint ()); } @@ -1582,7 +1483,7 @@ TEST (Value, isInt64) value = Array{1}; ASSERT_FALSE (value.isInt64 ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isInt64 ()); } @@ -1678,7 +1579,7 @@ TEST (Value, isUint64) value = Array{1}; ASSERT_FALSE (value.isUint64 ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isUint64 ()); } @@ -1774,7 +1675,7 @@ TEST (Value, isFloat) value = Array{1}; ASSERT_FALSE (value.isFloat ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isFloat ()); } @@ -1870,7 +1771,7 @@ TEST (Value, isDouble) value = Array{1}; ASSERT_FALSE (value.isDouble ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isDouble ()); } @@ -1966,7 +1867,7 @@ TEST (Value, isString) value = Array{1}; ASSERT_FALSE (value.isString ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isString ()); } @@ -2062,7 +1963,7 @@ TEST (Value, isArray) value = Array{1}; ASSERT_TRUE (value.isArray ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.isArray ()); } @@ -2158,217 +2059,10 @@ TEST (Value, isObject) value = Array{1}; ASSERT_FALSE (value.isObject ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_TRUE (value.isObject ()); } -/** - * @brief Test set method. - */ -TEST (Value, set) -{ - Value value; - - value.set (nullptr); - ASSERT_TRUE (value.is ()); - - value.set (nullptr); - ASSERT_TRUE (value.is ()); - - value.set (true); - ASSERT_TRUE (value.is ()); - ASSERT_TRUE (value.get ()); - - value.set (false); - ASSERT_TRUE (value.is ()); - ASSERT_FALSE (value.get ()); - - value.set (std::numeric_limits::min ()); - ASSERT_TRUE (value.is ()); - ASSERT_EQ (value.get (), std::numeric_limits::min ()); - - value.set (std::numeric_limits::min ()); - ASSERT_TRUE (value.is ()); - ASSERT_EQ (value.get (), std::numeric_limits::min ()); - - value.set (std::numeric_limits::max ()); - ASSERT_TRUE (value.is ()); - ASSERT_EQ (value.get (), std::numeric_limits::max ()); - - value.set (std::numeric_limits::max ()); - ASSERT_TRUE (value.is ()); - ASSERT_EQ (value.get (), std::numeric_limits::max ()); - - value.set (std::numeric_limits::min ()); - ASSERT_TRUE (value.is ()); - ASSERT_EQ (value.get (), std::numeric_limits::min ()); - - value.set (std::numeric_limits::min ()); - ASSERT_TRUE (value.is ()); - ASSERT_EQ (value.get (), std::numeric_limits::min ()); - - value.set (std::numeric_limits::max ()); - ASSERT_TRUE (value.is ()); - ASSERT_EQ (value.get (), std::numeric_limits::max ()); - - value.set (std::numeric_limits::max ()); - ASSERT_TRUE (value.is ()); - ASSERT_EQ (value.get (), std::numeric_limits::max ()); - - value.set (std::numeric_limits::min ()); - ASSERT_TRUE (value.is ()); - ASSERT_DOUBLE_EQ (value.get (), std::numeric_limits::min ()); - - value.set (std::numeric_limits::min ()); - ASSERT_TRUE (value.is ()); - ASSERT_DOUBLE_EQ (value.get (), std::numeric_limits::min ()); - - value.set ("foobar"); - ASSERT_TRUE (value.is ()); - ASSERT_EQ (value.get (), "foobar"); - - value.set ("foobar"); - ASSERT_TRUE (value.is ()); - ASSERT_EQ (value.get (), "foobar"); - - value.set ({1}); - ASSERT_TRUE (value.is ()); - ASSERT_EQ (value.get (), Array{1}); - - value.set ({{"i", 1}}); - ASSERT_TRUE (value.is ()); - ASSERT_EQ (value.get (), Object ({{"i", 1}})); -} - -/** - * @brief Test get method. - */ -TEST (Value, get) -{ - Value value; - - value.set (nullptr); - ASSERT_NO_THROW (value.get ()); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - - value.set (false); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_NO_THROW (value.get ()); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - - value.set (std::numeric_limits::min ()); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_NO_THROW (value.get ()); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - - value.set (std::numeric_limits::max ()); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_NO_THROW (value.get ()); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - - value.set (std::numeric_limits::min ()); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_NO_THROW (value.get ()); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - - value.set (std::numeric_limits::max ()); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_NO_THROW (value.get ()); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - - value.set (std::numeric_limits::min ()); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_NO_THROW (value.get ()); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - - value.set ("foobar"); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_NO_THROW (value.get ()); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - - value.set ({1}); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_NO_THROW (value.get ()); - ASSERT_THROW (value.get (), std::bad_cast); - - value.set ({{"i", 1}}); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_THROW (value.get (), std::bad_cast); - ASSERT_NO_THROW (value.get ()); -} - /** * @brief Test getBool method. */ @@ -2491,7 +2185,7 @@ TEST (Value, getBool) ASSERT_THROW (value.getBool (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.getBool (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); } @@ -2632,7 +2326,7 @@ TEST (Value, getInt8) ASSERT_THROW (value.getInt8 (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.getInt8 (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); } @@ -2773,7 +2467,7 @@ TEST (Value, getUint8) ASSERT_THROW (value.getUint8 (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.getUint8 (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); } @@ -2914,7 +2608,7 @@ TEST (Value, getInt16) ASSERT_THROW (value.getInt16 (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.getInt16 (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); } @@ -3055,7 +2749,7 @@ TEST (Value, getUint16) ASSERT_THROW (value.getUint16 (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.getUint16 (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); } @@ -3192,7 +2886,7 @@ TEST (Value, getInt) ASSERT_THROW (value.getInt (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.getInt (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); } @@ -3329,7 +3023,7 @@ TEST (Value, getUint) ASSERT_THROW (value.getUint (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.getUint (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); } @@ -3462,7 +3156,7 @@ TEST (Value, getInt64) ASSERT_THROW (value.getInt64 (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.getInt64 (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); } @@ -3595,7 +3289,7 @@ TEST (Value, getUint64) ASSERT_THROW (value.getUint64 (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.getUint64 (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); } @@ -3724,7 +3418,7 @@ TEST (Value, getFloat) ASSERT_THROW (value.getFloat (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.getFloat (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); } @@ -3853,7 +3547,7 @@ TEST (Value, getDouble) ASSERT_THROW (value.getDouble (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.getDouble (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); } @@ -3982,7 +3676,7 @@ TEST (Value, getString) ASSERT_THROW (value.getString (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.getString (), std::bad_cast); ASSERT_THROW ((tmp = static_cast (value)), std::bad_cast); } @@ -4079,7 +3773,7 @@ TEST (Value, getArray) value = Array{1}; ASSERT_NO_THROW (value.getArray ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.getArray (), std::bad_cast); } @@ -4175,7 +3869,7 @@ TEST (Value, getObject) value = Array{1}; ASSERT_THROW (value.getObject (), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_NO_THROW (value.getObject ()); } @@ -4186,7 +3880,7 @@ TEST (Value, index) { Value value; - value.set (nullptr); + value = nullptr; ASSERT_EQ (value.index (), static_cast (Value::Index::Null)); ASSERT_NE (value.index (), static_cast (Value::Index::Boolean)); ASSERT_NE (value.index (), static_cast (Value::Index::Integer)); @@ -4198,7 +3892,7 @@ TEST (Value, index) ASSERT_NE (value.index (), static_cast (Value::Index::ArrayValue)); ASSERT_NE (value.index (), static_cast (Value::Index::ObjectValue)); - value.set (true); + value = bool (true); ASSERT_NE (value.index (), static_cast (Value::Index::Null)); ASSERT_EQ (value.index (), static_cast (Value::Index::Boolean)); ASSERT_NE (value.index (), static_cast (Value::Index::Integer)); @@ -4210,7 +3904,7 @@ TEST (Value, index) ASSERT_NE (value.index (), static_cast (Value::Index::ArrayValue)); ASSERT_NE (value.index (), static_cast (Value::Index::ObjectValue)); - value.set (std::numeric_limits::min ()); + value = int32_t (std::numeric_limits::min ()); ASSERT_NE (value.index (), static_cast (Value::Index::Null)); ASSERT_NE (value.index (), static_cast (Value::Index::Boolean)); ASSERT_EQ (value.index (), static_cast (Value::Index::Integer)); @@ -4222,7 +3916,7 @@ TEST (Value, index) ASSERT_NE (value.index (), static_cast (Value::Index::ArrayValue)); ASSERT_NE (value.index (), static_cast (Value::Index::ObjectValue)); - value.set (std::numeric_limits::max ()); + value = uint32_t (std::numeric_limits::max ()); ASSERT_NE (value.index (), static_cast (Value::Index::Null)); ASSERT_NE (value.index (), static_cast (Value::Index::Boolean)); ASSERT_NE (value.index (), static_cast (Value::Index::Integer)); @@ -4234,7 +3928,7 @@ TEST (Value, index) ASSERT_NE (value.index (), static_cast (Value::Index::ArrayValue)); ASSERT_NE (value.index (), static_cast (Value::Index::ObjectValue)); - value.set (std::numeric_limits::min ()); + value = int64_t (std::numeric_limits::min ()); ASSERT_NE (value.index (), static_cast (Value::Index::Null)); ASSERT_NE (value.index (), static_cast (Value::Index::Boolean)); ASSERT_NE (value.index (), static_cast (Value::Index::Integer)); @@ -4246,7 +3940,7 @@ TEST (Value, index) ASSERT_NE (value.index (), static_cast (Value::Index::ArrayValue)); ASSERT_NE (value.index (), static_cast (Value::Index::ObjectValue)); - value.set (std::numeric_limits::max ()); + value = uint64_t (std::numeric_limits::max ()); ASSERT_NE (value.index (), static_cast (Value::Index::Null)); ASSERT_NE (value.index (), static_cast (Value::Index::Boolean)); ASSERT_NE (value.index (), static_cast (Value::Index::Integer)); @@ -4258,7 +3952,7 @@ TEST (Value, index) ASSERT_NE (value.index (), static_cast (Value::Index::ArrayValue)); ASSERT_NE (value.index (), static_cast (Value::Index::ObjectValue)); - value.set (std::numeric_limits::min ()); + value = double (std::numeric_limits::min ()); ASSERT_NE (value.index (), static_cast (Value::Index::Null)); ASSERT_NE (value.index (), static_cast (Value::Index::Boolean)); ASSERT_NE (value.index (), static_cast (Value::Index::Integer)); @@ -4270,7 +3964,7 @@ TEST (Value, index) ASSERT_NE (value.index (), static_cast (Value::Index::ArrayValue)); ASSERT_NE (value.index (), static_cast (Value::Index::ObjectValue)); - value.set ("foobar"); + value = std::string ("foobar"); ASSERT_NE (value.index (), static_cast (Value::Index::Null)); ASSERT_NE (value.index (), static_cast (Value::Index::Boolean)); ASSERT_NE (value.index (), static_cast (Value::Index::Integer)); @@ -4282,7 +3976,7 @@ TEST (Value, index) ASSERT_NE (value.index (), static_cast (Value::Index::ArrayValue)); ASSERT_NE (value.index (), static_cast (Value::Index::ObjectValue)); - value.set ({1}); + value = Array{1}; ASSERT_NE (value.index (), static_cast (Value::Index::Null)); ASSERT_NE (value.index (), static_cast (Value::Index::Boolean)); ASSERT_NE (value.index (), static_cast (Value::Index::Integer)); @@ -4294,7 +3988,7 @@ TEST (Value, index) ASSERT_EQ (value.index (), static_cast (Value::Index::ArrayValue)); ASSERT_NE (value.index (), static_cast (Value::Index::ObjectValue)); - value.set ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_NE (value.index (), static_cast (Value::Index::Null)); ASSERT_NE (value.index (), static_cast (Value::Index::Boolean)); ASSERT_NE (value.index (), static_cast (Value::Index::Integer)); @@ -4354,10 +4048,10 @@ TEST (Value, at) value["string"] = "foobar"; ASSERT_TRUE (value.at ("string").isString ()); - value["array"] = Array ({1, 2, 3, 4}); + value["array"] = Array{1, 2, 3, 4}; ASSERT_TRUE (value.at ("array").isArray ()); - value["object"] = Object ({{"foo", 1}, {"bar", 2}}); + value["object"] = Object{{"foo", 1}, {"bar", 2}}; ASSERT_TRUE (value.at ("object").isObject ()); ASSERT_THROW (value.at ("non existing path"), std::out_of_range); @@ -4385,11 +4079,21 @@ TEST (Value, assign) value["string"] = "foobar"; ASSERT_TRUE (value["string"].isString ()); - value["array"] = Array ({1, 2, 3, 4}); + value["array"] = Array{1, 2, 3, 4}; ASSERT_TRUE (value["array"].isArray ()); - value["object"] = Object ({{"foo", 1}, {"bar", 2}}); + value["object"] = Object{{"foo", 1}, {"bar", 2}}; ASSERT_TRUE (value["object"].isObject ()); + + value["braced"] = {}; + ASSERT_TRUE (value["braced"].isNull ()); + + value["braced"] = {1}; + ASSERT_TRUE (value["braced"].isInt ()); + + const std::string string ("foo"); + value["braced"] = {string}; + ASSERT_TRUE (value["braced"].isString ()); } /** @@ -4481,16 +4185,16 @@ TEST (Value, empty) value = "127.0.0.1"; ASSERT_FALSE (value.empty ()); - value = Array ({}); + value = Array{}; ASSERT_TRUE (value.empty ()); - value = Array ({1, 2, 3, 4}); + value = Array{1, 2, 3, 4}; ASSERT_FALSE (value.empty ()); value = Object ({}); ASSERT_TRUE (value.empty ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.empty ()); } @@ -4586,7 +4290,7 @@ TEST (Value, size) value = Array{1}; ASSERT_EQ (value.size (), 1); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_EQ (value.size (), 1); } @@ -4682,7 +4386,7 @@ TEST (Value, reserve) value = Array{1}; ASSERT_NO_THROW (value.reserve (12)); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_NO_THROW (value.reserve (12)); } @@ -4788,7 +4492,7 @@ TEST (Value, clear) ASSERT_NO_THROW (value.clear ()); ASSERT_TRUE (value.empty ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.empty ()); ASSERT_NO_THROW (value.clear ()); ASSERT_TRUE (value.empty ()); @@ -4886,7 +4590,7 @@ TEST (Value, insert) value = Array{1}; ASSERT_THROW (value.insert (std::make_pair ("i", 1)), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_NO_THROW (value.insert (std::make_pair ("i", 1))); } @@ -4982,7 +4686,7 @@ TEST (Value, erase) value = Array{1}; ASSERT_THROW (value.erase ("i"), std::bad_cast); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_EQ (value.erase ("i"), 1); ASSERT_EQ (value.erase ("j"), 0); } @@ -5079,7 +4783,7 @@ TEST (Value, pushBack) value = Array{1}; ASSERT_NO_THROW (value.pushBack (1)); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.pushBack (1), std::bad_cast); } @@ -5175,7 +4879,7 @@ TEST (Value, popBack) value = Array{1}; ASSERT_NO_THROW (value.popBack ()); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_THROW (value.popBack (), std::bad_cast); } @@ -5296,7 +5000,7 @@ TEST (Value, contains) ASSERT_THROW (value.contains (0), std::bad_cast); ASSERT_THROW (value.contains ("i"), std::bad_cast); - value = Array ({}); + value = Array{}; ASSERT_FALSE (value.contains (0)); ASSERT_THROW (value.contains ("i"), std::bad_cast); @@ -5308,7 +5012,7 @@ TEST (Value, contains) ASSERT_FALSE (value.contains (0)); ASSERT_FALSE (value.contains ("i")); - value = Object ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value.contains (0)); ASSERT_TRUE (value.contains ("i")); } @@ -5390,7 +5094,7 @@ TEST (Value, swap) ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (other.isString ()); - other = Object ({{"i", 1}}); + other = Object{{"i", 1}}; ASSERT_TRUE (value.isArray ()); ASSERT_TRUE (other.isObject ()); value.swap (other); @@ -5405,7 +5109,7 @@ TEST (Value, equal) { Value value; - value.set (nullptr); + value = nullptr; ASSERT_TRUE (value == nullptr); ASSERT_FALSE (value == true); ASSERT_FALSE (value == false); @@ -5423,7 +5127,7 @@ TEST (Value, equal) ASSERT_FALSE (value == Array{1}); ASSERT_FALSE (value == Object ({{"i", 1}})); - value.set (true); + value = bool (true); ASSERT_FALSE (value == nullptr); ASSERT_TRUE (value == true); ASSERT_FALSE (value == false); @@ -5441,7 +5145,7 @@ TEST (Value, equal) ASSERT_FALSE (value == Array{1}); ASSERT_FALSE (value == Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = int32_t (std::numeric_limits::min ()); ASSERT_FALSE (value == nullptr); ASSERT_FALSE (value == true); ASSERT_FALSE (value == false); @@ -5459,7 +5163,7 @@ TEST (Value, equal) ASSERT_FALSE (value == Array{1}); ASSERT_FALSE (value == Object ({{"i", 1}})); - value.set (std::numeric_limits::max ()); + value = uint32_t (std::numeric_limits::max ()); ASSERT_FALSE (value == nullptr); ASSERT_FALSE (value == true); ASSERT_FALSE (value == false); @@ -5477,7 +5181,7 @@ TEST (Value, equal) ASSERT_FALSE (value == Array{1}); ASSERT_FALSE (value == Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = int64_t (std::numeric_limits::min ()); ASSERT_FALSE (value == nullptr); ASSERT_FALSE (value == true); ASSERT_FALSE (value == false); @@ -5495,7 +5199,7 @@ TEST (Value, equal) ASSERT_FALSE (value == Array{1}); ASSERT_FALSE (value == Object ({{"i", 1}})); - value.set (std::numeric_limits::max ()); + value = uint64_t (std::numeric_limits::max ()); ASSERT_FALSE (value == nullptr); ASSERT_FALSE (value == true); ASSERT_FALSE (value == false); @@ -5513,7 +5217,7 @@ TEST (Value, equal) ASSERT_FALSE (value == Array{1}); ASSERT_FALSE (value == Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = double (std::numeric_limits::min ()); ASSERT_FALSE (value == nullptr); ASSERT_FALSE (value == true); ASSERT_FALSE (value == false); @@ -5531,7 +5235,7 @@ TEST (Value, equal) ASSERT_FALSE (value == Array{1}); ASSERT_FALSE (value == Object ({{"i", 1}})); - value.set ("foo"); + value = std::string ("foo"); ASSERT_FALSE (value == nullptr); ASSERT_FALSE (value == true); ASSERT_FALSE (value == false); @@ -5550,7 +5254,7 @@ TEST (Value, equal) ASSERT_FALSE (value == Array{1}); ASSERT_FALSE (value == Object ({{"i", 1}})); - value.set ({1}); + value = Array{1}; ASSERT_FALSE (value == nullptr); ASSERT_FALSE (value == true); ASSERT_FALSE (value == false); @@ -5568,7 +5272,7 @@ TEST (Value, equal) ASSERT_TRUE (value == Array{1}); ASSERT_FALSE (value == Object ({{"i", 1}})); - value.set ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value == nullptr); ASSERT_FALSE (value == true); ASSERT_FALSE (value == false); @@ -5586,7 +5290,7 @@ TEST (Value, equal) ASSERT_FALSE (value == Array{1}); ASSERT_TRUE (value == Object ({{"i", 1}})); - value.set (nullptr); + value = nullptr; ASSERT_TRUE (nullptr == value); ASSERT_FALSE (true == value); ASSERT_FALSE (false == value); @@ -5604,7 +5308,7 @@ TEST (Value, equal) ASSERT_FALSE (Array{1} == value); ASSERT_FALSE (Object ({{"i", 1}}) == value); - value.set (true); + value = bool (true); ASSERT_FALSE (nullptr == value); ASSERT_TRUE (true == value); ASSERT_FALSE (false == value); @@ -5622,7 +5326,7 @@ TEST (Value, equal) ASSERT_FALSE (Array{1} == value); ASSERT_FALSE (Object ({{"i", 1}}) == value); - value.set (std::numeric_limits::min ()); + value = int32_t (std::numeric_limits::min ()); ASSERT_FALSE (nullptr == value); ASSERT_FALSE (true == value); ASSERT_FALSE (false == value); @@ -5640,7 +5344,7 @@ TEST (Value, equal) ASSERT_FALSE (Array{1} == value); ASSERT_FALSE (Object ({{"i", 1}}) == value); - value.set (std::numeric_limits::max ()); + value = uint32_t (std::numeric_limits::max ()); ASSERT_FALSE (nullptr == value); ASSERT_FALSE (true == value); ASSERT_FALSE (false == value); @@ -5658,7 +5362,7 @@ TEST (Value, equal) ASSERT_FALSE (Array{1} == value); ASSERT_FALSE (Object ({{"i", 1}}) == value); - value.set (std::numeric_limits::min ()); + value = int64_t (std::numeric_limits::min ()); ASSERT_FALSE (nullptr == value); ASSERT_FALSE (true == value); ASSERT_FALSE (false == value); @@ -5676,7 +5380,7 @@ TEST (Value, equal) ASSERT_FALSE (Array{1} == value); ASSERT_FALSE (Object ({{"i", 1}}) == value); - value.set (std::numeric_limits::max ()); + value = uint64_t (std::numeric_limits::max ()); ASSERT_FALSE (nullptr == value); ASSERT_FALSE (true == value); ASSERT_FALSE (false == value); @@ -5694,7 +5398,7 @@ TEST (Value, equal) ASSERT_FALSE (Array{1} == value); ASSERT_FALSE (Object ({{"i", 1}}) == value); - value.set (std::numeric_limits::min ()); + value = double (std::numeric_limits::min ()); ASSERT_FALSE (nullptr == value); ASSERT_FALSE (true == value); ASSERT_FALSE (false == value); @@ -5712,7 +5416,7 @@ TEST (Value, equal) ASSERT_FALSE (Array{1} == value); ASSERT_FALSE (Object ({{"i", 1}}) == value); - value.set ("foo"); + value = std::string ("foo"); ASSERT_FALSE (nullptr == value); ASSERT_FALSE (true == value); ASSERT_FALSE (false == value); @@ -5731,7 +5435,7 @@ TEST (Value, equal) ASSERT_FALSE (Array{1} == value); ASSERT_FALSE (Object ({{"i", 1}}) == value); - value.set ({1}); + value = Array{1}; ASSERT_FALSE (nullptr == value); ASSERT_FALSE (true == value); ASSERT_FALSE (false == value); @@ -5749,7 +5453,7 @@ TEST (Value, equal) ASSERT_TRUE (Array{1} == value); ASSERT_FALSE (Object ({{"i", 1}}) == value); - value.set ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (nullptr == value); ASSERT_FALSE (true == value); ASSERT_FALSE (false == value); @@ -5775,7 +5479,7 @@ TEST (Value, notEqual) { Value value; - value.set (nullptr); + value = nullptr; ASSERT_FALSE (value != nullptr); ASSERT_TRUE (value != true); ASSERT_TRUE (value != false); @@ -5793,7 +5497,7 @@ TEST (Value, notEqual) ASSERT_TRUE (value != Array{1}); ASSERT_TRUE (value != Object ({{"i", 1}})); - value.set (true); + value = bool (true); ASSERT_TRUE (value != nullptr); ASSERT_FALSE (value != true); ASSERT_TRUE (value != false); @@ -5811,7 +5515,7 @@ TEST (Value, notEqual) ASSERT_TRUE (value != Array{1}); ASSERT_TRUE (value != Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = int32_t (std::numeric_limits::min ()); ASSERT_TRUE (value != nullptr); ASSERT_TRUE (value != true); ASSERT_TRUE (value != false); @@ -5829,7 +5533,7 @@ TEST (Value, notEqual) ASSERT_TRUE (value != Array{1}); ASSERT_TRUE (value != Object ({{"i", 1}})); - value.set (std::numeric_limits::max ()); + value = uint32_t (std::numeric_limits::max ()); ASSERT_TRUE (value != nullptr); ASSERT_TRUE (value != true); ASSERT_TRUE (value != false); @@ -5847,7 +5551,7 @@ TEST (Value, notEqual) ASSERT_TRUE (value != Array{1}); ASSERT_TRUE (value != Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = int64_t (std::numeric_limits::min ()); ASSERT_TRUE (value != nullptr); ASSERT_TRUE (value != true); ASSERT_TRUE (value != false); @@ -5865,7 +5569,7 @@ TEST (Value, notEqual) ASSERT_TRUE (value != Array{1}); ASSERT_TRUE (value != Object ({{"i", 1}})); - value.set (std::numeric_limits::max ()); + value = uint64_t (std::numeric_limits::max ()); ASSERT_TRUE (value != nullptr); ASSERT_TRUE (value != true); ASSERT_TRUE (value != false); @@ -5883,7 +5587,7 @@ TEST (Value, notEqual) ASSERT_TRUE (value != Array{1}); ASSERT_TRUE (value != Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = double (std::numeric_limits::min ()); ASSERT_TRUE (value != nullptr); ASSERT_TRUE (value != true); ASSERT_TRUE (value != false); @@ -5901,7 +5605,7 @@ TEST (Value, notEqual) ASSERT_TRUE (value != Array{1}); ASSERT_TRUE (value != Object ({{"i", 1}})); - value.set ("foo"); + value = std::string ("foo"); ASSERT_TRUE (value != nullptr); ASSERT_TRUE (value != true); ASSERT_TRUE (value != false); @@ -5919,7 +5623,7 @@ TEST (Value, notEqual) ASSERT_TRUE (value != Array{1}); ASSERT_TRUE (value != Object ({{"i", 1}})); - value.set ({1}); + value = Array{1}; ASSERT_TRUE (value != nullptr); ASSERT_TRUE (value != true); ASSERT_TRUE (value != false); @@ -5937,7 +5641,7 @@ TEST (Value, notEqual) ASSERT_FALSE (value != Array{1}); ASSERT_TRUE (value != Object ({{"i", 1}})); - value.set ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_TRUE (value != nullptr); ASSERT_TRUE (value != true); ASSERT_TRUE (value != false); @@ -5955,7 +5659,7 @@ TEST (Value, notEqual) ASSERT_TRUE (value != Array{1}); ASSERT_FALSE (value != Object ({{"i", 1}})); - value.set (nullptr); + value = nullptr; ASSERT_FALSE (nullptr != value); ASSERT_TRUE (true != value); ASSERT_TRUE (false != value); @@ -5973,7 +5677,7 @@ TEST (Value, notEqual) ASSERT_TRUE (Array{1} != value); ASSERT_TRUE (Object ({{"i", 1}}) != value); - value.set (true); + value = bool (true); ASSERT_TRUE (nullptr != value); ASSERT_FALSE (true != value); ASSERT_TRUE (false != value); @@ -5991,7 +5695,7 @@ TEST (Value, notEqual) ASSERT_TRUE (Array{1} != value); ASSERT_TRUE (Object ({{"i", 1}}) != value); - value.set (std::numeric_limits::min ()); + value = int32_t (std::numeric_limits::min ()); ASSERT_TRUE (nullptr != value); ASSERT_TRUE (true != value); ASSERT_TRUE (false != value); @@ -6009,7 +5713,7 @@ TEST (Value, notEqual) ASSERT_TRUE (Array{1} != value); ASSERT_TRUE (Object ({{"i", 1}}) != value); - value.set (std::numeric_limits::max ()); + value = uint32_t (std::numeric_limits::max ()); ASSERT_TRUE (nullptr != value); ASSERT_TRUE (true != value); ASSERT_TRUE (false != value); @@ -6027,7 +5731,7 @@ TEST (Value, notEqual) ASSERT_TRUE (Array{1} != value); ASSERT_TRUE (Object ({{"i", 1}}) != value); - value.set (std::numeric_limits::min ()); + value = int64_t (std::numeric_limits::min ()); ASSERT_TRUE (nullptr != value); ASSERT_TRUE (true != value); ASSERT_TRUE (false != value); @@ -6045,7 +5749,7 @@ TEST (Value, notEqual) ASSERT_TRUE (Array{1} != value); ASSERT_TRUE (Object ({{"i", 1}}) != value); - value.set (std::numeric_limits::max ()); + value = uint64_t (std::numeric_limits::max ()); ASSERT_TRUE (nullptr != value); ASSERT_TRUE (true != value); ASSERT_TRUE (false != value); @@ -6063,7 +5767,7 @@ TEST (Value, notEqual) ASSERT_TRUE (Array{1} != value); ASSERT_TRUE (Object ({{"i", 1}}) != value); - value.set (std::numeric_limits::min ()); + value = double (std::numeric_limits::min ()); ASSERT_TRUE (nullptr != value); ASSERT_TRUE (true != value); ASSERT_TRUE (false != value); @@ -6081,7 +5785,7 @@ TEST (Value, notEqual) ASSERT_TRUE (Array{1} != value); ASSERT_TRUE (Object ({{"i", 1}}) != value); - value.set ("foo"); + value = std::string ("foo"); ASSERT_TRUE (nullptr != value); ASSERT_TRUE (true != value); ASSERT_TRUE (false != value); @@ -6100,7 +5804,7 @@ TEST (Value, notEqual) ASSERT_TRUE (Array{1} != value); ASSERT_TRUE (Object ({{"i", 1}}) != value); - value.set ({1}); + value = Array{1}; ASSERT_TRUE (nullptr != value); ASSERT_TRUE (true != value); ASSERT_TRUE (false != value); @@ -6118,7 +5822,7 @@ TEST (Value, notEqual) ASSERT_FALSE (Array{1} != value); ASSERT_TRUE (Object ({{"i", 1}}) != value); - value.set ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_TRUE (nullptr != value); ASSERT_TRUE (true != value); ASSERT_TRUE (false != value); @@ -6144,7 +5848,7 @@ TEST (Value, lower) { Value value; - value.set (nullptr); + value = nullptr; ASSERT_FALSE (value < nullptr); ASSERT_TRUE (value < true); ASSERT_TRUE (value < false); @@ -6162,7 +5866,7 @@ TEST (Value, lower) ASSERT_TRUE (value < Array{1}); ASSERT_TRUE (value < Object ({{"i", 1}})); - value.set (true); + value = bool (true); ASSERT_FALSE (value < nullptr); ASSERT_FALSE (value < true); ASSERT_FALSE (value < false); @@ -6180,7 +5884,7 @@ TEST (Value, lower) ASSERT_TRUE (value < Array{1}); ASSERT_TRUE (value < Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = int32_t (std::numeric_limits::min ()); ASSERT_FALSE (value < nullptr); ASSERT_FALSE (value < true); ASSERT_FALSE (value < false); @@ -6198,7 +5902,7 @@ TEST (Value, lower) ASSERT_TRUE (value < Array{1}); ASSERT_TRUE (value < Object ({{"i", 1}})); - value.set (std::numeric_limits::max ()); + value = int64_t (std::numeric_limits::max ()); ASSERT_FALSE (value < nullptr); ASSERT_FALSE (value < true); ASSERT_FALSE (value < false); @@ -6216,7 +5920,7 @@ TEST (Value, lower) ASSERT_TRUE (value < Array{1}); ASSERT_TRUE (value < Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = double (std::numeric_limits::min ()); ASSERT_FALSE (value < nullptr); ASSERT_FALSE (value < true); ASSERT_FALSE (value < false); @@ -6234,7 +5938,7 @@ TEST (Value, lower) ASSERT_TRUE (value < Array{1}); ASSERT_TRUE (value < Object ({{"i", 1}})); - value.set ("foo"); + value = std::string ("foo"); ASSERT_FALSE (value < nullptr); ASSERT_FALSE (value < true); ASSERT_FALSE (value < false); @@ -6252,7 +5956,7 @@ TEST (Value, lower) ASSERT_TRUE (value < Array{1}); ASSERT_TRUE (value < Object ({{"i", 1}})); - value.set ({1}); + value = Array{1}; ASSERT_FALSE (value < nullptr); ASSERT_FALSE (value < true); ASSERT_FALSE (value < false); @@ -6270,7 +5974,7 @@ TEST (Value, lower) ASSERT_FALSE (value < Array{1}); ASSERT_TRUE (value < Object ({{"i", 1}})); - value.set ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value < nullptr); ASSERT_FALSE (value < true); ASSERT_FALSE (value < false); @@ -6288,7 +5992,7 @@ TEST (Value, lower) ASSERT_FALSE (value < Array{1}); ASSERT_FALSE (value < Object ({{"i", 1}})); - value.set (nullptr); + value = nullptr; ASSERT_FALSE (nullptr < value); ASSERT_FALSE (true < value); ASSERT_FALSE (false < value); @@ -6306,7 +6010,7 @@ TEST (Value, lower) ASSERT_FALSE (Array{1} < value); ASSERT_FALSE (Object ({{"i", 1}}) < value); - value.set (true); + value = bool (true); ASSERT_TRUE (nullptr < value); ASSERT_FALSE (true < value); ASSERT_TRUE (false < value); @@ -6324,7 +6028,7 @@ TEST (Value, lower) ASSERT_FALSE (Array{1} < value); ASSERT_FALSE (Object ({{"i", 1}}) < value); - value.set (std::numeric_limits::min ()); + value = int32_t (std::numeric_limits::min ()); ASSERT_TRUE (nullptr < value); ASSERT_TRUE (true < value); ASSERT_TRUE (false < value); @@ -6342,7 +6046,7 @@ TEST (Value, lower) ASSERT_FALSE (Array{1} < value); ASSERT_FALSE (Object ({{"i", 1}}) < value); - value.set (std::numeric_limits::max ()); + value = int64_t (std::numeric_limits::max ()); ASSERT_TRUE (nullptr < value); ASSERT_TRUE (true < value); ASSERT_TRUE (false < value); @@ -6360,7 +6064,7 @@ TEST (Value, lower) ASSERT_FALSE (Array{1} < value); ASSERT_FALSE (Object ({{"i", 1}}) < value); - value.set (std::numeric_limits::min ()); + value = double (std::numeric_limits::min ()); ASSERT_TRUE (nullptr < value); ASSERT_TRUE (true < value); ASSERT_TRUE (false < value); @@ -6378,7 +6082,7 @@ TEST (Value, lower) ASSERT_FALSE (Array{1} < value); ASSERT_FALSE (Object ({{"i", 1}}) < value); - value.set ("foo"); + value = std::string ("foo"); ASSERT_TRUE (nullptr < value); ASSERT_TRUE (true < value); ASSERT_TRUE (false < value); @@ -6396,7 +6100,7 @@ TEST (Value, lower) ASSERT_FALSE (Array{1} < value); ASSERT_FALSE (Object ({{"i", 1}}) < value); - value.set ({1}); + value = Array{1}; ASSERT_TRUE (nullptr < value); ASSERT_TRUE (true < value); ASSERT_TRUE (false < value); @@ -6414,7 +6118,7 @@ TEST (Value, lower) ASSERT_FALSE (Array{1} < value); ASSERT_FALSE (Object ({{"i", 1}}) < value); - value.set ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_TRUE (nullptr < value); ASSERT_TRUE (true < value); ASSERT_TRUE (false < value); @@ -6440,7 +6144,7 @@ TEST (Value, greater) { Value value; - value.set (nullptr); + value = nullptr; ASSERT_FALSE (value > nullptr); ASSERT_FALSE (value > true); ASSERT_FALSE (value > false); @@ -6458,7 +6162,7 @@ TEST (Value, greater) ASSERT_FALSE (value > Array{1}); ASSERT_FALSE (value > Object ({{"i", 1}})); - value.set (true); + value = bool (true); ASSERT_TRUE (value > nullptr); ASSERT_FALSE (value > true); ASSERT_TRUE (value > false); @@ -6476,7 +6180,7 @@ TEST (Value, greater) ASSERT_FALSE (value > Array{1}); ASSERT_FALSE (value > Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = int32_t (std::numeric_limits::min ()); ASSERT_TRUE (value > nullptr); ASSERT_TRUE (value > true); ASSERT_TRUE (value > false); @@ -6494,7 +6198,7 @@ TEST (Value, greater) ASSERT_FALSE (value > Array{1}); ASSERT_FALSE (value > Object ({{"i", 1}})); - value.set (std::numeric_limits::max ()); + value = int64_t (std::numeric_limits::max ()); ASSERT_TRUE (value > nullptr); ASSERT_TRUE (value > true); ASSERT_TRUE (value > false); @@ -6512,7 +6216,7 @@ TEST (Value, greater) ASSERT_FALSE (value > Array{1}); ASSERT_FALSE (value > Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = double (std::numeric_limits::min ()); ASSERT_TRUE (value > nullptr); ASSERT_TRUE (value > true); ASSERT_TRUE (value > false); @@ -6530,7 +6234,7 @@ TEST (Value, greater) ASSERT_FALSE (value > Array{1}); ASSERT_FALSE (value > Object ({{"i", 1}})); - value.set ("foo"); + value = std::string ("foo"); ASSERT_TRUE (value > nullptr); ASSERT_TRUE (value > true); ASSERT_TRUE (value > false); @@ -6548,7 +6252,7 @@ TEST (Value, greater) ASSERT_FALSE (value > Array{1}); ASSERT_FALSE (value > Object ({{"i", 1}})); - value.set ({1}); + value = Array{1}; ASSERT_TRUE (value > nullptr); ASSERT_TRUE (value > true); ASSERT_TRUE (value > false); @@ -6566,7 +6270,7 @@ TEST (Value, greater) ASSERT_FALSE (value > Array{1}); ASSERT_FALSE (value > Object ({{"i", 1}})); - value.set ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_TRUE (value > nullptr); ASSERT_TRUE (value > true); ASSERT_TRUE (value > false); @@ -6584,7 +6288,7 @@ TEST (Value, greater) ASSERT_TRUE (value > Array{1}); ASSERT_FALSE (value > Object ({{"i", 1}})); - value.set (nullptr); + value = nullptr; ASSERT_FALSE (nullptr > value); ASSERT_TRUE (true > value); ASSERT_TRUE (false > value); @@ -6602,7 +6306,7 @@ TEST (Value, greater) ASSERT_TRUE (Array{1} > value); ASSERT_TRUE (Object ({{"i", 1}}) > value); - value.set (true); + value = bool (true); ASSERT_FALSE (nullptr > value); ASSERT_FALSE (true > value); ASSERT_FALSE (false > value); @@ -6620,7 +6324,7 @@ TEST (Value, greater) ASSERT_TRUE (Array{1} > value); ASSERT_TRUE (Object ({{"i", 1}}) > value); - value.set (std::numeric_limits::min ()); + value = int32_t (std::numeric_limits::min ()); ASSERT_FALSE (nullptr > value); ASSERT_FALSE (true > value); ASSERT_FALSE (false > value); @@ -6638,7 +6342,7 @@ TEST (Value, greater) ASSERT_TRUE (Array{1} > value); ASSERT_TRUE (Object ({{"i", 1}}) > value); - value.set (std::numeric_limits::max ()); + value = int64_t (std::numeric_limits::max ()); ASSERT_FALSE (nullptr > value); ASSERT_FALSE (true > value); ASSERT_FALSE (false > value); @@ -6656,7 +6360,7 @@ TEST (Value, greater) ASSERT_TRUE (Array{1} > value); ASSERT_TRUE (Object ({{"i", 1}}) > value); - value.set (std::numeric_limits::min ()); + value = double (std::numeric_limits::min ()); ASSERT_FALSE (nullptr > value); ASSERT_FALSE (true > value); ASSERT_FALSE (false > value); @@ -6674,7 +6378,7 @@ TEST (Value, greater) ASSERT_TRUE (Array{1} > value); ASSERT_TRUE (Object ({{"i", 1}}) > value); - value.set ("foo"); + value = std::string ("foo"); ASSERT_FALSE (nullptr > value); ASSERT_FALSE (true > value); ASSERT_FALSE (false > value); @@ -6692,7 +6396,7 @@ TEST (Value, greater) ASSERT_TRUE (Array{1} > value); ASSERT_TRUE (Object ({{"i", 1}}) > value); - value.set ({1}); + value = Array{1}; ASSERT_FALSE (nullptr > value); ASSERT_FALSE (true > value); ASSERT_FALSE (false > value); @@ -6710,7 +6414,7 @@ TEST (Value, greater) ASSERT_FALSE (Array{1} > value); ASSERT_TRUE (Object ({{"i", 1}}) > value); - value.set ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (nullptr > value); ASSERT_FALSE (true > value); ASSERT_FALSE (false > value); @@ -6736,7 +6440,7 @@ TEST (Value, lowerOrEqual) { Value value; - value.set (nullptr); + value = nullptr; ASSERT_TRUE (value <= nullptr); ASSERT_TRUE (value <= true); ASSERT_TRUE (value <= false); @@ -6754,7 +6458,7 @@ TEST (Value, lowerOrEqual) ASSERT_TRUE (value <= Array{1}); ASSERT_TRUE (value <= Object ({{"i", 1}})); - value.set (true); + value = bool (true); ASSERT_FALSE (value <= nullptr); ASSERT_TRUE (value <= true); ASSERT_FALSE (value <= false); @@ -6772,7 +6476,7 @@ TEST (Value, lowerOrEqual) ASSERT_TRUE (value <= Array{1}); ASSERT_TRUE (value <= Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = int32_t (std::numeric_limits::min ()); ASSERT_FALSE (value <= nullptr); ASSERT_FALSE (value <= true); ASSERT_FALSE (value <= false); @@ -6790,7 +6494,7 @@ TEST (Value, lowerOrEqual) ASSERT_TRUE (value <= Array{1}); ASSERT_TRUE (value <= Object ({{"i", 1}})); - value.set (std::numeric_limits::max ()); + value = uint32_t (std::numeric_limits::max ()); ASSERT_FALSE (value <= nullptr); ASSERT_FALSE (value <= true); ASSERT_FALSE (value <= false); @@ -6808,7 +6512,7 @@ TEST (Value, lowerOrEqual) ASSERT_TRUE (value <= Array{1}); ASSERT_TRUE (value <= Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = int64_t (std::numeric_limits::min ()); ASSERT_FALSE (value <= nullptr); ASSERT_FALSE (value <= true); ASSERT_FALSE (value <= false); @@ -6826,7 +6530,7 @@ TEST (Value, lowerOrEqual) ASSERT_TRUE (value <= Array{1}); ASSERT_TRUE (value <= Object ({{"i", 1}})); - value.set (std::numeric_limits::max ()); + value = uint64_t (std::numeric_limits::max ()); ASSERT_FALSE (value <= nullptr); ASSERT_FALSE (value <= true); ASSERT_FALSE (value <= false); @@ -6844,7 +6548,7 @@ TEST (Value, lowerOrEqual) ASSERT_TRUE (value <= Array{1}); ASSERT_TRUE (value <= Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = double (std::numeric_limits::min ()); ASSERT_FALSE (value <= nullptr); ASSERT_FALSE (value <= true); ASSERT_FALSE (value <= false); @@ -6862,7 +6566,7 @@ TEST (Value, lowerOrEqual) ASSERT_TRUE (value <= Array{1}); ASSERT_TRUE (value <= Object ({{"i", 1}})); - value.set ("foo"); + value = std::string ("foo"); ASSERT_FALSE (value <= nullptr); ASSERT_FALSE (value <= true); ASSERT_FALSE (value <= false); @@ -6880,7 +6584,7 @@ TEST (Value, lowerOrEqual) ASSERT_TRUE (value <= Array{1}); ASSERT_TRUE (value <= Object ({{"i", 1}})); - value.set ({1}); + value = Array{1}; ASSERT_FALSE (value <= nullptr); ASSERT_FALSE (value <= true); ASSERT_FALSE (value <= false); @@ -6898,7 +6602,7 @@ TEST (Value, lowerOrEqual) ASSERT_TRUE (value <= Array{1}); ASSERT_TRUE (value <= Object ({{"i", 1}})); - value.set ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (value <= nullptr); ASSERT_FALSE (value <= true); ASSERT_FALSE (value <= false); @@ -6916,7 +6620,7 @@ TEST (Value, lowerOrEqual) ASSERT_FALSE (value <= Array{1}); ASSERT_TRUE (value <= Object ({{"i", 1}})); - value.set (nullptr); + value = nullptr; ASSERT_TRUE (nullptr <= value); ASSERT_FALSE (true <= value); ASSERT_FALSE (false <= value); @@ -6935,7 +6639,7 @@ TEST (Value, lowerOrEqual) ASSERT_FALSE (Array{1} <= value); ASSERT_FALSE (Object ({{"i", 1}}) <= value); - value.set (true); + value = bool (true); ASSERT_TRUE (nullptr <= value); ASSERT_TRUE (true <= value); ASSERT_TRUE (false <= value); @@ -6953,7 +6657,7 @@ TEST (Value, lowerOrEqual) ASSERT_FALSE (Array{1} <= value); ASSERT_FALSE (Object ({{"i", 1}}) <= value); - value.set (std::numeric_limits::min ()); + value = int32_t (std::numeric_limits::min ()); ASSERT_TRUE (nullptr <= value); ASSERT_TRUE (true <= value); ASSERT_TRUE (false <= value); @@ -6971,7 +6675,7 @@ TEST (Value, lowerOrEqual) ASSERT_FALSE (Array{1} <= value); ASSERT_FALSE (Object ({{"i", 1}}) <= value); - value.set (std::numeric_limits::max ()); + value = uint32_t (std::numeric_limits::max ()); ASSERT_TRUE (nullptr <= value); ASSERT_TRUE (true <= value); ASSERT_TRUE (false <= value); @@ -6989,7 +6693,7 @@ TEST (Value, lowerOrEqual) ASSERT_FALSE (Array{1} <= value); ASSERT_FALSE (Object ({{"i", 1}}) <= value); - value.set (std::numeric_limits::min ()); + value = int64_t (std::numeric_limits::min ()); ASSERT_TRUE (nullptr <= value); ASSERT_TRUE (true <= value); ASSERT_TRUE (false <= value); @@ -7007,7 +6711,7 @@ TEST (Value, lowerOrEqual) ASSERT_FALSE (Array{1} <= value); ASSERT_FALSE (Object ({{"i", 1}}) <= value); - value.set (std::numeric_limits::max ()); + value = uint64_t (std::numeric_limits::max ()); ASSERT_TRUE (nullptr <= value); ASSERT_TRUE (true <= value); ASSERT_TRUE (false <= value); @@ -7025,7 +6729,7 @@ TEST (Value, lowerOrEqual) ASSERT_FALSE (Array{1} <= value); ASSERT_FALSE (Object ({{"i", 1}}) <= value); - value.set (std::numeric_limits::min ()); + value = double (std::numeric_limits::min ()); ASSERT_TRUE (nullptr <= value); ASSERT_TRUE (true <= value); ASSERT_TRUE (false <= value); @@ -7043,7 +6747,7 @@ TEST (Value, lowerOrEqual) ASSERT_FALSE (Array{1} <= value); ASSERT_FALSE (Object ({{"i", 1}}) <= value); - value.set ("foo"); + value = std::string ("foo"); ASSERT_TRUE (nullptr <= value); ASSERT_TRUE (true <= value); ASSERT_TRUE (false <= value); @@ -7061,7 +6765,7 @@ TEST (Value, lowerOrEqual) ASSERT_FALSE (Array{1} <= value); ASSERT_FALSE (Object ({{"i", 1}}) <= value); - value.set ({1}); + value = Array{1}; ASSERT_TRUE (nullptr <= value); ASSERT_TRUE (true <= value); ASSERT_TRUE (false <= value); @@ -7079,7 +6783,7 @@ TEST (Value, lowerOrEqual) ASSERT_TRUE (Array{1} <= value); ASSERT_FALSE (Object ({{"i", 1}}) <= value); - value.set ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_TRUE (nullptr <= value); ASSERT_TRUE (true <= value); ASSERT_TRUE (false <= value); @@ -7105,7 +6809,7 @@ TEST (Value, greaterOrEqual) { Value value; - value.set (nullptr); + value = nullptr; ASSERT_TRUE (value >= nullptr); ASSERT_FALSE (value >= true); ASSERT_FALSE (value >= false); @@ -7123,7 +6827,7 @@ TEST (Value, greaterOrEqual) ASSERT_FALSE (value >= Array{1}); ASSERT_FALSE (value >= Object ({{"i", 1}})); - value.set (true); + value = bool (true); ASSERT_TRUE (value >= nullptr); ASSERT_TRUE (value >= true); ASSERT_TRUE (value >= false); @@ -7141,7 +6845,7 @@ TEST (Value, greaterOrEqual) ASSERT_FALSE (value >= Array{1}); ASSERT_FALSE (value >= Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = int32_t (std::numeric_limits::min ()); ASSERT_TRUE (value >= nullptr); ASSERT_TRUE (value >= true); ASSERT_TRUE (value >= false); @@ -7159,7 +6863,7 @@ TEST (Value, greaterOrEqual) ASSERT_FALSE (value >= Array{1}); ASSERT_FALSE (value >= Object ({{"i", 1}})); - value.set (std::numeric_limits::max ()); + value = uint32_t (std::numeric_limits::max ()); ASSERT_TRUE (value >= nullptr); ASSERT_TRUE (value >= true); ASSERT_TRUE (value >= false); @@ -7177,7 +6881,7 @@ TEST (Value, greaterOrEqual) ASSERT_FALSE (value >= Array{1}); ASSERT_FALSE (value >= Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = int64_t (std::numeric_limits::min ()); ASSERT_TRUE (value >= nullptr); ASSERT_TRUE (value >= true); ASSERT_TRUE (value >= false); @@ -7195,7 +6899,7 @@ TEST (Value, greaterOrEqual) ASSERT_FALSE (value >= Array{1}); ASSERT_FALSE (value >= Object ({{"i", 1}})); - value.set (std::numeric_limits::max ()); + value = uint64_t (std::numeric_limits::max ()); ASSERT_TRUE (value >= nullptr); ASSERT_TRUE (value >= true); ASSERT_TRUE (value >= false); @@ -7213,7 +6917,7 @@ TEST (Value, greaterOrEqual) ASSERT_FALSE (value >= Array{1}); ASSERT_FALSE (value >= Object ({{"i", 1}})); - value.set (std::numeric_limits::min ()); + value = double (std::numeric_limits::min ()); ASSERT_TRUE (value >= nullptr); ASSERT_TRUE (value >= true); ASSERT_TRUE (value >= false); @@ -7231,7 +6935,7 @@ TEST (Value, greaterOrEqual) ASSERT_FALSE (value >= Array{1}); ASSERT_FALSE (value >= Object ({{"i", 1}})); - value.set ("foo"); + value = std::string ("foo"); ASSERT_TRUE (value >= nullptr); ASSERT_TRUE (value >= true); ASSERT_TRUE (value >= false); @@ -7249,7 +6953,7 @@ TEST (Value, greaterOrEqual) ASSERT_FALSE (value >= Array{1}); ASSERT_FALSE (value >= Object ({{"i", 1}})); - value.set ({1}); + value = Array{1}; ASSERT_TRUE (value >= nullptr); ASSERT_TRUE (value >= true); ASSERT_TRUE (value >= false); @@ -7267,7 +6971,7 @@ TEST (Value, greaterOrEqual) ASSERT_TRUE (value >= Array{1}); ASSERT_FALSE (value >= Object ({{"i", 1}})); - value.set ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_TRUE (value >= nullptr); ASSERT_TRUE (value >= true); ASSERT_TRUE (value >= false); @@ -7285,7 +6989,7 @@ TEST (Value, greaterOrEqual) ASSERT_TRUE (value >= Array{1}); ASSERT_TRUE (value >= Object ({{"i", 1}})); - value.set (nullptr); + value = nullptr; ASSERT_TRUE (nullptr >= value); ASSERT_TRUE (true >= value); ASSERT_TRUE (false >= value); @@ -7303,7 +7007,7 @@ TEST (Value, greaterOrEqual) ASSERT_TRUE (Array{1} >= value); ASSERT_TRUE (Object ({{"i", 1}}) >= value); - value.set (true); + value = bool (true); ASSERT_FALSE (nullptr >= value); ASSERT_TRUE (true >= value); ASSERT_FALSE (false >= value); @@ -7321,7 +7025,7 @@ TEST (Value, greaterOrEqual) ASSERT_TRUE (Array{1} >= value); ASSERT_TRUE (Object ({{"i", 1}}) >= value); - value.set (std::numeric_limits::min ()); + value = int32_t (std::numeric_limits::min ()); ASSERT_FALSE (nullptr >= value); ASSERT_FALSE (true >= value); ASSERT_FALSE (false >= value); @@ -7339,7 +7043,7 @@ TEST (Value, greaterOrEqual) ASSERT_TRUE (Array{1} >= value); ASSERT_TRUE (Object ({{"i", 1}}) >= value); - value.set (std::numeric_limits::max ()); + value = uint32_t (std::numeric_limits::max ()); ASSERT_FALSE (nullptr >= value); ASSERT_FALSE (true >= value); ASSERT_FALSE (false >= value); @@ -7357,7 +7061,7 @@ TEST (Value, greaterOrEqual) ASSERT_TRUE (Array{1} >= value); ASSERT_TRUE (Object ({{"i", 1}}) >= value); - value.set (std::numeric_limits::min ()); + value = int64_t (std::numeric_limits::min ()); ASSERT_FALSE (nullptr >= value); ASSERT_FALSE (true >= value); ASSERT_FALSE (false >= value); @@ -7375,7 +7079,7 @@ TEST (Value, greaterOrEqual) ASSERT_TRUE (Array{1} >= value); ASSERT_TRUE (Object ({{"i", 1}}) >= value); - value.set (std::numeric_limits::max ()); + value = uint64_t (std::numeric_limits::max ()); ASSERT_FALSE (nullptr >= value); ASSERT_FALSE (true >= value); ASSERT_FALSE (false >= value); @@ -7393,7 +7097,7 @@ TEST (Value, greaterOrEqual) ASSERT_TRUE (Array{1} >= value); ASSERT_TRUE (Object ({{"i", 1}}) >= value); - value.set (std::numeric_limits::min ()); + value = double (std::numeric_limits::min ()); ASSERT_FALSE (nullptr >= value); ASSERT_FALSE (true >= value); ASSERT_FALSE (false >= value); @@ -7411,7 +7115,7 @@ TEST (Value, greaterOrEqual) ASSERT_TRUE (Array{1} >= value); ASSERT_TRUE (Object ({{"i", 1}}) >= value); - value.set ("foo"); + value = std::string ("foo"); ASSERT_FALSE (nullptr >= value); ASSERT_FALSE (true >= value); ASSERT_FALSE (false >= value); @@ -7429,7 +7133,7 @@ TEST (Value, greaterOrEqual) ASSERT_TRUE (Array{1} >= value); ASSERT_TRUE (Object ({{"i", 1}}) >= value); - value.set ({1}); + value = Array{1}; ASSERT_FALSE (nullptr >= value); ASSERT_FALSE (true >= value); ASSERT_FALSE (false >= value); @@ -7447,7 +7151,7 @@ TEST (Value, greaterOrEqual) ASSERT_TRUE (Array{1} >= value); ASSERT_TRUE (Object ({{"i", 1}}) >= value); - value.set ({{"i", 1}}); + value = Object{{"i", 1}}; ASSERT_FALSE (nullptr >= value); ASSERT_FALSE (true >= value); ASSERT_FALSE (false >= value); From 85e4db2e960d495ee8a74251d019ffb375fc8e4c Mon Sep 17 00:00:00 2001 From: mrabine Date: Sun, 18 Oct 2026 01:09:52 +0200 Subject: [PATCH 2/2] fix emplace back --- data/include/join/value.hpp | 211 ++++++++++++++---------------------- data/tests/value_test.cpp | 15 +++ 2 files changed, 96 insertions(+), 130 deletions(-) diff --git a/data/include/join/value.hpp b/data/include/join/value.hpp index 8ca476d5..0802f4e9 100644 --- a/data/include/join/value.hpp +++ b/data/include/join/value.hpp @@ -364,14 +364,28 @@ namespace join */ constexpr int8_t getInt8 () const { - if (_data.index () == Integer && - _data.get () >= static_cast (std::numeric_limits::min ()) && - _data.get () <= static_cast (std::numeric_limits::max ())) + if (isInt8 ()) { - return static_cast (_data.get ()); + switch (_data.index ()) + { + case Integer: + return static_cast (_data.get ()); + + case Unsigned: + return static_cast (_data.get ()); + + case Integer64: + return static_cast (_data.get ()); + + case Unsigned64: + return static_cast (_data.get ()); + + default: + return static_cast (_data.get ()); + } } - return convertInt8 (); + throw std::bad_cast (); } /** @@ -424,13 +438,28 @@ namespace join */ constexpr uint8_t getUint8 () const { - if (_data.index () == Integer && _data.get () >= 0 && - _data.get () <= static_cast (std::numeric_limits::max ())) + if (isUint8 ()) { - return static_cast (_data.get ()); + switch (_data.index ()) + { + case Integer: + return static_cast (_data.get ()); + + case Unsigned: + return static_cast (_data.get ()); + + case Integer64: + return static_cast (_data.get ()); + + case Unsigned64: + return static_cast (_data.get ()); + + default: + return static_cast (_data.get ()); + } } - return convertUint8 (); + throw std::bad_cast (); } /** @@ -484,14 +513,28 @@ namespace join */ constexpr int16_t getInt16 () const { - if (_data.index () == Integer && - _data.get () >= static_cast (std::numeric_limits::min ()) && - _data.get () <= static_cast (std::numeric_limits::max ())) + if (isInt16 ()) { - return static_cast (_data.get ()); + switch (_data.index ()) + { + case Integer: + return static_cast (_data.get ()); + + case Unsigned: + return static_cast (_data.get ()); + + case Integer64: + return static_cast (_data.get ()); + + case Unsigned64: + return static_cast (_data.get ()); + + default: + return static_cast (_data.get ()); + } } - return convertInt16 (); + throw std::bad_cast (); } /** @@ -544,13 +587,28 @@ namespace join */ constexpr uint16_t getUint16 () const { - if (_data.index () == Integer && _data.get () >= 0 && - _data.get () <= static_cast (std::numeric_limits::max ())) + if (isUint16 ()) { - return static_cast (_data.get ()); + switch (_data.index ()) + { + case Integer: + return static_cast (_data.get ()); + + case Unsigned: + return static_cast (_data.get ()); + + case Integer64: + return static_cast (_data.get ()); + + case Unsigned64: + return static_cast (_data.get ()); + + default: + return static_cast (_data.get ()); + } } - return convertUint16 (); + throw std::bad_cast (); } /** @@ -1184,9 +1242,9 @@ namespace join } /** - * @brief appends element at the end of the nested container. - * @param value the element to append. - * @return a reference to the pushed object. + * @brief constructs an element in place at the end of the nested container. + * @param args arguments forwarded to the element constructor. + * @return a reference to the constructed object. * @throw std::bad_cast. */ template @@ -1196,7 +1254,8 @@ namespace join { _data.set (); } - return _data.get ().emplace_back (std::forward (args)...); + _data.get ().emplace_back (std::forward (args)...); + return _data.get ().back (); } /** @@ -1281,114 +1340,6 @@ namespace join friend constexpr bool operator< (const Value& lhs, const Value& rhs); private: - /** - * @brief convert the value held by the variant to int8_t. - * @return converted value. - */ - int8_t convertInt8 () const - { - if (isInt8 ()) - { - switch (_data.index ()) - { - case Unsigned: - return static_cast (_data.get ()); - - case Integer64: - return static_cast (_data.get ()); - - case Unsigned64: - return static_cast (_data.get ()); - - default: - return static_cast (_data.get ()); - } - } - - throw std::bad_cast (); - } - - /** - * @brief convert the value held by the variant to uint8_t. - * @return converted value. - */ - uint8_t convertUint8 () const - { - if (isUint8 ()) - { - switch (_data.index ()) - { - case Unsigned: - return static_cast (_data.get ()); - - case Integer64: - return static_cast (_data.get ()); - - case Unsigned64: - return static_cast (_data.get ()); - - default: - return static_cast (_data.get ()); - } - } - - throw std::bad_cast (); - } - - /** - * @brief convert the value held by the variant to int16_t. - * @return converted value. - */ - int16_t convertInt16 () const - { - if (isInt16 ()) - { - switch (_data.index ()) - { - case Unsigned: - return static_cast (_data.get ()); - - case Integer64: - return static_cast (_data.get ()); - - case Unsigned64: - return static_cast (_data.get ()); - - default: - return static_cast (_data.get ()); - } - } - - throw std::bad_cast (); - } - - /** - * @brief convert the value held by the variant to uint16_t. - * @return converted value. - */ - uint16_t convertUint16 () const - { - if (isUint16 ()) - { - switch (_data.index ()) - { - case Unsigned: - return static_cast (_data.get ()); - - case Integer64: - return static_cast (_data.get ()); - - case Unsigned64: - return static_cast (_data.get ()); - - default: - return static_cast (_data.get ()); - } - } - - throw std::bad_cast (); - } - /** * @brief convert the value held by the variant to float. * @return converted value. diff --git a/data/tests/value_test.cpp b/data/tests/value_test.cpp index a63c3d7e..4dcd1680 100644 --- a/data/tests/value_test.cpp +++ b/data/tests/value_test.cpp @@ -4691,6 +4691,21 @@ TEST (Value, erase) ASSERT_EQ (value.erase ("j"), 0); } +/** + * @brief Test emplaceBack method. + */ +TEST (Value, emplaceBack) +{ + Value value; + ASSERT_EQ (value.emplaceBack (1), 1); + ASSERT_EQ (value.emplaceBack ("foo"), "foo"); + ASSERT_EQ (value.size (), 2U); + ASSERT_EQ (value[1], "foo"); + + value = true; + ASSERT_THROW (value.emplaceBack (1), std::bad_cast); +} + /** * @brief Test pushBack method. */