From 43b4f34a43ffb59b03a4a84a696c66c33bebcd79 Mon Sep 17 00:00:00 2001 From: Benjamin Morgan Date: Mon, 10 Jul 2023 12:45:02 +0200 Subject: [PATCH] fable: Perfect-forward arguments to fmt::format --- fable/include/fable/conf.hpp | 4 +-- fable/include/fable/error.hpp | 50 ++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/fable/include/fable/conf.hpp b/fable/include/fable/conf.hpp index d66ddff9e..28431cb4e 100644 --- a/fable/include/fable/conf.hpp +++ b/fable/include/fable/conf.hpp @@ -321,8 +321,8 @@ class Conf { std::string resolve_file(const std::string& filename) const; template - [[noreturn]] void throw_error(const char* msg, const Args&... args) const { - throw_error(fmt::format(msg, args...)); + [[noreturn]] void throw_error(std::string_view format, Args&&... args) const { + throw_error(fmt::format(format, std::forward(args)...)); } [[noreturn]] void throw_error(const std::string& msg) const; [[noreturn]] void throw_unexpected(const std::string& key, const std::string& msg = "") const; diff --git a/fable/include/fable/error.hpp b/fable/include/fable/error.hpp index 776b10df1..4a2906568 100644 --- a/fable/include/fable/error.hpp +++ b/fable/include/fable/error.hpp @@ -41,7 +41,8 @@ class Error : public std::exception { explicit Error(const char* what) : err_(what) {} template - explicit Error(const char* format, const Args&... args) : err_(fmt::format(format, args...)) {} + explicit Error(std::string_view format, Args&&... args) + : err_(fmt::format(format, std::forward(args)...)) {} virtual ~Error() noexcept = default; @@ -59,9 +60,10 @@ class ConfError : public Error { ConfError(const Conf& c, const std::string& msg) : Error(msg), data_(c) {} ConfError(const Conf& c, const char* msg) : Error(msg), data_(c) {} + template - ConfError(const Conf& c, const char* format, const Args&... args) - : Error(format, args...), data_(c) {} + ConfError(const Conf& c, std::string_view format, Args&&... args) + : Error(format, std::forward(args)...), data_(c) {} std::string file() const { return data_.file(); } std::string root() const { return data_.root(); } @@ -122,19 +124,49 @@ class SchemaError : public ConfError { public: // Constructors virtual ~SchemaError() noexcept = default; + /** + * Construct SchemaError with a ConfError. + * + * \param c ConfError + * \param s Schema used for validation + */ SchemaError(const ConfError& c, const Json& s) : ConfError(c), schema_(s) {} + /** + * Construct SchemaError with a ConfError. + * + * \param c ConfError + * \param s Schema used for validation + * \param ctx Extra contextual data as JSON + */ SchemaError(const ConfError& c, const Json& s, const Json& ctx) : ConfError(c), schema_(s), context_(ctx) {} + /** + * Construct SchemaError. + * + * \param c Input Conf where error occurred + * \param s Schema used for validation + * \param format Message format string for fmt::format + * \param args Arguments to message format + */ template - SchemaError(const Conf& c, const Json& s, const char* format, const Args&... args) - : ConfError(c, format, args...), schema_(s) {} - + SchemaError(const Conf& c, const Json& s, std::string_view format, Args&&... args) + : ConfError(c, format, std::forward(args)...), schema_(s) {} + + /** + * Construct SchemaError. + * + * \param c Input Conf where error occurred + * \param s Schema used for validation + * \param ctx Extra contextual data as JSON + * \param format Message format string for fmt::format + * \param args Arguments to message format + */ template - SchemaError(const Conf& c, const Json& s, const Json& ctx, const char* format, - const Args&... args) - : ConfError(c, format, args...), schema_(s), context_(ctx) {} + SchemaError(const Conf& c, const Json& s, const Json& ctx, std::string_view format, + Args&&... args) + : ConfError(c, format, std::forward(args)...), schema_(s), context_(ctx) {} public: // Special const Json& schema() const { return schema_; }