Skip to content

Commit 43b4f34

Browse files
committed
fable: Perfect-forward arguments to fmt::format
1 parent 7c9b960 commit 43b4f34

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

fable/include/fable/conf.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ class Conf {
321321
std::string resolve_file(const std::string& filename) const;
322322

323323
template <typename... Args>
324-
[[noreturn]] void throw_error(const char* msg, const Args&... args) const {
325-
throw_error(fmt::format(msg, args...));
324+
[[noreturn]] void throw_error(std::string_view format, Args&&... args) const {
325+
throw_error(fmt::format(format, std::forward<Args>(args)...));
326326
}
327327
[[noreturn]] void throw_error(const std::string& msg) const;
328328
[[noreturn]] void throw_unexpected(const std::string& key, const std::string& msg = "") const;

fable/include/fable/error.hpp

+41-9
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class Error : public std::exception {
4141
explicit Error(const char* what) : err_(what) {}
4242

4343
template <typename... Args>
44-
explicit Error(const char* format, const Args&... args) : err_(fmt::format(format, args...)) {}
44+
explicit Error(std::string_view format, Args&&... args)
45+
: err_(fmt::format(format, std::forward<Args>(args)...)) {}
4546

4647
virtual ~Error() noexcept = default;
4748

@@ -59,9 +60,10 @@ class ConfError : public Error {
5960

6061
ConfError(const Conf& c, const std::string& msg) : Error(msg), data_(c) {}
6162
ConfError(const Conf& c, const char* msg) : Error(msg), data_(c) {}
63+
6264
template <typename... Args>
63-
ConfError(const Conf& c, const char* format, const Args&... args)
64-
: Error(format, args...), data_(c) {}
65+
ConfError(const Conf& c, std::string_view format, Args&&... args)
66+
: Error(format, std::forward<Args>(args)...), data_(c) {}
6567

6668
std::string file() const { return data_.file(); }
6769
std::string root() const { return data_.root(); }
@@ -122,19 +124,49 @@ class SchemaError : public ConfError {
122124
public: // Constructors
123125
virtual ~SchemaError() noexcept = default;
124126

127+
/**
128+
* Construct SchemaError with a ConfError.
129+
*
130+
* \param c ConfError
131+
* \param s Schema used for validation
132+
*/
125133
SchemaError(const ConfError& c, const Json& s) : ConfError(c), schema_(s) {}
126134

135+
/**
136+
* Construct SchemaError with a ConfError.
137+
*
138+
* \param c ConfError
139+
* \param s Schema used for validation
140+
* \param ctx Extra contextual data as JSON
141+
*/
127142
SchemaError(const ConfError& c, const Json& s, const Json& ctx)
128143
: ConfError(c), schema_(s), context_(ctx) {}
129144

145+
/**
146+
* Construct SchemaError.
147+
*
148+
* \param c Input Conf where error occurred
149+
* \param s Schema used for validation
150+
* \param format Message format string for fmt::format
151+
* \param args Arguments to message format
152+
*/
130153
template <typename... Args>
131-
SchemaError(const Conf& c, const Json& s, const char* format, const Args&... args)
132-
: ConfError(c, format, args...), schema_(s) {}
133-
154+
SchemaError(const Conf& c, const Json& s, std::string_view format, Args&&... args)
155+
: ConfError(c, format, std::forward<Args>(args)...), schema_(s) {}
156+
157+
/**
158+
* Construct SchemaError.
159+
*
160+
* \param c Input Conf where error occurred
161+
* \param s Schema used for validation
162+
* \param ctx Extra contextual data as JSON
163+
* \param format Message format string for fmt::format
164+
* \param args Arguments to message format
165+
*/
134166
template <typename... Args>
135-
SchemaError(const Conf& c, const Json& s, const Json& ctx, const char* format,
136-
const Args&... args)
137-
: ConfError(c, format, args...), schema_(s), context_(ctx) {}
167+
SchemaError(const Conf& c, const Json& s, const Json& ctx, std::string_view format,
168+
Args&&... args)
169+
: ConfError(c, format, std::forward<Args>(args)...), schema_(s), context_(ctx) {}
138170

139171
public: // Special
140172
const Json& schema() const { return schema_; }

0 commit comments

Comments
 (0)