diff --git a/toml/get.hpp b/toml/get.hpp index 20944f4..1cf15ba 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -182,9 +182,10 @@ T get(const value& v) T container; if(ar.size() != container.size()) { - throw std::out_of_range(detail::format_error_for_value(v, concat_to_string( + throw std::out_of_range(detail::format_underline(concat_to_string( "[erorr] toml::get specified container size is ", container.size(), - " but there are ", ar.size(), " elements in toml array."), "here")); + " but there are ", ar.size(), " elements in toml array."), + detail::get_region(v), "here")); } std::transform(ar.cbegin(), ar.cend(), container.begin(), [](const value& x){return ::toml::get(x);}); @@ -204,9 +205,9 @@ T get(const value& v) const auto& ar = v.cast(); if(ar.size() != 2) { - throw std::out_of_range(detail::format_error_for_value(v, concat_to_string( + throw std::out_of_range(detail::format_underline(concat_to_string( "[erorr] toml::get specified std::pair but there are ", ar.size(), - " elements in toml array."), "here")); + " elements in toml array."), detail::get_region(v), "here")); } return std::make_pair(::toml::get(ar.at(0)), ::toml::get(ar.at(1))); @@ -234,10 +235,10 @@ T get(const value& v) const auto& ar = v.cast(); if(ar.size() != std::tuple_size::value) { - throw std::out_of_range(detail::format_error_for_value(v, concat_to_string( + throw std::out_of_range(detail::format_underline(concat_to_string( "[erorr] toml::get specified std::tuple with ", std::tuple_size::value, "elements, but there are ", ar.size(), - " elements in toml array."), "here")); + " elements in toml array."), detail::get_region(v), "here")); } return detail::get_tuple_impl(ar, detail::make_index_sequence::value>{}); @@ -312,9 +313,9 @@ find(const toml::value& v, const toml::key& ky) const auto& tab = ::toml::get(v); if(tab.count(ky) == 0) { - throw std::out_of_range(detail::format_error_for_value(v, - concat_to_string("[error] key \"", ky, "\" not found"), - concat_to_string("in this table"))); + throw std::out_of_range(detail::format_underline(concat_to_string( + "[error] key \"", ky, "\" not found"), detail::get_region(v), + "in this table")); } return ::toml::get(tab.at(ky)); } @@ -325,9 +326,9 @@ find(toml::value& v, const toml::key& ky) auto& tab = ::toml::get(v); if(tab.count(ky) == 0) { - throw std::out_of_range(detail::format_error_for_value(v, - concat_to_string("[error] key \"", ky, "\" not found"), - concat_to_string("in this table"))); + throw std::out_of_range(detail::format_underline(concat_to_string( + "[error] key \"", ky, "\" not found"), detail::get_region(v), + "in this table")); } return ::toml::get(tab.at(ky)); } @@ -338,9 +339,9 @@ find(toml::value&& v, const toml::key& ky) auto tab = ::toml::get(std::move(v)); if(tab.count(ky) == 0) { - throw std::out_of_range(detail::format_error_for_value(v, - concat_to_string("[error] key \"", ky, "\" not found"), - concat_to_string("in this table"))); + throw std::out_of_range(detail::format_underline(concat_to_string( + "[error] key \"", ky, "\" not found"), detail::get_region(v), + "in this table")); } return ::toml::get(std::move(tab[ky])); } diff --git a/toml/parser.hpp b/toml/parser.hpp index a965e1e..d6859b4 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -6,6 +6,7 @@ #include "lexer.hpp" #include "types.hpp" #include "value.hpp" +#include #include namespace toml diff --git a/toml/value.hpp b/toml/value.hpp index 8b98ca1..ca5b653 100644 --- a/toml/value.hpp +++ b/toml/value.hpp @@ -17,8 +17,8 @@ namespace toml namespace detail { -std::string // forward decl -format_error_for_value(const value&, const std::string&, const std::string&); +// to show error messages. not recommended for users. +region_base const& get_region(const value&); }// detail template @@ -555,14 +555,8 @@ class value } } - std::string - format_error(const std::string& msg, const std::string& com) const - { - return detail::format_underline(msg, *(this->region_info_), com); - } - - friend std::string detail::format_error_for_value( - const value&, const std::string&, const std::string&); + // for error messages + friend region_base const& detail::get_region(const value&); template struct switch_cast; @@ -594,10 +588,9 @@ class value namespace detail { -inline std::string format_error_for_value( - const value& v, const std::string& m, const std::string& c) +inline region_base const& get_region(const value& v) { - return v.format_error(m, c); + return *(v.region_info_); } }// detail @@ -668,8 +661,8 @@ typename detail::toml_default_type::type& value::cast() & { if(T != this->type_) { - throw type_error(this->format_error(concat_to_string( - "[error] toml::value bad_cast to ", T), + throw type_error(format_underline(concat_to_string( + "[error] toml::value bad_cast to ", T), *region_info_, concat_to_string("the actual type is ", this->type_))); } return switch_cast::invoke(*this); @@ -679,8 +672,8 @@ typename detail::toml_default_type::type const& value::cast() const& { if(T != this->type_) { - throw type_error(this->format_error(concat_to_string( - "[error] toml::value bad_cast to ", T), + throw type_error(format_underline(concat_to_string( + "[error] toml::value bad_cast to ", T), *region_info_, concat_to_string("the actual type is ", this->type_))); } return switch_cast::invoke(*this); @@ -690,8 +683,8 @@ typename detail::toml_default_type::type&& value::cast() && { if(T != this->type_) { - throw type_error(this->format_error(concat_to_string( - "[error] toml::value bad_cast to ", T), + throw type_error(format_underline(concat_to_string( + "[error] toml::value bad_cast to ", T), *region_info_, concat_to_string("the actual type is ", this->type_))); } return switch_cast::invoke(std::move(*this));