Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ d8f14fdddb5ca0fbb32d8e2bf5ac2960d6ac5ce6
ed2117e6d6826a98b6988e2f18c0c34e408563b6
# Added by the bot
4b010b7634aee1045743be80c268d4644522cd29
421e60ee1015878325bd8b62a65ed0943293ce31
25 changes: 13 additions & 12 deletions include/bout/boutexception.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>
#include <utility>

#include "fmt/base.h"
#include "fmt/core.h"

/// Throw BoutRhsFail with \p message if any one process has non-zero
Expand All @@ -19,10 +20,10 @@ public:
BoutException& operator=(BoutException&&) = delete;
BoutException(std::string msg);

template <class S, class... Args>
BoutException(S&& format, Args&&... args)
: BoutException(fmt::format(fmt::runtime(std::forward<S>(format)),
std::forward<decltype(args)>(args)...)) {}
template <class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
BoutException(fmt::format_string<Args...> format, Args&&... args)
: BoutException(fmt::vformat(format, fmt::make_format_args(args...))) {}

~BoutException() override;

Expand All @@ -44,19 +45,19 @@ private:
class BoutRhsFail : public BoutException {
public:
BoutRhsFail(std::string message) : BoutException(std::move(message)) {}
template <class S, class... Args>
BoutRhsFail(S&& format, Args&&... args)
: BoutRhsFail(fmt::format(fmt::runtime(std::forward<S>(format)),
std::forward<decltype(args)>(args)...)) {}
template <class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
BoutRhsFail(fmt::format_string<Args...> format, Args&&... args)
: BoutRhsFail(fmt::vformat(format, fmt::make_format_args(args...))) {}
};

class BoutIterationFail : public BoutException {
public:
BoutIterationFail(std::string message) : BoutException(std::move(message)) {}
template <class S, class... Args>
BoutIterationFail(S&& format, Args&&... args)
: BoutIterationFail(fmt::format(fmt::runtime(std::forward<S>(format)),
std::forward<decltype(args)>(args)...)) {}
template <class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
BoutIterationFail(fmt::format_string<Args...> format, Args&&... args)
: BoutIterationFail(fmt::vformat(format, fmt::make_format_args(args...))) {}
};

#endif
21 changes: 13 additions & 8 deletions include/bout/msg_stack.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MsgStack;

#include "bout/build_defines.hxx"

#include "fmt/base.h"
#include "fmt/core.h"

#include <cstddef>
Expand Down Expand Up @@ -60,9 +61,10 @@ public:
int push(std::string message);
int push() { return push(""); }

template <class S, class... Args>
int push(const S& format, const Args&... args) {
return push(fmt::format(fmt::runtime(format), args...));
template <class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
int push(fmt::format_string<Args...> format, Args&&... args) {
return push(fmt::vformat(format, fmt::make_format_args(args...)));
}

void pop(); ///< Remove the last message
Expand All @@ -74,8 +76,8 @@ public:
#else
/// Dummy functions which should be optimised out
int push(const std::string&) { return 0; }
template <class S, class... Args>
int push(const S&, const Args&...) {
template <class... Args>
int push(fmt::format_string<Args...> format, const Args&... args) {
return 0;
}

Expand Down Expand Up @@ -132,10 +134,13 @@ public:
MsgStackItem(const std::string& file, int line, const char* msg)
: point(msg_stack.push("{:s} on line {:d} of '{:s}'", msg, line, file)) {}

template <class S, class... Args>
MsgStackItem(const std::string& file, int line, const S& msg, const Args&... args)
template <class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
MsgStackItem(const std::string& file, int line, fmt::format_string<Args...> msg,
Args&&... args)
: point(msg_stack.push("{:s} on line {:d} of '{:s}'",
fmt::format(fmt::runtime(msg), args...), line, file)) {}
fmt::vformat(msg, fmt::make_format_args(args...)), line,
file)) {}
~MsgStackItem() {
// If an exception has occurred, don't pop the message
if (exception_count == std::uncaught_exceptions()) {
Expand Down
46 changes: 39 additions & 7 deletions include/bout/options.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Options;
#include "bout/utils.hxx"

#include <fmt/core.h>
#include <fmt/format.h>

#include <cmath>
#include <functional>
Expand Down Expand Up @@ -626,8 +627,8 @@ public:
// Option not found. Copy the value from the default.
this->_set_no_check(def.value, DEFAULT_SOURCE);

output_info << _("\tOption ") << full_name << " = " << def.full_name << " ("
<< DEFAULT_SOURCE << ")\n";
output_info.write("{}{} = {}({})\n", _("\tOption "), full_name, def.full_name,
DEFAULT_SOURCE);
} else {
// Check if this was previously set as a default option
if (bout::utils::variantEqualTo(attributes.at("source"), DEFAULT_SOURCE)) {
Expand Down Expand Up @@ -911,8 +912,8 @@ private:
<< ")\n";
} else {
throw BoutException(
_("Options: Setting a value from same source ({:s}) to new value "
"'{:s}' - old value was '{:s}'."),
_f("Options: Setting a value from same source ({:s}) to new value "
"'{:s}' - old value was '{:s}'."),
source, toString(val), bout::utils::variantToString(value));
}
}
Expand Down Expand Up @@ -1043,7 +1044,40 @@ namespace details {
/// so that we can put the function definitions in the .cxx file,
/// avoiding lengthy recompilation if we change it
struct OptionsFormatterBase {
auto parse(fmt::format_parse_context& ctx) -> fmt::format_parse_context::iterator;
constexpr auto parse(fmt::format_parse_context& ctx) {
const auto* it = ctx.begin();
const auto* const end = ctx.end();

while (it != end and *it != '}') {
switch (*it) {
case 'd':
docstrings = true;
++it;
break;
case 'i':
inline_section_names = true;
++it;
break;
case 'k':
key_only = true;
++it;
break;
case 's':
source = true;
++it;
break;
case 'u':
unused = true;
++it;
break;
default:
throw fmt::format_error("invalid format for 'Options'");
}
}

return it;
}

auto format(const Options& options, fmt::format_context& ctx) const
-> fmt::format_context::iterator;

Expand All @@ -1060,8 +1094,6 @@ private:
bool key_only{false};
/// Include the 'source' attribute, if present
bool source{false};
/// Format string to passed down to subsections
std::string format_string;
};
} // namespace details
} // namespace bout
Expand Down
17 changes: 10 additions & 7 deletions include/bout/optionsreader.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class OptionsReader;

#include "bout/options.hxx"

#include "fmt/core.h"
#include <fmt/base.h>
#include <fmt/core.h>

#include <string>
#include <vector>
Expand Down Expand Up @@ -69,9 +70,10 @@ public:
/// @param[in] file The name of the file. printf style arguments can be used to create the file name.
void read(Options* options, const std::string& filename);

template <class S, class... Args>
void read(Options* options, const S& format, const Args&... args) {
return read(options, fmt::format(fmt::runtime(format), args...));
template <class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
void read(Options* options, fmt::format_string<Args...> format, Args&&... args) {
return read(options, fmt::vformat(format, fmt::make_format_args(args...)));
}

/// Write options to file
Expand All @@ -80,9 +82,10 @@ public:
/// @param[in] file The name of the file to (over)write
void write(Options* options, const std::string& filename);

template <class S, class... Args>
void write(Options* options, const S& format, const Args&... args) {
return write(options, fmt::format(fmt::runtime(format), args...));
template <class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
void write(Options* options, fmt::format_string<Args...> format, Args&&... args) {
return write(options, fmt::vformat(format, fmt::make_format_args(args...)));
}

/// Parse options from the command line
Expand Down
48 changes: 26 additions & 22 deletions include/bout/output.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class Output;
#include "bout/sys/gettext.hxx" // IWYU pragma: keep for gettext _() macro
#include "bout/unused.hxx"

#include "fmt/core.h"
#include <fmt/base.h>
#include <fmt/core.h>

#include <utility>

Expand Down Expand Up @@ -81,10 +82,10 @@ public:
open(filename);
}

template <class S, class... Args>
Output(const S& format, Args&&... args)
: Output(fmt::format(fmt::runtime(format), std::forward<decltype(args)>(args)...)) {
}
template <class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
Output(fmt::format_string<Args...> format, Args&&... args)
: Output(fmt::vformat(format, fmt::make_format_args(args...))) {}

~Output() override { close(); }

Expand All @@ -94,9 +95,10 @@ public:
/// Open an output log file
int open(const std::string& filename);

template <class S, class... Args>
int open(const S& format, Args&&... args) {
return open(fmt::format(fmt::runtime(format), std::forward<decltype(args)>(args)...));
template <class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
int open(fmt::format_string<Args...> format, Args&&... args) {
return open(fmt::vformat(format, fmt::make_format_args(args...)));
}

/// Close the log file
Expand All @@ -105,16 +107,18 @@ public:
/// Write a string using fmt format
virtual void write(const std::string& message);

template <class S, class... Args>
void write(const S& format, Args&&... args) {
write(fmt::format(fmt::runtime(format), std::forward<decltype(args)>(args)...));
template <class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
void write(fmt::format_string<Args...> format, Args&&... args) {
write(fmt::vformat(format, fmt::make_format_args(args...)));
}
/// Same as write, but only to screen
virtual void print(const std::string& message);

template <class S, class... Args>
void print(const S& format, Args&&... args) {
print(fmt::format(fmt::runtime(format), std::forward<decltype(args)>(args)...));
template <class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
void print(fmt::format_string<Args...> format, Args&&... args) {
print(fmt::vformat(format, fmt::make_format_args(args...)));
}

/// Add an output stream. All output will be sent to all streams
Expand Down Expand Up @@ -175,25 +179,25 @@ public:
/// This string is then sent to log file and stdout (on processor 0)
void write(const std::string& message) override;

template <class S, class... Args>
void write(const S& format, Args&&... args) {
template <class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
void write(fmt::format_string<Args...> format, Args&&... args) {
if (enabled) {
ASSERT1(base != nullptr);
base->write(
fmt::format(fmt::runtime(format), std::forward<decltype(args)>(args)...));
base->write(fmt::vformat(format, fmt::make_format_args(args...)));
}
}

/// If enabled, print a string to stdout using fmt formatting
/// note: unlike write, this is not also sent to log files
void print(const std::string& message) override;

template <class S, class... Args>
void print(const S& format, Args&&... args) {
template <class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
void print(fmt::format_string<Args...> format, Args&&... args) {
if (enabled) {
ASSERT1(base != nullptr);
base->print(
fmt::format(fmt::runtime(format), std::forward<decltype(args)>(args)...));
base->print(fmt::vformat(format, fmt::make_format_args(args...)));
}
}

Expand Down
14 changes: 10 additions & 4 deletions include/bout/sys/expressionparser.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@

#include "bout/unused.hxx"

#include "fmt/core.h"
#include <fmt/base.h>
#include <fmt/core.h>

#include <exception>
#include <list>
Expand Down Expand Up @@ -239,11 +240,16 @@ private:

class ParseException : public std::exception {
public:
ParseException(const ParseException&) = default;
ParseException(ParseException&&) = delete;
ParseException& operator=(const ParseException&) = default;
ParseException& operator=(ParseException&&) = delete;
ParseException(const std::string& message_) : message(message_) {}

template <class S, class... Args>
ParseException(const S& format, const Args&... args)
: message(fmt::format(fmt::runtime(format), args...)) {}
template <class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
ParseException(fmt::format_string<Args...> format, Args&&... args)
: message(fmt::vformat(format, fmt::make_format_args(args...))) {}

~ParseException() override = default;

Expand Down
Loading