From d1af42f151820aeb48dee02a78f3f1eb0167f2e0 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 29 Feb 2020 22:23:15 +0900 Subject: [PATCH] refactor: add throw_key_not_found_error and replace related throw statements with it --- toml/get.hpp | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/toml/get.hpp b/toml/get.hpp index 4a2f045..1d33650 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -9,6 +9,20 @@ namespace toml { +namespace detail +{ +// Throw from `toml::find`, generating an error message +template class M, template class V> +[[noreturn]] void +throw_key_not_found_error(const basic_value& v, const key& ky) +{ + throw std::out_of_range(format_underline(concat_to_string( + "key \"", ky, "\" not found"), { + {std::addressof(get_region(v)), "in this table"} + })); +} +} // detail // ============================================================================ // exact toml::* type @@ -449,10 +463,7 @@ basic_value const& find(const basic_value& v, const key& ky) const auto& tab = v.as_table(); if(tab.count(ky) == 0) { - throw std::out_of_range(detail::format_underline(concat_to_string( - "key \"", ky, "\" not found"), { - {std::addressof(detail::get_region(v)), "in this table"} - })); + detail::throw_key_not_found_error(v, ky); } return tab.at(ky); } @@ -463,10 +474,7 @@ basic_value& find(basic_value& v, const key& ky) auto& tab = v.as_table(); if(tab.count(ky) == 0) { - throw std::out_of_range(detail::format_underline(concat_to_string( - "key \"", ky, "\" not found"), { - {std::addressof(detail::get_region(v)), "in this table"} - })); + detail::throw_key_not_found_error(v, ky); } return tab.at(ky); } @@ -477,10 +485,7 @@ basic_value find(basic_value&& v, const key& ky) typename basic_value::table_type tab = std::move(v).as_table(); if(tab.count(ky) == 0) { - throw std::out_of_range(detail::format_underline(concat_to_string( - "key \"", ky, "\" not found"), { - {std::addressof(detail::get_region(v)), "in this table"} - })); + detail::throw_key_not_found_error(v, ky); } return basic_value(std::move(tab.at(ky))); } @@ -542,10 +547,7 @@ find(const basic_value& v, const key& ky) const auto& tab = v.as_table(); if(tab.count(ky) == 0) { - throw std::out_of_range(detail::format_underline(concat_to_string( - "key \"", ky, "\" not found"), { - {std::addressof(detail::get_region(v)), "in this table"} - })); + detail::throw_key_not_found_error(v, ky); } return ::toml::get(tab.at(ky)); } @@ -558,10 +560,7 @@ find(basic_value& v, const key& ky) auto& tab = v.as_table(); if(tab.count(ky) == 0) { - throw std::out_of_range(detail::format_underline(concat_to_string( - "key \"", ky, "\" not found"), { - {std::addressof(detail::get_region(v)), "in this table"} - })); + detail::throw_key_not_found_error(v, ky); } return ::toml::get(tab.at(ky)); } @@ -574,10 +573,7 @@ find(basic_value&& v, const key& ky) typename basic_value::table_type tab = std::move(v).as_table(); if(tab.count(ky) == 0) { - throw std::out_of_range(detail::format_underline(concat_to_string( - "key \"", ky, "\" not found"), { - {std::addressof(detail::get_region(v)), "in this table"} - })); + detail::throw_key_not_found_error(v, ky); } return ::toml::get(std::move(tab.at(ky))); }