From e609922bf0d7618a75863061bd42d6f054145c86 Mon Sep 17 00:00:00 2001 From: mmd-osm Date: Sat, 8 Jun 2024 21:08:00 +0200 Subject: [PATCH] sjparser: Remove NthTypes, simplify code --- .../sjparser/internals/key_value_parser.h | 69 +++++++++---------- contrib/sjparser/library/sjparser/object.h | 4 +- .../sjparser/library/sjparser/s_auto_object.h | 2 +- .../library/sjparser/s_custom_object.h | 4 +- contrib/sjparser/library/sjparser/s_union.h | 2 +- contrib/sjparser/library/sjparser/union.h | 4 +- 6 files changed, 39 insertions(+), 46 deletions(-) diff --git a/contrib/sjparser/library/sjparser/internals/key_value_parser.h b/contrib/sjparser/library/sjparser/internals/key_value_parser.h index 200666a3..21a4a3f6 100644 --- a/contrib/sjparser/library/sjparser/internals/key_value_parser.h +++ b/contrib/sjparser/library/sjparser/internals/key_value_parser.h @@ -63,41 +63,34 @@ class KeyValueParser : public TokenParser { void onMember(InternalNameType member); - template - struct NthTypes { - using Tuple = std::tuple; - static constexpr auto Size = sizeof...(ParserTDs); + using ParserTupleType = std::tuple; - template - using Nth = typename std::tuple_element::type; + template + using Nth = typename std::tuple_element::type; - template - using ParserType = std::decay_t>; + template + using ParserType = std::decay_t>; - template > - using ValueType = typename U::ValueType; + template > + using ValueType = typename U::ValueType; - template - static constexpr bool has_value_type = IsStorageParser>; - }; + template + static constexpr bool has_value_type = IsStorageParser>; - template - using ParserType = typename NthTypes::template ParserType; + using ParsersArrayType = std::array; + using ParsersMapType = std::unordered_map; - // Returns NthTypes::template ValueType if it is - // available, otherwise NthTypes::template ParserType - template [[nodiscard]] auto &get(); + // Returns ValueType if it is available, otherwise ParserType + template + [[nodiscard]] auto &get(); template - [[nodiscard]] typename NthTypes::template ParserType &parser(); + [[nodiscard]] ParserType &parser(); template - typename NthTypes::template ValueType pop(); + ValueType pop(); protected: - using parsers_array_t = std::array; - using parsers_map_t = std::unordered_map; - template struct MemberParser { MemberParser() = delete; @@ -125,8 +118,8 @@ class KeyValueParser : public TokenParser { MemberParsers(MemberParsers&& other) noexcept = default; MemberParsers& operator=(MemberParsers&& other) noexcept = default; - MemberParsers(parsers_array_t& parsers_array, - parsers_map_t& parsers_map, + MemberParsers(ParsersArrayType& parsers_array, + ParsersMapType& parsers_map, std::tuple...>& members) : mbr_parsers(to_member_parser_tuple(members)) { registerParsers(parsers_array, parsers_map); @@ -137,13 +130,13 @@ class KeyValueParser : public TokenParser { return std::get(mbr_parsers); } - void registerParsers(parsers_array_t& parsers_array, parsers_map_t& parsers_map); + void registerParsers(ParsersArrayType& parsers_array, ParsersMapType& parsers_map); private: template void registerParser(ParserT& parser, NameT& name, int i, - parsers_array_t& parsers_array, - parsers_map_t& parsers_map); + ParsersArrayType& parsers_array, + ParsersMapType& parsers_map); void check_duplicate(bool inserted, NameT& name); @@ -164,8 +157,8 @@ class KeyValueParser : public TokenParser { [[nodiscard]] auto &memberParsers(); private: - parsers_array_t _parsers_array; - parsers_map_t _parsers_map; + ParsersArrayType _parsers_array; + ParsersMapType _parsers_map; MemberParsers _member_parsers; Ignore _ignore_parser; ObjectOptions _options; @@ -258,7 +251,7 @@ template auto &KeyValueParser::get() { auto &member = _member_parsers.template get(); - if constexpr (NthTypes::template has_value_type) { + if constexpr (has_value_type) { if (!member.parser.isSet() && member.default_value.value) { return static_cast( @@ -273,14 +266,14 @@ auto &KeyValueParser::get() { template template -typename KeyValueParser::template NthTypes::template ParserType & +typename KeyValueParser::template ParserType & KeyValueParser::parser() { return _member_parsers.template get().parser; } template template -typename KeyValueParser::template NthTypes::template ValueType +typename KeyValueParser::template ValueType KeyValueParser::pop() { auto &member = _member_parsers.template get(); @@ -312,8 +305,8 @@ auto &KeyValueParser::memberParsers() { template void KeyValueParser::MemberParsers::registerParsers( - parsers_array_t &parsers_array, - parsers_map_t &parsers_map) { + ParsersArrayType &parsers_array, + ParsersMapType &parsers_map) { parsers_map.clear(); @@ -330,12 +323,12 @@ template template void KeyValueParser< NameT, ParserTs... >::MemberParsers::registerParser( ParserT &parser, NameT &name, int i, - parsers_array_t &parsers_array, - parsers_map_t &parsers_map) { + ParsersArrayType &parsers_array, + ParsersMapType &parsers_map) { parsers_array[i] = &parser; - auto [_, inserted] = parsers_map.insert( { name, &parser }); + auto [_, inserted] = parsers_map.insert({ name, &parser }); std::ignore = _; check_duplicate(inserted, name); diff --git a/contrib/sjparser/library/sjparser/object.h b/contrib/sjparser/library/sjparser/object.h index 1b5a0dcf..3a3e932f 100644 --- a/contrib/sjparser/library/sjparser/object.h +++ b/contrib/sjparser/library/sjparser/object.h @@ -157,7 +157,7 @@ class Object : public KeyValueParser { * @return Reference to n-th member parser. */ [[nodiscard]] template - typename NthTypes::template ParserType &parser(); + ParserType &parser(); /** @brief Get the member parsed value and unset the member parser. * @@ -172,7 +172,7 @@ class Object : public KeyValueParser { * value was parsed or #pop was called for the member parser). */ template - typename NthTypes::template ValueType &&pop(); + ValueType &&pop(); #endif protected: diff --git a/contrib/sjparser/library/sjparser/s_auto_object.h b/contrib/sjparser/library/sjparser/s_auto_object.h index e093ac86..94f6decc 100644 --- a/contrib/sjparser/library/sjparser/s_auto_object.h +++ b/contrib/sjparser/library/sjparser/s_auto_object.h @@ -164,7 +164,7 @@ template class SAutoObject : public Object { * @return Reference to n-th member parser. */ [[nodiscard]] template - typename NthTypes::template ParserType &parser(); + ParserType &parser(); #endif private: diff --git a/contrib/sjparser/library/sjparser/s_custom_object.h b/contrib/sjparser/library/sjparser/s_custom_object.h index a272ced7..a0e4f9d5 100644 --- a/contrib/sjparser/library/sjparser/s_custom_object.h +++ b/contrib/sjparser/library/sjparser/s_custom_object.h @@ -176,7 +176,7 @@ class SCustomObject : public Object { * @return Reference to n-th member parser. */ [[nodiscard]] template - typename NthTypes::template ParserType &parser(); + ParserType &parser(); #endif using Object::get; @@ -202,7 +202,7 @@ class SCustomObject : public Object { * value was parsed or #pop was called for the member parser). */ template - typename NthTypes::template ValueType &&pop(); + ValueType &&pop(); #endif using Object::pop; diff --git a/contrib/sjparser/library/sjparser/s_union.h b/contrib/sjparser/library/sjparser/s_union.h index f0b98895..4470633d 100644 --- a/contrib/sjparser/library/sjparser/s_union.h +++ b/contrib/sjparser/library/sjparser/s_union.h @@ -174,7 +174,7 @@ class SUnion : public Union { * @return Reference to n-th member parser. */ [[nodiscard]] template - typename NthTypes::template ParserType &parser(); + ParserType &parser(); #endif /** @brief Get the parsed value and unset the parser. diff --git a/contrib/sjparser/library/sjparser/union.h b/contrib/sjparser/library/sjparser/union.h index 7ce96388..c0571956 100644 --- a/contrib/sjparser/library/sjparser/union.h +++ b/contrib/sjparser/library/sjparser/union.h @@ -190,7 +190,7 @@ class Union : public KeyValueParser { * @return Reference to n-th member parser. */ [[nodiscard]] template - typename NthTypes::template ParserType &parser(); + ParserType &parser(); /** @brief Get the member parsed value and unset the member parser. * @@ -204,7 +204,7 @@ class Union : public KeyValueParser { * value was parsed or #pop was called for the member parser). */ template - typename NthTypes::template ValueType &&pop(); + ValueType &&pop(); #endif protected: