Repository navigation
Fmt wrappers - #3505
Fmt wrappers#3505tomc271 wants to merge 44 commits into
Conversation
so that fmt library will handle them automatically.
(by falling back to the default 'c' representation).
as fmt will handle it (via the new `format_as` wrappers).
…fined (ODR) Prevents fmt compilation issues.
to match the original code flow.
(Clang-Tidy)
(Clang-Tidy)
…ions
('*' has higher precedence than '+').
…ions
('*' has higher precedence than '+').
…s implicitly inlined
so no need to call `toString()` explicitly.
(as it's needed for other functions, such as ::SQ).
… to bout_types.cxx and add `toString` and `format_as()` to bout_types.hxx for the `const DirectionTypes&` type
to call its `toString()` method.
Not required as handled by fmt::formatter for SpecificInd.
Clang-Tidy: No header providing "UNUSED" is directly included
Not required as handled by fmt::formatter for SpecificInd.
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
ZedThree
left a comment
There was a problem hiding this comment.
Thanks @tomc271, I didn't realise it was as simple as adding format_as, very nice!
As well as the comments below, a couple of other things:
- could we flip the implementations, so that
format_ashas the actual implementation andtoStringjust callsformat_as? - then we can add
[deprecated]to alltoStringdefinitions with a nice message about usingfmt::formatinstead
| std::string toString(const DirectionTypes& dir); | ||
| inline std::string format_as(const DirectionTypes& dir) { return toString(dir); } |
There was a problem hiding this comment.
It doesn't particularly matter, but could these be moved below the definition of DirectionTypes, and then we can drop the forward-decl on L31?
| namespace bout { | ||
| inline std::string format_as(const ConductionMethod& method) { return toString(method); } | ||
| } // namespace bout |
There was a problem hiding this comment.
If we add this to the BOUT_ENUM_CLASS{,_NS} macros, then we don't need to add it manually
| #include <type_traits> | ||
|
|
||
| template <IND_TYPE N> | ||
| struct fmt::formatter<SpecificInd<N>> { |
There was a problem hiding this comment.
What was the issue that required this being moved?
The motivation for having it in this header, rather than region.hxx, was to make compilation cheaper -- this would only be included where needed
There was a problem hiding this comment.
Just because having it in the same file prevented "unknown format specifier" errors at compile-time when the output_bout_types.hxx file isn't explicitly included
e.g.
include/bout/field.hxx: In instantiation of ‘void bout::checkFinite(const T&, const std::string&, const std::string&) [with T = Field2D; std::string = std::__cxx11::basic_string]’:
src/mesh/metric_tensor.cxx:33:20: required from here
include/bout/field.hxx:303:13: in ‘constexpr’ expansion of ‘fmt::v12::fstring<const std::__cxx11::basic_string<char, std::char_traits, std::allocator >&, SpecificInd<IND_TYPE::IND_2D>&>("{:s} is not finite at {:s}")’
| struct fmt::formatter<SpecificInd<N>> { | ||
| char presentation = 'c'; | ||
|
|
||
| // Parses format specifications: ['c' | 'i' | 's'] or empty {} |
There was a problem hiding this comment.
It was added to prevent this line failing:
throw BoutException("{:s} is not finite at {:s}", name, i);
The second :s here is a SpecificInd, so the parse() method of fmt::formatter<SpecificInd<N>> fails unless 's' is explicitly handled.
Perhaps the right solution is instead to change the calling code to:
throw BoutException("{:s} is not finite at {:c}", name, i);
or:
throw BoutException("{:s} is not finite at {:i}", name, i);
There was a problem hiding this comment.
Oh, that's because it has migrated from %s when we were using printf style formatting. Probably {:c} makes the most sense.
| // Custom fmt formatter for SpecificInd | ||
| template <IND_TYPE N> | ||
| struct fmt::formatter<SpecificInd<N>> { | ||
| char presentation = 'c'; |
There was a problem hiding this comment.
This has lost the docstring about the meanings of c and i
which were accidentally deleted when moving.
…w its definition so forward declaration is not needed.
03533e1 to
2e41f70
Compare
in SpecificInd fmt formatter
…itive()` to field.cxx
(Clang-Tidy suggestion)
…nd is defined (ODR)" This reverts commit 163fda2
| @@ -135,30 +135,30 @@ struct DerivativeStore { | |||
| if (standard.count(key) != 0) { | |||
| throw BoutException("Trying to override standard derivative : " | |||
| "direction {:s}, stagger {:s}, key {:s}", | |||
There was a problem hiding this comment.
| "direction {:s}, stagger {:s}, key {:s}", | |
| "direction {}, stagger {}, key {:s}", |
I assume the s should always be removed?
There was a problem hiding this comment.
Enums using format_as support {:s} automatically, so it's only those types using a custom fmt::formatter<T> template specialisation (such as SpecificInd<N>) that may not handle :s
(because their custom parse() method defines the parsing of format presentation characters).
Even though not necessary, {} might still still be preferable though?
There was a problem hiding this comment.
Yeah, nice to be consistent, then new developers looking at this are less likely to get tripped up
now that fmt::formatter<SpecificInd<N>> is there
| throw BoutException("{:s} is not allocated", name); | ||
| } | ||
|
|
||
| BOUT_FOR_SERIAL(i, f.getRegion(rgn)) { |
There was a problem hiding this comment.
warning: no header providing "BOUT_FOR_SERIAL" is directly included [misc-include-cleaner]
src/field/field.cxx:25:
- #include <bout/bout_types.hxx>
+ #include "bout/region.hxx"
+ #include <bout/bout_types.hxx>
Implement template specializations for the fmt library (or
format_as()wrapper for enums).This removes the need to call
toString()on these types when passing them to BoutException.