mirror of
https://github.com/ToruNiina/toml11.git
synced 2024-11-09 22:30:07 +00:00
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:
parent
06f04af375
commit
acc7b7870f
31
toml/get.hpp
31
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<value_type>(x);});
|
||||
@ -204,9 +205,9 @@ T get(const value& v)
|
||||
const auto& ar = v.cast<value_t::Array>();
|
||||
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<first_type >(ar.at(0)),
|
||||
::toml::get<second_type>(ar.at(1)));
|
||||
@ -234,10 +235,10 @@ T get(const value& v)
|
||||
const auto& ar = v.cast<value_t::Array>();
|
||||
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 ",
|
||||
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,
|
||||
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);
|
||||
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<T>(tab.at(ky));
|
||||
}
|
||||
@ -325,9 +326,9 @@ find(toml::value& v, const toml::key& ky)
|
||||
auto& tab = ::toml::get<toml::table>(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<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));
|
||||
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<T>(std::move(tab[ky]));
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include "lexer.hpp"
|
||||
#include "types.hpp"
|
||||
#include "value.hpp"
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
namespace toml
|
||||
|
@ -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<typename T>
|
||||
@ -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<value_t T>
|
||||
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<T>::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<T>::invoke(*this);
|
||||
@ -679,8 +672,8 @@ typename detail::toml_default_type<T>::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<T>::invoke(*this);
|
||||
@ -690,8 +683,8 @@ typename detail::toml_default_type<T>::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<T>::invoke(std::move(*this));
|
||||
|
Loading…
Reference in New Issue
Block a user