From 61dfa4a2dce2f8a328f7ec3d5c3d1e735b8049d8 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 15 Mar 2019 12:38:37 +0900 Subject: [PATCH] feat: format any number of values into an err msg ```cpp toml::format_error("[error] message", v1, "v1", v2, "v2", ...); ``` --- toml/value.hpp | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/toml/value.hpp b/toml/value.hpp index 27c2d8a..a866b04 100644 --- a/toml/value.hpp +++ b/toml/value.hpp @@ -794,24 +794,38 @@ inline bool operator>=(const toml::value& lhs, const toml::value& rhs) return !(lhs < rhs); } -inline std::string format_error(const std::string& err_msg, - const toml::value& v, const std::string& comment, - std::vector hints = {}) +namespace detail { - return detail::format_underline(err_msg, { - {std::addressof(detail::get_region(v)), comment} - }, std::move(hints)); +inline std::string format_error_impl(const std::string& err_msg, + std::vector> val, + std::vector hints) +{ + return format_underline(err_msg, std::move(val), std::move(hints)); +} +inline std::string format_error_impl(const std::string& err_msg, + std::vector> val) +{ + return format_underline(err_msg, std::move(val)); } -inline std::string format_error(const std::string& err_msg, - const toml::value& v1, const std::string& comment1, - const toml::value& v2, const std::string& comment2, - std::vector hints = {}) +template +std::string format_error_impl(const std::string& err_msg, + std::vector> val, + const toml::value& v, const std::string& comment, + Ts&& ... args) { - return detail::format_underline(err_msg, { - {std::addressof(detail::get_region(v1)), comment1}, - {std::addressof(detail::get_region(v2)), comment2} - }, std::move(hints)); + val.push_back(std::make_pair(std::addressof(get_region(v)), comment)); + return format_error_impl(err_msg, std::move(val), std::forward(args)...); +} +} // detail + +template +std::string format_error(const std::string& err_msg, Ts&& ... args) +{ + std::vector> val; + val.reserve(sizeof...(args) / 2); + return detail::format_error_impl(err_msg, std::move(val), + std::forward(args)...); } template