Skip to content

Commit 5d98a62

Browse files
Ivan ShynkarenkaIvan Shynkarenka
authored andcommitted
Add support of fmt::formatter specializations
1 parent 51ac6c8 commit 5d98a62

File tree

7 files changed

+265
-124
lines changed

7 files changed

+265
-124
lines changed

include/generator_cpp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ class GeneratorCpp : public Generator
144144
void GenerateStruct_Source(const std::shared_ptr<Package>& p, const std::shared_ptr<StructType>& s);
145145
void GenerateStructOutputStream(const std::shared_ptr<Package>& p, const std::shared_ptr<StructType>& s);
146146
void GenerateStructLoggingStream(const std::shared_ptr<Package>& p, const std::shared_ptr<StructType>& s);
147+
void GenerateStructFormatter(const std::shared_ptr<Package>& p, const std::shared_ptr<StructType>& s);
147148
void GenerateStructHash(const std::shared_ptr<Package>& p, const std::shared_ptr<StructType>& s);
148149
void GenerateStructJson(const std::shared_ptr<Package>& p, const std::shared_ptr<StructType>& s);
149150
void GenerateStructFieldModel_Header(const std::shared_ptr<Package>& p, const std::shared_ptr<StructType>& s);

proto/enums.h

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ enum class EnumByte : uint8_t
3737

3838
std::ostream& operator<<(std::ostream& stream, EnumByte value);
3939

40+
#if defined(FMT_VERSION)
41+
} template <> struct fmt::formatter<enums::EnumByte> : ostream_formatter {}; namespace enums {
42+
#endif
43+
4044
#if defined(LOGGING_PROTOCOL)
4145
CppLogging::Record& operator<<(CppLogging::Record& record, EnumByte value);
4246
#endif
@@ -53,6 +57,10 @@ enum class EnumChar : uint8_t
5357

5458
std::ostream& operator<<(std::ostream& stream, EnumChar value);
5559

60+
#if defined(FMT_VERSION)
61+
} template <> struct fmt::formatter<enums::EnumChar> : ostream_formatter {}; namespace enums {
62+
#endif
63+
5664
#if defined(LOGGING_PROTOCOL)
5765
CppLogging::Record& operator<<(CppLogging::Record& record, EnumChar value);
5866
#endif
@@ -69,6 +77,10 @@ enum class EnumWChar : uint32_t
6977

7078
std::ostream& operator<<(std::ostream& stream, EnumWChar value);
7179

80+
#if defined(FMT_VERSION)
81+
} template <> struct fmt::formatter<enums::EnumWChar> : ostream_formatter {}; namespace enums {
82+
#endif
83+
7284
#if defined(LOGGING_PROTOCOL)
7385
CppLogging::Record& operator<<(CppLogging::Record& record, EnumWChar value);
7486
#endif
@@ -85,6 +97,10 @@ enum class EnumInt8 : int8_t
8597

8698
std::ostream& operator<<(std::ostream& stream, EnumInt8 value);
8799

100+
#if defined(FMT_VERSION)
101+
} template <> struct fmt::formatter<enums::EnumInt8> : ostream_formatter {}; namespace enums {
102+
#endif
103+
88104
#if defined(LOGGING_PROTOCOL)
89105
CppLogging::Record& operator<<(CppLogging::Record& record, EnumInt8 value);
90106
#endif
@@ -101,6 +117,10 @@ enum class EnumUInt8 : uint8_t
101117

102118
std::ostream& operator<<(std::ostream& stream, EnumUInt8 value);
103119

120+
#if defined(FMT_VERSION)
121+
} template <> struct fmt::formatter<enums::EnumUInt8> : ostream_formatter {}; namespace enums {
122+
#endif
123+
104124
#if defined(LOGGING_PROTOCOL)
105125
CppLogging::Record& operator<<(CppLogging::Record& record, EnumUInt8 value);
106126
#endif
@@ -117,6 +137,10 @@ enum class EnumInt16 : int16_t
117137

118138
std::ostream& operator<<(std::ostream& stream, EnumInt16 value);
119139

140+
#if defined(FMT_VERSION)
141+
} template <> struct fmt::formatter<enums::EnumInt16> : ostream_formatter {}; namespace enums {
142+
#endif
143+
120144
#if defined(LOGGING_PROTOCOL)
121145
CppLogging::Record& operator<<(CppLogging::Record& record, EnumInt16 value);
122146
#endif
@@ -133,6 +157,10 @@ enum class EnumUInt16 : uint16_t
133157

134158
std::ostream& operator<<(std::ostream& stream, EnumUInt16 value);
135159

160+
#if defined(FMT_VERSION)
161+
} template <> struct fmt::formatter<enums::EnumUInt16> : ostream_formatter {}; namespace enums {
162+
#endif
163+
136164
#if defined(LOGGING_PROTOCOL)
137165
CppLogging::Record& operator<<(CppLogging::Record& record, EnumUInt16 value);
138166
#endif
@@ -149,6 +177,10 @@ enum class EnumInt32 : int32_t
149177

150178
std::ostream& operator<<(std::ostream& stream, EnumInt32 value);
151179

180+
#if defined(FMT_VERSION)
181+
} template <> struct fmt::formatter<enums::EnumInt32> : ostream_formatter {}; namespace enums {
182+
#endif
183+
152184
#if defined(LOGGING_PROTOCOL)
153185
CppLogging::Record& operator<<(CppLogging::Record& record, EnumInt32 value);
154186
#endif
@@ -165,6 +197,10 @@ enum class EnumUInt32 : uint32_t
165197

166198
std::ostream& operator<<(std::ostream& stream, EnumUInt32 value);
167199

200+
#if defined(FMT_VERSION)
201+
} template <> struct fmt::formatter<enums::EnumUInt32> : ostream_formatter {}; namespace enums {
202+
#endif
203+
168204
#if defined(LOGGING_PROTOCOL)
169205
CppLogging::Record& operator<<(CppLogging::Record& record, EnumUInt32 value);
170206
#endif
@@ -181,6 +217,10 @@ enum class EnumInt64 : int64_t
181217

182218
std::ostream& operator<<(std::ostream& stream, EnumInt64 value);
183219

220+
#if defined(FMT_VERSION)
221+
} template <> struct fmt::formatter<enums::EnumInt64> : ostream_formatter {}; namespace enums {
222+
#endif
223+
184224
#if defined(LOGGING_PROTOCOL)
185225
CppLogging::Record& operator<<(CppLogging::Record& record, EnumInt64 value);
186226
#endif
@@ -197,6 +237,10 @@ enum class EnumUInt64 : uint64_t
197237

198238
std::ostream& operator<<(std::ostream& stream, EnumUInt64 value);
199239

240+
#if defined(FMT_VERSION)
241+
} template <> struct fmt::formatter<enums::EnumUInt64> : ostream_formatter {}; namespace enums {
242+
#endif
243+
200244
#if defined(LOGGING_PROTOCOL)
201245
CppLogging::Record& operator<<(CppLogging::Record& record, EnumUInt64 value);
202246
#endif
@@ -298,10 +342,12 @@ struct Enums
298342

299343
} // namespace enums
300344

301-
namespace std {
345+
#if defined(FMT_VERSION)
346+
template <> struct fmt::formatter<enums::Enums> : ostream_formatter {};
347+
#endif
302348

303349
template<>
304-
struct hash<enums::Enums>
350+
struct std::hash<enums::Enums>
305351
{
306352
typedef enums::Enums argument_type;
307353
typedef size_t result_type;
@@ -313,8 +359,6 @@ struct hash<enums::Enums>
313359
}
314360
};
315361

316-
} // namespace std
317-
318362
namespace enums {
319363

320364
} // namespace enums

proto/fbe.h

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,20 @@ class decimal_t
321321

322322
} // namespace FBE
323323

324-
namespace std {
324+
#if defined(FMT_VERSION)
325+
template <>
326+
struct fmt::formatter<FBE::decimal_t> : formatter<std::string_view>
327+
{
328+
template <typename FormatContext>
329+
auto format(const FBE::decimal_t& value, FormatContext& ctx) const
330+
{
331+
return formatter<string_view>::format((double)value, ctx);
332+
}
333+
};
334+
#endif
325335

326336
template <>
327-
struct hash<FBE::decimal_t>
337+
struct std::hash<FBE::decimal_t>
328338
{
329339
typedef FBE::decimal_t argument_type;
330340
typedef size_t result_type;
@@ -337,8 +347,6 @@ struct hash<FBE::decimal_t>
337347
}
338348
};
339349

340-
} // namespace std
341-
342350
namespace FBE {
343351

344352
// Register a new enum-based flags macro
@@ -530,10 +538,20 @@ class uuid_t
530538

531539
} // namespace FBE
532540

533-
namespace std {
541+
#if defined(FMT_VERSION)
542+
template <>
543+
struct fmt::formatter<FBE::uuid_t> : formatter<std::string_view>
544+
{
545+
template <typename FormatContext>
546+
auto format(const FBE::uuid_t& value, FormatContext& ctx) const
547+
{
548+
return formatter<string_view>::format(value.string(), ctx);
549+
}
550+
};
551+
#endif
534552

535553
template <>
536-
struct hash<FBE::uuid_t>
554+
struct std::hash<FBE::uuid_t>
537555
{
538556
typedef FBE::uuid_t argument_type;
539557
typedef size_t result_type;
@@ -548,8 +566,6 @@ struct hash<FBE::uuid_t>
548566
}
549567
};
550568

551-
} // namespace std
552-
553569
namespace FBE {
554570

555571
// Fast Binary Encoding buffer based on the dynamic byte buffer

proto/proto.h

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ enum class OrderSide : uint8_t
3333

3434
std::ostream& operator<<(std::ostream& stream, OrderSide value);
3535

36+
#if defined(FMT_VERSION)
37+
} template <> struct fmt::formatter<proto::OrderSide> : ostream_formatter {}; namespace proto {
38+
#endif
39+
3640
#if defined(LOGGING_PROTOCOL)
3741
CppLogging::Record& operator<<(CppLogging::Record& record, OrderSide value);
3842
#endif
@@ -46,6 +50,10 @@ enum class OrderType : uint8_t
4650

4751
std::ostream& operator<<(std::ostream& stream, OrderType value);
4852

53+
#if defined(FMT_VERSION)
54+
} template <> struct fmt::formatter<proto::OrderType> : ostream_formatter {}; namespace proto {
55+
#endif
56+
4957
#if defined(LOGGING_PROTOCOL)
5058
CppLogging::Record& operator<<(CppLogging::Record& record, OrderType value);
5159
#endif
@@ -65,6 +73,10 @@ FBE_ENUM_FLAGS(State)
6573

6674
std::ostream& operator<<(std::ostream& stream, State value);
6775

76+
#if defined(FMT_VERSION)
77+
} template <> struct fmt::formatter<proto::State> : ostream_formatter {}; namespace proto {
78+
#endif
79+
6880
#if defined(LOGGING_PROTOCOL)
6981
CppLogging::Record& operator<<(CppLogging::Record& record, State value);
7082
#endif
@@ -106,10 +118,12 @@ struct Order
106118

107119
} // namespace proto
108120

109-
namespace std {
121+
#if defined(FMT_VERSION)
122+
template <> struct fmt::formatter<proto::Order> : ostream_formatter {};
123+
#endif
110124

111125
template<>
112-
struct hash<proto::Order>
126+
struct std::hash<proto::Order>
113127
{
114128
typedef proto::Order argument_type;
115129
typedef size_t result_type;
@@ -122,8 +136,6 @@ struct hash<proto::Order>
122136
}
123137
};
124138

125-
} // namespace std
126-
127139
namespace proto {
128140

129141
struct Balance
@@ -159,10 +171,12 @@ struct Balance
159171

160172
} // namespace proto
161173

162-
namespace std {
174+
#if defined(FMT_VERSION)
175+
template <> struct fmt::formatter<proto::Balance> : ostream_formatter {};
176+
#endif
163177

164178
template<>
165-
struct hash<proto::Balance>
179+
struct std::hash<proto::Balance>
166180
{
167181
typedef proto::Balance argument_type;
168182
typedef size_t result_type;
@@ -175,8 +189,6 @@ struct hash<proto::Balance>
175189
}
176190
};
177191

178-
} // namespace std
179-
180192
namespace proto {
181193

182194
struct Account
@@ -216,10 +228,12 @@ struct Account
216228

217229
} // namespace proto
218230

219-
namespace std {
231+
#if defined(FMT_VERSION)
232+
template <> struct fmt::formatter<proto::Account> : ostream_formatter {};
233+
#endif
220234

221235
template<>
222-
struct hash<proto::Account>
236+
struct std::hash<proto::Account>
223237
{
224238
typedef proto::Account argument_type;
225239
typedef size_t result_type;
@@ -232,8 +246,6 @@ struct hash<proto::Account>
232246
}
233247
};
234248

235-
} // namespace std
236-
237249
namespace proto {
238250

239251
struct OrderMessage
@@ -268,10 +280,12 @@ struct OrderMessage
268280

269281
} // namespace proto
270282

271-
namespace std {
283+
#if defined(FMT_VERSION)
284+
template <> struct fmt::formatter<proto::OrderMessage> : ostream_formatter {};
285+
#endif
272286

273287
template<>
274-
struct hash<proto::OrderMessage>
288+
struct std::hash<proto::OrderMessage>
275289
{
276290
typedef proto::OrderMessage argument_type;
277291
typedef size_t result_type;
@@ -283,8 +297,6 @@ struct hash<proto::OrderMessage>
283297
}
284298
};
285299

286-
} // namespace std
287-
288300
namespace proto {
289301

290302
struct BalanceMessage
@@ -319,10 +331,12 @@ struct BalanceMessage
319331

320332
} // namespace proto
321333

322-
namespace std {
334+
#if defined(FMT_VERSION)
335+
template <> struct fmt::formatter<proto::BalanceMessage> : ostream_formatter {};
336+
#endif
323337

324338
template<>
325-
struct hash<proto::BalanceMessage>
339+
struct std::hash<proto::BalanceMessage>
326340
{
327341
typedef proto::BalanceMessage argument_type;
328342
typedef size_t result_type;
@@ -334,8 +348,6 @@ struct hash<proto::BalanceMessage>
334348
}
335349
};
336350

337-
} // namespace std
338-
339351
namespace proto {
340352

341353
struct AccountMessage
@@ -370,10 +382,12 @@ struct AccountMessage
370382

371383
} // namespace proto
372384

373-
namespace std {
385+
#if defined(FMT_VERSION)
386+
template <> struct fmt::formatter<proto::AccountMessage> : ostream_formatter {};
387+
#endif
374388

375389
template<>
376-
struct hash<proto::AccountMessage>
390+
struct std::hash<proto::AccountMessage>
377391
{
378392
typedef proto::AccountMessage argument_type;
379393
typedef size_t result_type;
@@ -385,8 +399,6 @@ struct hash<proto::AccountMessage>
385399
}
386400
};
387401

388-
} // namespace std
389-
390402
namespace proto {
391403

392404
} // namespace proto

0 commit comments

Comments
 (0)