Skip to content

Commit 0e57afa

Browse files
committed
added enum19 fmt support
1 parent 3303c63 commit 0e57afa

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/enum19.lib/enum19/Enum.fmt.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
#include "Enum.names.h"
3+
#include "string19/StringView.fmt.h"
4+
5+
/// noto: for this header you need fmt library (not included as library dependency)
6+
#include <fmt/format.h>
7+
8+
namespace fmt {
9+
10+
/// adds strong support for fmt
11+
template<enum19::HasMetaEnum T, class char_type> struct formatter<T, char_type> {
12+
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
13+
14+
template<typename FormatCtx> auto format(const T& v, FormatCtx& ctx) {
15+
auto underlying = static_cast<std::underlying_type_t<T>>(v);
16+
return format_to(ctx.out(), "{} ({})", valueName(v), underlying);
17+
}
18+
};
19+
20+
} // namespace fmt

src/enum19.lib/enum19/Enum.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ template<class T> constexpr void metaEnumFor(ADL*, T*) {
3434

3535
template<class T> constexpr auto meta_enum_for = metaEnumFor(adl, nullptr_to<T>);
3636

37+
template<class T> concept HasMetaEnum = requires(T v) { {meta_enum_for<T>}; };
38+
3739
template<class Enum> struct MetaEnumMember {
3840
size_t index;
3941
StringView name;

src/enum19.lib/enum19/enum19.qbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
Product {
33
Depends { name: "cpp" }
44
Depends { name: "string19" }
5+
Depends { name: "fmt"; required: false }
56

67
files: [
78
"ADL.h",
89
"Enum.extras.ostream.h",
10+
"Enum.fmt.h",
911
"Enum.h",
1012
"Enum.names.h",
1113
"Enum.ostream.h",

0 commit comments

Comments
 (0)