From 19cc9a2edf11d81203c6cd6c1a085d373c58d975 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 25 Jul 2020 22:01:34 +0900 Subject: [PATCH] refactor: remove template from detail::region --- toml/combinator.hpp | 42 ++++++++++++++--------------- toml/parser.hpp | 64 ++++++++++++++++++++++----------------------- toml/region.hpp | 10 ++----- toml/value.hpp | 56 +++++++++++++++++---------------------- 4 files changed, 78 insertions(+), 94 deletions(-) diff --git a/toml/combinator.hpp b/toml/combinator.hpp index 4df55aa..e250188 100644 --- a/toml/combinator.hpp +++ b/toml/combinator.hpp @@ -58,7 +58,7 @@ struct character { static constexpr char target = C; - static result>, none_t> + static result invoke(location& loc) { if(loc.iter() == loc.end()) {return none();} @@ -71,7 +71,7 @@ struct character } loc.advance(); // update location - return ok(region>(loc, first, loc.iter())); + return ok(region(loc, first, loc.iter())); } }; template @@ -87,7 +87,7 @@ struct in_range static constexpr char upper = Up; static constexpr char lower = Low; - static result>, none_t> + static result invoke(location& loc) { if(loc.iter() == loc.end()) {return none();} @@ -100,7 +100,7 @@ struct in_range } loc.advance(); - return ok(region>(loc, first, loc.iter())); + return ok(region(loc, first, loc.iter())); } }; template constexpr char in_range::upper; @@ -111,7 +111,7 @@ template constexpr char in_range::lower; template struct exclude { - static result>, none_t> + static result invoke(location& loc) { if(loc.iter() == loc.end()) {return none();} @@ -124,7 +124,7 @@ struct exclude return none(); } loc.reset(std::next(first)); // XXX maybe loc.advance() is okay but... - return ok(region>(loc, first, loc.iter())); + return ok(region(loc, first, loc.iter())); } }; @@ -132,7 +132,7 @@ struct exclude template struct maybe { - static result>, none_t> + static result invoke(location& loc) { const auto rslt = Combinator::invoke(loc); @@ -140,7 +140,7 @@ struct maybe { return rslt; } - return ok(region>(loc)); + return ok(region(loc)); } }; @@ -150,7 +150,7 @@ struct sequence; template struct sequence { - static result>, none_t> + static result invoke(location& loc) { const auto first = loc.iter(); @@ -165,8 +165,8 @@ struct sequence // called from the above function only, recursively. template - static result>, none_t> - invoke(location& loc, region> reg, Iterator first) + static result + invoke(location& loc, region reg, Iterator first) { const auto rslt = Head::invoke(loc); if(rslt.is_err()) @@ -184,8 +184,8 @@ struct sequence { // would be called from sequence::invoke only. template - static result>, none_t> - invoke(location& loc, region> reg, Iterator first) + static result + invoke(location& loc, region reg, Iterator first) { const auto rslt = Head::invoke(loc); if(rslt.is_err()) @@ -204,7 +204,7 @@ struct either; template struct either { - static result>, none_t> + static result invoke(location& loc) { const auto rslt = Head::invoke(loc); @@ -215,7 +215,7 @@ struct either template struct either { - static result>, none_t> + static result invoke(location& loc) { return Head::invoke(loc); @@ -232,10 +232,10 @@ struct unlimited{}; template struct repeat> { - static result>, none_t> + static result invoke(location& loc) { - region> retval(loc); + region retval(loc); const auto first = loc.iter(); for(std::size_t i=0; i> template struct repeat> { - static result>, none_t> + static result invoke(location& loc) { - region> retval(loc); + region retval(loc); const auto first = loc.iter(); for(std::size_t i=0; i> template struct repeat { - static result>, none_t> + static result invoke(location& loc) { - region> retval(loc); + region retval(loc); while(true) { auto rslt = T::invoke(loc); diff --git a/toml/parser.hpp b/toml/parser.hpp index 6ab5252..084bbd7 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -25,7 +25,7 @@ namespace toml namespace detail { -inline result>>, std::string> +inline result, std::string> parse_boolean(location& loc) { const auto first = loc.iter(); @@ -47,7 +47,7 @@ parse_boolean(location& loc) {{std::addressof(loc), "the next token is not a boolean"}})); } -inline result>>, std::string> +inline result, std::string> parse_binary_integer(location& loc) { const auto first = loc.iter(); @@ -76,7 +76,7 @@ parse_binary_integer(location& loc) {{std::addressof(loc), "the next token is not an integer"}})); } -inline result>>, std::string> +inline result, std::string> parse_octal_integer(location& loc) { const auto first = loc.iter(); @@ -96,7 +96,7 @@ parse_octal_integer(location& loc) {{std::addressof(loc), "the next token is not an integer"}})); } -inline result>>, std::string> +inline result, std::string> parse_hexadecimal_integer(location& loc) { const auto first = loc.iter(); @@ -116,7 +116,7 @@ parse_hexadecimal_integer(location& loc) {{std::addressof(loc), "the next token is not an integer"}})); } -inline result>>, std::string> +inline result, std::string> parse_integer(location& loc) { const auto first = loc.iter(); @@ -125,7 +125,7 @@ parse_integer(location& loc) const auto second = std::next(first); if(second == loc.end()) // the token is just zero. { - return ok(std::make_pair(0, region>(loc, first, second))); + return ok(std::make_pair(0, region(loc, first, second))); } if(*second == 'b') {return parse_binary_integer (loc);} // 0b1100 @@ -161,7 +161,7 @@ parse_integer(location& loc) {{std::addressof(loc), "the next token is not an integer"}})); } -inline result>>, std::string> +inline result, std::string> parse_floating(location& loc) { const auto first = loc.iter(); @@ -249,9 +249,7 @@ parse_floating(location& loc) {{std::addressof(loc), "the next token is not a float"}})); } -template -std::string read_utf8_codepoint(const region& reg, - /* for err msg */ const location& loc) +inline std::string read_utf8_codepoint(const region& reg, const location& loc) { const auto str = reg.str().substr(1); std::uint_least32_t codepoint; @@ -363,7 +361,7 @@ inline result parse_escape_sequence(location& loc) return err(msg); } -inline result>>, std::string> +inline result, std::string> parse_ml_basic_string(location& loc) { const auto first = loc.iter(); @@ -442,7 +440,7 @@ parse_ml_basic_string(location& loc) } } -inline result>>, std::string> +inline result, std::string> parse_basic_string(location& loc) { const auto first = loc.iter(); @@ -494,7 +492,7 @@ parse_basic_string(location& loc) } } -inline result>>, std::string> +inline result, std::string> parse_ml_literal_string(location& loc) { const auto first = loc.iter(); @@ -556,7 +554,7 @@ parse_ml_literal_string(location& loc) } } -inline result>>, std::string> +inline result, std::string> parse_literal_string(location& loc) { const auto first = loc.iter(); @@ -596,7 +594,7 @@ parse_literal_string(location& loc) } } -inline result>>, std::string> +inline result, std::string> parse_string(location& loc) { if(loc.iter() != loc.end() && *(loc.iter()) == '"') @@ -627,7 +625,7 @@ parse_string(location& loc) {{std::addressof(loc), "the next token is not a string"}})); } -inline result>>, std::string> +inline result, std::string> parse_local_date(location& loc) { const auto first = loc.iter(); @@ -676,7 +674,7 @@ parse_local_date(location& loc) } } -inline result>>, std::string> +inline result, std::string> parse_local_time(location& loc) { const auto first = loc.iter(); @@ -764,7 +762,7 @@ parse_local_time(location& loc) } } -inline result>>, std::string> +inline result, std::string> parse_local_datetime(location& loc) { const auto first = loc.iter(); @@ -808,7 +806,7 @@ parse_local_datetime(location& loc) } } -inline result>>, std::string> +inline result, std::string> parse_offset_datetime(location& loc) { const auto first = loc.iter(); @@ -856,7 +854,7 @@ parse_offset_datetime(location& loc) } } -inline result>>, std::string> +inline result, std::string> parse_simple_key(location& loc) { if(const auto bstr = parse_basic_string(loc)) @@ -877,7 +875,7 @@ parse_simple_key(location& loc) } // dotted key become vector of keys -inline result, region>>, std::string> +inline result, region>, std::string> parse_key(location& loc) { const auto first = loc.iter(); @@ -939,7 +937,7 @@ template result parse_value(location&); template -result>>, std::string> +result, std::string> parse_array(location& loc) { using value_type = Value; @@ -968,7 +966,7 @@ parse_array(location& loc) { loc.advance(); // skip ']' return ok(std::make_pair(retval, - region>(loc, first, loc.iter()))); + region(loc, first, loc.iter()))); } if(auto val = parse_value(loc)) @@ -1021,7 +1019,7 @@ parse_array(location& loc) { loc.advance(); // skip ']' return ok(std::make_pair(retval, - region>(loc, first, loc.iter()))); + region(loc, first, loc.iter()))); } else { @@ -1044,7 +1042,7 @@ parse_array(location& loc) } template -result, region>>, Value>, std::string> +result, region>, Value>, std::string> parse_key_value_pair(location& loc) { using value_type = Value; @@ -1133,7 +1131,7 @@ std::string format_dotted_keys(InputIterator first, const InputIterator last) } // forward decl for is_valid_forward_table_definition -result, region>>, std::string> +result, region>, std::string> parse_table_key(location& loc); // The following toml file is allowed. @@ -1205,7 +1203,7 @@ template result insert_nested_key(typename Value::table_type& root, const Value& v, InputIterator iter, const InputIterator last, - region> key_reg, + region key_reg, const bool is_array_of_table = false) { static_assert(std::is_same -result>>, std::string> +result, std::string> parse_inline_table(location& loc) { using value_type = Value; @@ -1461,7 +1459,7 @@ parse_inline_table(location& loc) { loc.advance(); // skip `}` return ok(std::make_pair(retval, - region>(loc, first, loc.iter()))); + region(loc, first, loc.iter()))); } const auto kv_r = parse_key_value_pair(loc); @@ -1492,7 +1490,7 @@ parse_inline_table(location& loc) { loc.advance(); // skip `}` return ok(std::make_pair( - retval, region>(loc, first, loc.iter()))); + retval, region(loc, first, loc.iter()))); } else if(*loc.iter() == '#' || *loc.iter() == '\r' || *loc.iter() == '\n') { @@ -1709,7 +1707,7 @@ result parse_value(location& loc) } } -inline result, region>>, std::string> +inline result, region>, std::string> parse_table_key(location& loc) { if(auto token = lex_std_table::invoke(loc)) @@ -1770,7 +1768,7 @@ parse_table_key(location& loc) } } -inline result, region>>, std::string> +inline result, region>, std::string> parse_array_table_key(location& loc) { if(auto token = lex_array_table::invoke(loc)) @@ -1927,7 +1925,7 @@ result parse_toml_file(location& loc) // put the first line as a region of a file // Here first != loc.end(), so taking std::next is okay - const region> file(loc, first, std::next(loc.iter())); + const region file(loc, first, std::next(loc.iter())); // The first successive comments that are separated from the first value // by an empty line are for a file itself. diff --git a/toml/region.hpp b/toml/region.hpp index 55a748d..fde6d50 100644 --- a/toml/region.hpp +++ b/toml/region.hpp @@ -194,16 +194,10 @@ struct location final : public region_base // // it contains pointer to the file content and iterator that points the first // and last location. -template struct region final : public region_base { - using const_iterator = typename Container::const_iterator; - using source_ptr = std::shared_ptr; - - static_assert(std::is_same::value,""); - static_assert(std::is_same::iterator_category>::value, - "container should be randomly accessible"); + using const_iterator = typename std::vector::const_iterator; + using source_ptr = std::shared_ptr>; // delete default constructor. source_ never be null. region() = delete; diff --git a/toml/value.hpp b/toml/value.hpp index d2f8cb9..d0af508 100644 --- a/toml/value.hpp +++ b/toml/value.hpp @@ -1056,95 +1056,87 @@ class basic_value // // Those constructors take detail::region that contains parse result. - template - basic_value(boolean b, detail::region reg) + basic_value(boolean b, detail::region reg) : type_(value_t::boolean), - region_info_(std::make_shared>(std::move(reg))), + region_info_(std::make_shared(std::move(reg))), comments_(region_info_->comments()) { assigner(this->boolean_, b); } - template, detail::negation> >::value, std::nullptr_t>::type = nullptr> - basic_value(T i, detail::region reg) + basic_value(T i, detail::region reg) : type_(value_t::integer), - region_info_(std::make_shared>(std::move(reg))), + region_info_(std::make_shared(std::move(reg))), comments_(region_info_->comments()) { assigner(this->integer_, static_cast(i)); } - template::value, std::nullptr_t>::type = nullptr> - basic_value(T f, detail::region reg) + basic_value(T f, detail::region reg) : type_(value_t::floating), - region_info_(std::make_shared>(std::move(reg))), + region_info_(std::make_shared(std::move(reg))), comments_(region_info_->comments()) { assigner(this->floating_, static_cast(f)); } - template - basic_value(toml::string s, detail::region reg) + basic_value(toml::string s, detail::region reg) : type_(value_t::string), - region_info_(std::make_shared>(std::move(reg))), + region_info_(std::make_shared(std::move(reg))), comments_(region_info_->comments()) { assigner(this->string_, std::move(s)); } - template - basic_value(const local_date& ld, detail::region reg) + basic_value(const local_date& ld, detail::region reg) : type_(value_t::local_date), - region_info_(std::make_shared>(std::move(reg))), + region_info_(std::make_shared(std::move(reg))), comments_(region_info_->comments()) { assigner(this->local_date_, ld); } - template - basic_value(const local_time& lt, detail::region reg) + basic_value(const local_time& lt, detail::region reg) : type_(value_t::local_time), - region_info_(std::make_shared>(std::move(reg))), + region_info_(std::make_shared(std::move(reg))), comments_(region_info_->comments()) { assigner(this->local_time_, lt); } - template - basic_value(const local_datetime& ldt, detail::region reg) + basic_value(const local_datetime& ldt, detail::region reg) : type_(value_t::local_datetime), - region_info_(std::make_shared>(std::move(reg))), + region_info_(std::make_shared(std::move(reg))), comments_(region_info_->comments()) { assigner(this->local_datetime_, ldt); } - template - basic_value(const offset_datetime& odt, detail::region reg) + basic_value(const offset_datetime& odt, detail::region reg) : type_(value_t::offset_datetime), - region_info_(std::make_shared>(std::move(reg))), + region_info_(std::make_shared(std::move(reg))), comments_(region_info_->comments()) { assigner(this->offset_datetime_, odt); } - template - basic_value(const array_type& ary, detail::region reg) + basic_value(const array_type& ary, detail::region reg) : type_(value_t::array), - region_info_(std::make_shared>(std::move(reg))), + region_info_(std::make_shared(std::move(reg))), comments_(region_info_->comments()) { assigner(this->array_, ary); } - template - basic_value(const table_type& tab, detail::region reg) + basic_value(const table_type& tab, detail::region reg) : type_(value_t::table), - region_info_(std::make_shared>(std::move(reg))), + region_info_(std::make_shared(std::move(reg))), comments_(region_info_->comments()) { assigner(this->table_, tab); } - template::value, std::nullptr_t>::type = nullptr> - basic_value(std::pair> parse_result) + basic_value(std::pair parse_result) : basic_value(std::move(parse_result.first), std::move(parse_result.second)) {}