From 540c58964dba265453d13c43ffd93a21504c4157 Mon Sep 17 00:00:00 2001 From: Niels Date: Mon, 29 Jun 2015 20:53:01 +0200 Subject: [PATCH] closed #91 --- src/json.hpp | 99 ++++++++++++++++++++++++++++++++++++++--------- src/json.hpp.re2c | 90 +++++++++++++++++++++++++++++++++++------- 2 files changed, 155 insertions(+), 34 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 8f6f50d4..5190c64e 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -1860,37 +1860,73 @@ class basic_json } /// get a pointer to the value (object) - const object_t* get_impl_ptr(object_t*) const noexcept + object_t* get_impl_ptr(object_t*) noexcept + { + return is_object() ? m_value.object : nullptr; + } + + /// get a pointer to the value (object) + const object_t* get_impl_ptr(const object_t*) const noexcept { return is_object() ? m_value.object : nullptr; } /// get a pointer to the value (array) - const array_t* get_impl_ptr(array_t*) const noexcept + array_t* get_impl_ptr(array_t*) noexcept + { + return is_array() ? m_value.array : nullptr; + } + + /// get a pointer to the value (array) + const array_t* get_impl_ptr(const array_t*) const noexcept { return is_array() ? m_value.array : nullptr; } /// get a pointer to the value (string) - const string_t* get_impl_ptr(string_t*) const noexcept + string_t* get_impl_ptr(string_t*) noexcept + { + return is_string() ? m_value.string : nullptr; + } + + /// get a pointer to the value (string) + const string_t* get_impl_ptr(const string_t*) const noexcept { return is_string() ? m_value.string : nullptr; } /// get a pointer to the value (boolean) - const boolean_t* get_impl_ptr(boolean_t*) const noexcept + boolean_t* get_impl_ptr(boolean_t*) noexcept + { + return is_boolean() ? &m_value.boolean : nullptr; + } + + /// get a pointer to the value (boolean) + const boolean_t* get_impl_ptr(const boolean_t*) const noexcept { return is_boolean() ? &m_value.boolean : nullptr; } /// get a pointer to the value (integer number) - const number_integer_t* get_impl_ptr(number_integer_t*) const noexcept + number_integer_t* get_impl_ptr(number_integer_t*) noexcept + { + return is_number_integer() ? &m_value.number_integer : nullptr; + } + + /// get a pointer to the value (integer number) + const number_integer_t* get_impl_ptr(const number_integer_t*) const noexcept { return is_number_integer() ? &m_value.number_integer : nullptr; } /// get a pointer to the value (floating-point number) - const number_float_t* get_impl_ptr(number_float_t*) const noexcept + number_float_t* get_impl_ptr(number_float_t*) noexcept + { + return is_number_float() ? &m_value.number_float : nullptr; + } + + /// get a pointer to the value (floating-point number) + const number_float_t* get_impl_ptr(const number_float_t*) const noexcept { return is_number_float() ? &m_value.number_float : nullptr; } @@ -1969,7 +2005,21 @@ class basic_json std::enable_if< std::is_pointer::value , int>::type = 0> - PointerType get() const noexcept + PointerType get() noexcept + { + // delegate the call to get_ptr + return get_ptr(); + } + + /*! + @brief get a pointer value (explicit) + @copydoc get() + */ + template::value + , int>::type = 0> + const PointerType get() const noexcept { // delegate the call to get_ptr return get_ptr(); @@ -2002,15 +2052,25 @@ class basic_json std::enable_if< std::is_pointer::value , int>::type = 0> - PointerType get_ptr() const noexcept + PointerType get_ptr() noexcept { - // get_impl_ptr will only work with non-const and non-volatile pointer - // types. Therefore, we case away all cv properties to be able to - // select the correct function. The cv properties will then be added - // again by the const const cast to PointerType. - return const_cast(get_impl_ptr( - static_cast::type>::type>::type> - (nullptr))); + // delegate the call to get_impl_ptr<>() + return get_impl_ptr(static_cast(nullptr)); + } + + /*! + @brief get a pointer value (implicit) + @copydoc get_ptr() + */ + template::value + and std::is_const::value + , int>::type = 0> + const PointerType get_ptr() const noexcept + { + // delegate the call to get_impl_ptr<>() const + return get_impl_ptr(static_cast(nullptr)); } /*! @@ -6326,15 +6386,16 @@ basic_json_parser_59: { public: /// constructor for strings - parser(const string_t& s, parser_callback_t cb = nullptr) : callback(cb), m_lexer(s) + parser(const string_t& s, parser_callback_t cb = nullptr) + : callback(cb), m_lexer(s) { // read first token get_token(); } /// a parser reading from an input stream - parser(std::istream& _is, parser_callback_t cb = nullptr) : callback(cb), - m_lexer(&_is) + parser(std::istream& _is, parser_callback_t cb = nullptr) + : callback(cb), m_lexer(&_is) { // read first token get_token(); @@ -6601,7 +6662,7 @@ basic_json_parser_59: } private: - /// levels of recursion + /// current level of recursion int depth = 0; /// callback function parser_callback_t callback; diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index c96bc60e..3650c8e7 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -1860,37 +1860,73 @@ class basic_json } /// get a pointer to the value (object) - const object_t* get_impl_ptr(object_t*) const noexcept + object_t* get_impl_ptr(object_t*) noexcept + { + return is_object() ? m_value.object : nullptr; + } + + /// get a pointer to the value (object) + const object_t* get_impl_ptr(const object_t*) const noexcept { return is_object() ? m_value.object : nullptr; } /// get a pointer to the value (array) - const array_t* get_impl_ptr(array_t*) const noexcept + array_t* get_impl_ptr(array_t*) noexcept + { + return is_array() ? m_value.array : nullptr; + } + + /// get a pointer to the value (array) + const array_t* get_impl_ptr(const array_t*) const noexcept { return is_array() ? m_value.array : nullptr; } /// get a pointer to the value (string) - const string_t* get_impl_ptr(string_t*) const noexcept + string_t* get_impl_ptr(string_t*) noexcept + { + return is_string() ? m_value.string : nullptr; + } + + /// get a pointer to the value (string) + const string_t* get_impl_ptr(const string_t*) const noexcept { return is_string() ? m_value.string : nullptr; } /// get a pointer to the value (boolean) - const boolean_t* get_impl_ptr(boolean_t*) const noexcept + boolean_t* get_impl_ptr(boolean_t*) noexcept + { + return is_boolean() ? &m_value.boolean : nullptr; + } + + /// get a pointer to the value (boolean) + const boolean_t* get_impl_ptr(const boolean_t*) const noexcept { return is_boolean() ? &m_value.boolean : nullptr; } /// get a pointer to the value (integer number) - const number_integer_t* get_impl_ptr(number_integer_t*) const noexcept + number_integer_t* get_impl_ptr(number_integer_t*) noexcept + { + return is_number_integer() ? &m_value.number_integer : nullptr; + } + + /// get a pointer to the value (integer number) + const number_integer_t* get_impl_ptr(const number_integer_t*) const noexcept { return is_number_integer() ? &m_value.number_integer : nullptr; } /// get a pointer to the value (floating-point number) - const number_float_t* get_impl_ptr(number_float_t*) const noexcept + number_float_t* get_impl_ptr(number_float_t*) noexcept + { + return is_number_float() ? &m_value.number_float : nullptr; + } + + /// get a pointer to the value (floating-point number) + const number_float_t* get_impl_ptr(const number_float_t*) const noexcept { return is_number_float() ? &m_value.number_float : nullptr; } @@ -1969,7 +2005,21 @@ class basic_json std::enable_if< std::is_pointer::value , int>::type = 0> - PointerType get() const noexcept + PointerType get() noexcept + { + // delegate the call to get_ptr + return get_ptr(); + } + + /*! + @brief get a pointer value (explicit) + @copydoc get() + */ + template::value + , int>::type = 0> + const PointerType get() const noexcept { // delegate the call to get_ptr return get_ptr(); @@ -2002,15 +2052,25 @@ class basic_json std::enable_if< std::is_pointer::value , int>::type = 0> - PointerType get_ptr() const noexcept + PointerType get_ptr() noexcept { - // get_impl_ptr will only work with non-const and non-volatile pointer - // types. Therefore, we case away all cv properties to be able to - // select the correct function. The cv properties will then be added - // again by the const const cast to PointerType. - return const_cast(get_impl_ptr( - static_cast::type>::type>::type> - (nullptr))); + // delegate the call to get_impl_ptr<>() + return get_impl_ptr(static_cast(nullptr)); + } + + /*! + @brief get a pointer value (implicit) + @copydoc get_ptr() + */ + template::value + and std::is_const::value + , int>::type = 0> + const PointerType get_ptr() const noexcept + { + // delegate the call to get_impl_ptr<>() const + return get_impl_ptr(static_cast(nullptr)); } /*!