Skip to content

Commit

Permalink
fable: Perfect-forward arguments to fmt::format
Browse files Browse the repository at this point in the history
  • Loading branch information
cassava committed Apr 22, 2024
1 parent 7c9b960 commit 43b4f34
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
4 changes: 2 additions & 2 deletions fable/include/fable/conf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ class Conf {
std::string resolve_file(const std::string& filename) const;

template <typename... Args>
[[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>(args)...));
}
[[noreturn]] void throw_error(const std::string& msg) const;
[[noreturn]] void throw_unexpected(const std::string& key, const std::string& msg = "") const;
Expand Down
50 changes: 41 additions & 9 deletions fable/include/fable/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class Error : public std::exception {
explicit Error(const char* what) : err_(what) {}

template <typename... Args>
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>(args)...)) {}

virtual ~Error() noexcept = default;

Expand All @@ -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 <typename... Args>
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>(args)...), data_(c) {}

std::string file() const { return data_.file(); }
std::string root() const { return data_.root(); }
Expand Down Expand Up @@ -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 <typename... Args>
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>(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 <typename... Args>
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>(args)...), schema_(s), context_(ctx) {}

public: // Special
const Json& schema() const { return schema_; }
Expand Down

0 comments on commit 43b4f34

Please sign in to comment.