From 07bc82d3a7aac7092bcc7d3465779c32b4606364 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20DELRIEU?= Date: Sun, 15 Jan 2017 20:19:54 +0100 Subject: [PATCH] put const to the left --- src/json.hpp | 54 ++++++++++++++++---------------- test/src/unit-udt.cpp | 72 +++++++++++++++++++++---------------------- 2 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 75fece93..2457f5d7 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -168,7 +168,7 @@ using is_unscoped_enum = namespace detail { -template std::string type_name(Json const &j) +template std::string type_name(const Json &j) { switch (j.m_type) { @@ -496,7 +496,7 @@ template ::value, int> = 0> -void get_arithmetic_value(Json const &j, ArithmeticType &val) +void get_arithmetic_value(const Json &j, ArithmeticType &val) { // unsigned must be checked first, since is_number_integer() == true for unsigned if (j.is_number_unsigned()) @@ -565,7 +565,7 @@ template < is_compatible_array_type::value or std::is_same::value, int> = 0> -void to_json(Json &j, CompatibleArrayType const &arr) +void to_json(Json &j, const CompatibleArrayType &arr) { external_constructor::construct(j, arr); } @@ -574,13 +574,13 @@ template < typename Json, typename CompatibleObjectType, enable_if_t::value, int> = 0> -void to_json(Json &j, CompatibleObjectType const &arr) +void to_json(Json &j, const CompatibleObjectType &arr) { external_constructor::construct(j, arr); } template -void from_json(Json const& j, typename Json::boolean_t& b) +void from_json(const Json & j, typename Json::boolean_t& b) { if (!j.is_boolean()) throw std::domain_error("type must be boolean, but is " + type_name(j)); @@ -588,7 +588,7 @@ void from_json(Json const& j, typename Json::boolean_t& b) } template -void from_json(Json const& j, typename Json::string_t& s) +void from_json(const Json & j, typename Json::string_t& s) { if (!j.is_string()) throw std::domain_error("type must be string, but is " + type_name(j)); @@ -596,26 +596,26 @@ void from_json(Json const& j, typename Json::string_t& s) } template -void from_json(Json const& j, typename Json::number_float_t& val) +void from_json(const Json & j, typename Json::number_float_t& val) { get_arithmetic_value(j, val); } template -void from_json(Json const& j, typename Json::number_unsigned_t& val) +void from_json(const Json & j, typename Json::number_unsigned_t& val) { get_arithmetic_value(j, val); } template -void from_json(Json const& j, typename Json::number_integer_t& val) +void from_json(const Json & j, typename Json::number_integer_t& val) { get_arithmetic_value(j, val); } template ::value, int> = 0> -void from_json(Json const &j, UnscopedEnumType& e) +void from_json(const Json &j, UnscopedEnumType& e) { typename std::underlying_type::type val = e; get_arithmetic_value(j, val); @@ -623,7 +623,7 @@ void from_json(Json const &j, UnscopedEnumType& e) } template -void from_json(Json const &j, typename Json::array_t &arr) +void from_json(const Json &j, typename Json::array_t &arr) { if (!j.is_array()) throw std::domain_error("type must be array, but is " + type_name(j)); @@ -632,7 +632,7 @@ void from_json(Json const &j, typename Json::array_t &arr) // forward_list doesn't have an insert method, TODO find a way to avoid including forward_list template -void from_json(Json const&j, std::forward_list& l) +void from_json(const Json &j, std::forward_list& l) { // do not perform the check when user wants to retrieve jsons // (except when it's null.. ?) @@ -648,13 +648,13 @@ void from_json(Json const&j, std::forward_list& l) } template -void from_json_array_impl(Json const &j, CompatibleArrayType &arr, priority_tag<0>) +void from_json_array_impl(const Json &j, CompatibleArrayType &arr, priority_tag<0>) { using std::begin; using std::end; std::transform( - j.begin(), j.end(), std::inserter(arr, end(arr)), [](Json const &i) + j.begin(), j.end(), std::inserter(arr, end(arr)), [](const Json &i) { // get() returns *this, this won't call a from_json method when // value_type is Json @@ -663,7 +663,7 @@ void from_json_array_impl(Json const &j, CompatibleArrayType &arr, priority_tag< } template -auto from_json_array_impl(Json const &j, CompatibleArrayType &arr, priority_tag<1>) +auto from_json_array_impl(const Json &j, CompatibleArrayType &arr, priority_tag<1>) -> decltype( arr.reserve(std::declval()), void()) @@ -673,7 +673,7 @@ auto from_json_array_impl(Json const &j, CompatibleArrayType &arr, priority_tag< arr.reserve(j.size()); std::transform( - j.begin(), j.end(), std::inserter(arr, end(arr)), [](Json const &i) + j.begin(), j.end(), std::inserter(arr, end(arr)), [](const Json &i) { // get() returns *this, this won't call a from_json method when // value_type is Json @@ -687,7 +687,7 @@ template < not std::is_same::value, int> = 0> -void from_json(Json const &j, CompatibleArrayType &arr) +void from_json(const Json &j, CompatibleArrayType &arr) { if (j.is_null()) throw std::domain_error("type must be array, but is " + type_name(j)); @@ -705,7 +705,7 @@ template < typename Json, typename CompatibleObjectType, enable_if_t::value, int> = 0> -void from_json(Json const &j, CompatibleObjectType &obj) +void from_json(const Json &j, CompatibleObjectType &obj) { if (!j.is_object()) throw std::domain_error("type must be object, but is " + type_name(j)); @@ -731,7 +731,7 @@ template < not std::is_same::value and not std::is_same::value, int> = 0> -void from_json(Json const &j, ArithmeticType &val) +void from_json(const Json &j, ArithmeticType &val) { if (j.is_number_unsigned()) val = static_cast(*j.template get_ptr()); @@ -775,7 +775,7 @@ struct from_json_fn { private: template - auto call(Json const &j, T &val, priority_tag<1>) const + auto call(const Json &j, T &val, priority_tag<1>) const noexcept(noexcept(from_json(j, val))) -> decltype(from_json(j, val), void()) { @@ -783,14 +783,14 @@ private: } template - void call(Json const&, T&, priority_tag<0>) const noexcept + void call(const Json &, T&, priority_tag<0>) const noexcept { static_assert(sizeof(Json) == 0, "from_json method in T's namespace can not be called"); } public: template - void operator()(Json const &j, T &val) const + void operator()(const Json &j, T &val) const noexcept(noexcept(std::declval().call(j, val, priority_tag<1>{}))) { return call(j, val, priority_tag<1>{}); @@ -831,8 +831,8 @@ constexpr T static_const::value; inline namespace { -constexpr auto const& to_json = static_const::value; -constexpr auto const& from_json = static_const::value; +constexpr const auto & to_json = static_const::value; +constexpr const auto & from_json = static_const::value; } // default JSONSerializer template argument, doesn't care about template argument @@ -948,7 +948,7 @@ class basic_json { private: template <::nlohmann::value_t> friend struct detail::external_constructor; - template friend std::string detail::type_name(Json const &); + template friend std::string detail::type_name(const Json &); /// workaround type for MSVC using basic_json_t = basic_json>::from_json( - std::declval(), std::declval &>()))) + std::declval(), std::declval &>()))) -> uncvref_t { using type = uncvref_t; @@ -3052,7 +3052,7 @@ class basic_json detail::has_non_default_from_json>::value, int> = 0> - uncvref_t get() const noexcept(noexcept(JSONSerializer::from_json(std::declval()))) + uncvref_t get() const noexcept(noexcept(JSONSerializer::from_json(std::declval()))) { return JSONSerializer::from_json(*this); } diff --git a/test/src/unit-udt.cpp b/test/src/unit-udt.cpp index 10e8b71a..0590a8e5 100644 --- a/test/src/unit-udt.cpp +++ b/test/src/unit-udt.cpp @@ -91,7 +91,7 @@ void to_json(Json& j, age a) } template -void to_json(Json& j, name const& n) +void to_json(Json& j, const name& n) { j = n.m_val; } @@ -114,22 +114,22 @@ void to_json(Json& j, country c) } template -void to_json(Json& j, person const& p) +void to_json(Json& j, const person & p) { j = Json{{"age", p.m_age}, {"name", p.m_name}, {"country", p.m_country}}; } -void to_json(nlohmann::json& j, address const& a) +void to_json(nlohmann::json& j, const address & a) { j = a.m_val; } -void to_json(nlohmann::json& j, contact const& c) +void to_json(nlohmann::json& j, const contact & c) { j = json{{"person", c.m_person}, {"address", c.m_address}}; } -void to_json(nlohmann::json& j, contact_book const& cb) +void to_json(nlohmann::json& j, const contact_book & cb) { j = json{{"name", cb.m_book_name}, {"contacts", cb.m_contacts}}; } @@ -140,28 +140,28 @@ bool operator==(age lhs, age rhs) return lhs.m_val == rhs.m_val; } -bool operator==(address const& lhs, address const& rhs) +bool operator==(const address & lhs, const address & rhs) { return lhs.m_val == rhs.m_val; } -bool operator==(name const& lhs, name const& rhs) +bool operator==(const name & lhs, const name & rhs) { return lhs.m_val == rhs.m_val; } -bool operator==(person const& lhs, person const& rhs) +bool operator==(const person & lhs, const person & rhs) { return std::tie(lhs.m_name, lhs.m_age) == std::tie(rhs.m_name, rhs.m_age); } -bool operator==(contact const& lhs, contact const& rhs) +bool operator==(const contact & lhs, const contact & rhs) { return std::tie(lhs.m_person, lhs.m_address) == std::tie(rhs.m_person, rhs.m_address); } -bool operator==(contact_book const& lhs, contact_book const& rhs) +bool operator==(const contact_book & lhs, const contact_book & rhs) { return std::tie(lhs.m_book_name, lhs.m_contacts) == std::tie(rhs.m_book_name, rhs.m_contacts); @@ -172,19 +172,19 @@ bool operator==(contact_book const& lhs, contact_book const& rhs) namespace udt { template -void from_json(Json const& j, age& a) +void from_json(const Json & j, age& a) { a.m_val = j.template get(); } template -void from_json(Json const& j, name& n) +void from_json(const Json & j, name& n) { n.m_val = j.template get(); } template -void from_json(Json const& j, country& c) +void from_json(const Json & j, country& c) { const auto str = j.template get(); static const std::map m = @@ -200,25 +200,25 @@ void from_json(Json const& j, country& c) } template -void from_json(Json const& j, person& p) +void from_json(const Json & j, person& p) { p.m_age = j["age"].template get(); p.m_name = j["name"].template get(); p.m_country = j["country"].template get(); } -void from_json(nlohmann::json const& j, address& a) +void from_json(const nlohmann::json & j, address& a) { a.m_val = j.get(); } -void from_json(nlohmann::json const& j, contact& c) +void from_json(const nlohmann::json & j, contact& c) { c.m_person = j["person"].get(); c.m_address = j["address"].get
(); } -void from_json(nlohmann::json const& j, contact_book& cb) +void from_json(const nlohmann::json & j, contact_book& cb) { cb.m_book_name = j["name"].get(); cb.m_contacts = j["contacts"].get>(); @@ -297,7 +297,7 @@ namespace nlohmann template struct adl_serializer> { - static void to_json(json& j, std::shared_ptr const& opt) + static void to_json(json& j, const std::shared_ptr & opt) { if (opt) { @@ -309,7 +309,7 @@ struct adl_serializer> } } - static void from_json(json const& j, std::shared_ptr& opt) + static void from_json(const json & j, std::shared_ptr& opt) { if (j.is_null()) { @@ -325,12 +325,12 @@ struct adl_serializer> template <> struct adl_serializer { - static void to_json(json& j, udt::legacy_type const& l) + static void to_json(json& j, const udt::legacy_type & l) { j = std::stoi(l.number); } - static void from_json(json const& j, udt::legacy_type& l) + static void from_json(const json & j, udt::legacy_type& l) { l.number = std::to_string(j.get()); } @@ -395,18 +395,18 @@ template <> struct adl_serializer> { using type = std::vector; - static void to_json(json& j, type const&) + static void to_json(json& j, const type &) { j = "hijacked!"; } - static void from_json(json const&, type& opt) + static void from_json(const json &, type& opt) { opt = {42.0, 42.0, 42.0}; } // preferred version - static type from_json(json const&) + static type from_json(const json &) { return {4.0, 5.0, 6.0}; } @@ -427,7 +427,7 @@ namespace nlohmann template struct adl_serializer> { - static void to_json(json& j, std::unique_ptr const& opt) + static void to_json(json& j, const std::unique_ptr & opt) { if (opt) { @@ -440,7 +440,7 @@ struct adl_serializer> } // this is the overload needed for non-copyable types, - static std::unique_ptr from_json(json const& j) + static std::unique_ptr from_json(const json & j) { if (j.is_null()) { @@ -496,7 +496,7 @@ struct pod_serializer typename Json, typename U = T, typename std::enable_if< not(std::is_pod::value and std::is_class::value), int>::type = 0> - static void from_json(Json const &j, U &t) + static void from_json(const Json &j, U &t) { using nlohmann::from_json; from_json(j, t); @@ -506,7 +506,7 @@ struct pod_serializer template ::value and std::is_class::value, int>::type = 0> - static void from_json(Json const &j, U &t) + static void from_json(const Json &j, U &t) { std::uint64_t value; // TODO The following block is no longer relevant in this serializer, make another one that shows the issue @@ -532,7 +532,7 @@ struct pod_serializer typename Json, typename U = T, typename std::enable_if< not(std::is_pod::value and std::is_class::value), int>::type = 0> - static void to_json(Json &j, T const &t) + static void to_json(Json &j, const T &t) { using nlohmann::to_json; to_json(j, t); @@ -541,9 +541,9 @@ struct pod_serializer template ::value and std::is_class::value, int>::type = 0> - static void to_json(Json &j, T const &t) noexcept + static void to_json(Json &j, const T &t) noexcept { - auto bytes = static_cast(static_cast(&t)); + auto bytes = static_cast< const unsigned char*>(static_cast(&t)); std::uint64_t value = bytes[0]; for (auto i = 1; i < 8; ++i) value |= std::uint64_t{bytes[i]} << 8 * i; @@ -566,13 +566,13 @@ struct non_pod }; template -void to_json(Json& j, non_pod const& np) +void to_json(Json& j, const non_pod & np) { j = np.s; } template -void from_json(Json const& j, non_pod& np) +void from_json(const Json & j, non_pod& np) { np.s = j.template get(); } @@ -583,7 +583,7 @@ bool operator==(small_pod lhs, small_pod rhs) noexcept std::tie(rhs.begin, rhs.middle, rhs.end); } -bool operator==(non_pod const &lhs, non_pod const &rhs) noexcept +bool operator==(const non_pod &lhs, const non_pod &rhs) noexcept { return lhs.s == rhs.s; } @@ -622,13 +622,13 @@ using custom_json = nlohmann::basic_json struct another_adl_serializer { - static void from_json(custom_json const& j , T& t) + static void from_json(const custom_json & j , T& t) { using nlohmann::from_json; from_json(j, t); } - static void to_json(custom_json& j , T const& t) + static void to_json(custom_json& j , const T & t) { using nlohmann::to_json; to_json(j, t);