remove format_error_for_value and add get_region instead

it is convenient to have get_region function that can access region_info
in toml::value. get_region is placed in toml::detail and made friend of
toml::value because I don't want to make toml::value::region_info public
and keep it internal use only.
This commit is contained in:
ToruNiina 2018-12-13 16:13:05 +09:00
parent 06f04af375
commit acc7b7870f
3 changed files with 29 additions and 34 deletions

View File

@ -182,9 +182,10 @@ T get(const value& v)
T container; T container;
if(ar.size() != container.size()) 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(), "[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(), std::transform(ar.cbegin(), ar.cend(), container.begin(),
[](const value& x){return ::toml::get<value_type>(x);}); [](const value& x){return ::toml::get<value_type>(x);});
@ -204,9 +205,9 @@ T get(const value& v)
const auto& ar = v.cast<value_t::Array>(); const auto& ar = v.cast<value_t::Array>();
if(ar.size() != 2) 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(), "[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<first_type >(ar.at(0)), return std::make_pair(::toml::get<first_type >(ar.at(0)),
::toml::get<second_type>(ar.at(1))); ::toml::get<second_type>(ar.at(1)));
@ -234,10 +235,10 @@ T get(const value& v)
const auto& ar = v.cast<value_t::Array>(); const auto& ar = v.cast<value_t::Array>();
if(ar.size() != std::tuple_size<T>::value) if(ar.size() != std::tuple_size<T>::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 ", "[erorr] toml::get specified std::tuple with ",
std::tuple_size<T>::value, "elements, but there are ", ar.size(), std::tuple_size<T>::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<T>(ar, return detail::get_tuple_impl<T>(ar,
detail::make_index_sequence<std::tuple_size<T>::value>{}); detail::make_index_sequence<std::tuple_size<T>::value>{});
@ -312,9 +313,9 @@ find(const toml::value& v, const toml::key& ky)
const auto& tab = ::toml::get<toml::table>(v); const auto& tab = ::toml::get<toml::table>(v);
if(tab.count(ky) == 0) if(tab.count(ky) == 0)
{ {
throw std::out_of_range(detail::format_error_for_value(v, throw std::out_of_range(detail::format_underline(concat_to_string(
concat_to_string("[error] key \"", ky, "\" not found"), "[error] key \"", ky, "\" not found"), detail::get_region(v),
concat_to_string("in this table"))); "in this table"));
} }
return ::toml::get<T>(tab.at(ky)); return ::toml::get<T>(tab.at(ky));
} }
@ -325,9 +326,9 @@ find(toml::value& v, const toml::key& ky)
auto& tab = ::toml::get<toml::table>(v); auto& tab = ::toml::get<toml::table>(v);
if(tab.count(ky) == 0) if(tab.count(ky) == 0)
{ {
throw std::out_of_range(detail::format_error_for_value(v, throw std::out_of_range(detail::format_underline(concat_to_string(
concat_to_string("[error] key \"", ky, "\" not found"), "[error] key \"", ky, "\" not found"), detail::get_region(v),
concat_to_string("in this table"))); "in this table"));
} }
return ::toml::get<T>(tab.at(ky)); return ::toml::get<T>(tab.at(ky));
} }
@ -338,9 +339,9 @@ find(toml::value&& v, const toml::key& ky)
auto tab = ::toml::get<toml::table>(std::move(v)); auto tab = ::toml::get<toml::table>(std::move(v));
if(tab.count(ky) == 0) if(tab.count(ky) == 0)
{ {
throw std::out_of_range(detail::format_error_for_value(v, throw std::out_of_range(detail::format_underline(concat_to_string(
concat_to_string("[error] key \"", ky, "\" not found"), "[error] key \"", ky, "\" not found"), detail::get_region(v),
concat_to_string("in this table"))); "in this table"));
} }
return ::toml::get<T>(std::move(tab[ky])); return ::toml::get<T>(std::move(tab[ky]));
} }

View File

@ -6,6 +6,7 @@
#include "lexer.hpp" #include "lexer.hpp"
#include "types.hpp" #include "types.hpp"
#include "value.hpp" #include "value.hpp"
#include <iostream>
#include <cstring> #include <cstring>
namespace toml namespace toml

View File

@ -17,8 +17,8 @@ namespace toml
namespace detail namespace detail
{ {
std::string // forward decl // to show error messages. not recommended for users.
format_error_for_value(const value&, const std::string&, const std::string&); region_base const& get_region(const value&);
}// detail }// detail
template<typename T> template<typename T>
@ -555,14 +555,8 @@ class value
} }
} }
std::string // for error messages
format_error(const std::string& msg, const std::string& com) const friend region_base const& detail::get_region(const value&);
{
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&);
template<value_t T> template<value_t T>
struct switch_cast; struct switch_cast;
@ -594,10 +588,9 @@ class value
namespace detail namespace detail
{ {
inline std::string format_error_for_value( inline region_base const& get_region(const value& v)
const value& v, const std::string& m, const std::string& c)
{ {
return v.format_error(m, c); return *(v.region_info_);
} }
}// detail }// detail
@ -668,8 +661,8 @@ typename detail::toml_default_type<T>::type& value::cast() &
{ {
if(T != this->type_) if(T != this->type_)
{ {
throw type_error(this->format_error(concat_to_string( throw type_error(format_underline(concat_to_string(
"[error] toml::value bad_cast to ", T), "[error] toml::value bad_cast to ", T), *region_info_,
concat_to_string("the actual type is ", this->type_))); concat_to_string("the actual type is ", this->type_)));
} }
return switch_cast<T>::invoke(*this); return switch_cast<T>::invoke(*this);
@ -679,8 +672,8 @@ typename detail::toml_default_type<T>::type const& value::cast() const&
{ {
if(T != this->type_) if(T != this->type_)
{ {
throw type_error(this->format_error(concat_to_string( throw type_error(format_underline(concat_to_string(
"[error] toml::value bad_cast to ", T), "[error] toml::value bad_cast to ", T), *region_info_,
concat_to_string("the actual type is ", this->type_))); concat_to_string("the actual type is ", this->type_)));
} }
return switch_cast<T>::invoke(*this); return switch_cast<T>::invoke(*this);
@ -690,8 +683,8 @@ typename detail::toml_default_type<T>::type&& value::cast() &&
{ {
if(T != this->type_) if(T != this->type_)
{ {
throw type_error(this->format_error(concat_to_string( throw type_error(format_underline(concat_to_string(
"[error] toml::value bad_cast to ", T), "[error] toml::value bad_cast to ", T), *region_info_,
concat_to_string("the actual type is ", this->type_))); concat_to_string("the actual type is ", this->type_)));
} }
return switch_cast<T>::invoke(std::move(*this)); return switch_cast<T>::invoke(std::move(*this));