Skip to content

Commit

Permalink
Add support of fmt::formatter specializations
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Shynkarenka authored and Ivan Shynkarenka committed Mar 13, 2022
1 parent 51ac6c8 commit 5d98a62
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 124 deletions.
1 change: 1 addition & 0 deletions include/generator_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class GeneratorCpp : public Generator
void GenerateStruct_Source(const std::shared_ptr<Package>& p, const std::shared_ptr<StructType>& s);
void GenerateStructOutputStream(const std::shared_ptr<Package>& p, const std::shared_ptr<StructType>& s);
void GenerateStructLoggingStream(const std::shared_ptr<Package>& p, const std::shared_ptr<StructType>& s);
void GenerateStructFormatter(const std::shared_ptr<Package>& p, const std::shared_ptr<StructType>& s);
void GenerateStructHash(const std::shared_ptr<Package>& p, const std::shared_ptr<StructType>& s);
void GenerateStructJson(const std::shared_ptr<Package>& p, const std::shared_ptr<StructType>& s);
void GenerateStructFieldModel_Header(const std::shared_ptr<Package>& p, const std::shared_ptr<StructType>& s);
Expand Down
52 changes: 48 additions & 4 deletions proto/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ enum class EnumByte : uint8_t

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

#if defined(FMT_VERSION)
} template <> struct fmt::formatter<enums::EnumByte> : ostream_formatter {}; namespace enums {
#endif

#if defined(LOGGING_PROTOCOL)
CppLogging::Record& operator<<(CppLogging::Record& record, EnumByte value);
#endif
Expand All @@ -53,6 +57,10 @@ enum class EnumChar : uint8_t

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

#if defined(FMT_VERSION)
} template <> struct fmt::formatter<enums::EnumChar> : ostream_formatter {}; namespace enums {
#endif

#if defined(LOGGING_PROTOCOL)
CppLogging::Record& operator<<(CppLogging::Record& record, EnumChar value);
#endif
Expand All @@ -69,6 +77,10 @@ enum class EnumWChar : uint32_t

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

#if defined(FMT_VERSION)
} template <> struct fmt::formatter<enums::EnumWChar> : ostream_formatter {}; namespace enums {
#endif

#if defined(LOGGING_PROTOCOL)
CppLogging::Record& operator<<(CppLogging::Record& record, EnumWChar value);
#endif
Expand All @@ -85,6 +97,10 @@ enum class EnumInt8 : int8_t

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

#if defined(FMT_VERSION)
} template <> struct fmt::formatter<enums::EnumInt8> : ostream_formatter {}; namespace enums {
#endif

#if defined(LOGGING_PROTOCOL)
CppLogging::Record& operator<<(CppLogging::Record& record, EnumInt8 value);
#endif
Expand All @@ -101,6 +117,10 @@ enum class EnumUInt8 : uint8_t

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

#if defined(FMT_VERSION)
} template <> struct fmt::formatter<enums::EnumUInt8> : ostream_formatter {}; namespace enums {
#endif

#if defined(LOGGING_PROTOCOL)
CppLogging::Record& operator<<(CppLogging::Record& record, EnumUInt8 value);
#endif
Expand All @@ -117,6 +137,10 @@ enum class EnumInt16 : int16_t

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

#if defined(FMT_VERSION)
} template <> struct fmt::formatter<enums::EnumInt16> : ostream_formatter {}; namespace enums {
#endif

#if defined(LOGGING_PROTOCOL)
CppLogging::Record& operator<<(CppLogging::Record& record, EnumInt16 value);
#endif
Expand All @@ -133,6 +157,10 @@ enum class EnumUInt16 : uint16_t

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

#if defined(FMT_VERSION)
} template <> struct fmt::formatter<enums::EnumUInt16> : ostream_formatter {}; namespace enums {
#endif

#if defined(LOGGING_PROTOCOL)
CppLogging::Record& operator<<(CppLogging::Record& record, EnumUInt16 value);
#endif
Expand All @@ -149,6 +177,10 @@ enum class EnumInt32 : int32_t

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

#if defined(FMT_VERSION)
} template <> struct fmt::formatter<enums::EnumInt32> : ostream_formatter {}; namespace enums {
#endif

#if defined(LOGGING_PROTOCOL)
CppLogging::Record& operator<<(CppLogging::Record& record, EnumInt32 value);
#endif
Expand All @@ -165,6 +197,10 @@ enum class EnumUInt32 : uint32_t

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

#if defined(FMT_VERSION)
} template <> struct fmt::formatter<enums::EnumUInt32> : ostream_formatter {}; namespace enums {
#endif

#if defined(LOGGING_PROTOCOL)
CppLogging::Record& operator<<(CppLogging::Record& record, EnumUInt32 value);
#endif
Expand All @@ -181,6 +217,10 @@ enum class EnumInt64 : int64_t

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

#if defined(FMT_VERSION)
} template <> struct fmt::formatter<enums::EnumInt64> : ostream_formatter {}; namespace enums {
#endif

#if defined(LOGGING_PROTOCOL)
CppLogging::Record& operator<<(CppLogging::Record& record, EnumInt64 value);
#endif
Expand All @@ -197,6 +237,10 @@ enum class EnumUInt64 : uint64_t

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

#if defined(FMT_VERSION)
} template <> struct fmt::formatter<enums::EnumUInt64> : ostream_formatter {}; namespace enums {
#endif

#if defined(LOGGING_PROTOCOL)
CppLogging::Record& operator<<(CppLogging::Record& record, EnumUInt64 value);
#endif
Expand Down Expand Up @@ -298,10 +342,12 @@ struct Enums

} // namespace enums

namespace std {
#if defined(FMT_VERSION)
template <> struct fmt::formatter<enums::Enums> : ostream_formatter {};
#endif

template<>
struct hash<enums::Enums>
struct std::hash<enums::Enums>
{
typedef enums::Enums argument_type;
typedef size_t result_type;
Expand All @@ -313,8 +359,6 @@ struct hash<enums::Enums>
}
};

} // namespace std

namespace enums {

} // namespace enums
32 changes: 24 additions & 8 deletions proto/fbe.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,20 @@ class decimal_t

} // namespace FBE

namespace std {
#if defined(FMT_VERSION)
template <>
struct fmt::formatter<FBE::decimal_t> : formatter<std::string_view>
{
template <typename FormatContext>
auto format(const FBE::decimal_t& value, FormatContext& ctx) const
{
return formatter<string_view>::format((double)value, ctx);
}
};
#endif

template <>
struct hash<FBE::decimal_t>
struct std::hash<FBE::decimal_t>
{
typedef FBE::decimal_t argument_type;
typedef size_t result_type;
Expand All @@ -337,8 +347,6 @@ struct hash<FBE::decimal_t>
}
};

} // namespace std

namespace FBE {

// Register a new enum-based flags macro
Expand Down Expand Up @@ -530,10 +538,20 @@ class uuid_t

} // namespace FBE

namespace std {
#if defined(FMT_VERSION)
template <>
struct fmt::formatter<FBE::uuid_t> : formatter<std::string_view>
{
template <typename FormatContext>
auto format(const FBE::uuid_t& value, FormatContext& ctx) const
{
return formatter<string_view>::format(value.string(), ctx);
}
};
#endif

template <>
struct hash<FBE::uuid_t>
struct std::hash<FBE::uuid_t>
{
typedef FBE::uuid_t argument_type;
typedef size_t result_type;
Expand All @@ -548,8 +566,6 @@ struct hash<FBE::uuid_t>
}
};

} // namespace std

namespace FBE {

// Fast Binary Encoding buffer based on the dynamic byte buffer
Expand Down
60 changes: 36 additions & 24 deletions proto/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ enum class OrderSide : uint8_t

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

#if defined(FMT_VERSION)
} template <> struct fmt::formatter<proto::OrderSide> : ostream_formatter {}; namespace proto {
#endif

#if defined(LOGGING_PROTOCOL)
CppLogging::Record& operator<<(CppLogging::Record& record, OrderSide value);
#endif
Expand All @@ -46,6 +50,10 @@ enum class OrderType : uint8_t

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

#if defined(FMT_VERSION)
} template <> struct fmt::formatter<proto::OrderType> : ostream_formatter {}; namespace proto {
#endif

#if defined(LOGGING_PROTOCOL)
CppLogging::Record& operator<<(CppLogging::Record& record, OrderType value);
#endif
Expand All @@ -65,6 +73,10 @@ FBE_ENUM_FLAGS(State)

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

#if defined(FMT_VERSION)
} template <> struct fmt::formatter<proto::State> : ostream_formatter {}; namespace proto {
#endif

#if defined(LOGGING_PROTOCOL)
CppLogging::Record& operator<<(CppLogging::Record& record, State value);
#endif
Expand Down Expand Up @@ -106,10 +118,12 @@ struct Order

} // namespace proto

namespace std {
#if defined(FMT_VERSION)
template <> struct fmt::formatter<proto::Order> : ostream_formatter {};
#endif

template<>
struct hash<proto::Order>
struct std::hash<proto::Order>
{
typedef proto::Order argument_type;
typedef size_t result_type;
Expand All @@ -122,8 +136,6 @@ struct hash<proto::Order>
}
};

} // namespace std

namespace proto {

struct Balance
Expand Down Expand Up @@ -159,10 +171,12 @@ struct Balance

} // namespace proto

namespace std {
#if defined(FMT_VERSION)
template <> struct fmt::formatter<proto::Balance> : ostream_formatter {};
#endif

template<>
struct hash<proto::Balance>
struct std::hash<proto::Balance>
{
typedef proto::Balance argument_type;
typedef size_t result_type;
Expand All @@ -175,8 +189,6 @@ struct hash<proto::Balance>
}
};

} // namespace std

namespace proto {

struct Account
Expand Down Expand Up @@ -216,10 +228,12 @@ struct Account

} // namespace proto

namespace std {
#if defined(FMT_VERSION)
template <> struct fmt::formatter<proto::Account> : ostream_formatter {};
#endif

template<>
struct hash<proto::Account>
struct std::hash<proto::Account>
{
typedef proto::Account argument_type;
typedef size_t result_type;
Expand All @@ -232,8 +246,6 @@ struct hash<proto::Account>
}
};

} // namespace std

namespace proto {

struct OrderMessage
Expand Down Expand Up @@ -268,10 +280,12 @@ struct OrderMessage

} // namespace proto

namespace std {
#if defined(FMT_VERSION)
template <> struct fmt::formatter<proto::OrderMessage> : ostream_formatter {};
#endif

template<>
struct hash<proto::OrderMessage>
struct std::hash<proto::OrderMessage>
{
typedef proto::OrderMessage argument_type;
typedef size_t result_type;
Expand All @@ -283,8 +297,6 @@ struct hash<proto::OrderMessage>
}
};

} // namespace std

namespace proto {

struct BalanceMessage
Expand Down Expand Up @@ -319,10 +331,12 @@ struct BalanceMessage

} // namespace proto

namespace std {
#if defined(FMT_VERSION)
template <> struct fmt::formatter<proto::BalanceMessage> : ostream_formatter {};
#endif

template<>
struct hash<proto::BalanceMessage>
struct std::hash<proto::BalanceMessage>
{
typedef proto::BalanceMessage argument_type;
typedef size_t result_type;
Expand All @@ -334,8 +348,6 @@ struct hash<proto::BalanceMessage>
}
};

} // namespace std

namespace proto {

struct AccountMessage
Expand Down Expand Up @@ -370,10 +382,12 @@ struct AccountMessage

} // namespace proto

namespace std {
#if defined(FMT_VERSION)
template <> struct fmt::formatter<proto::AccountMessage> : ostream_formatter {};
#endif

template<>
struct hash<proto::AccountMessage>
struct std::hash<proto::AccountMessage>
{
typedef proto::AccountMessage argument_type;
typedef size_t result_type;
Expand All @@ -385,8 +399,6 @@ struct hash<proto::AccountMessage>
}
};

} // namespace std

namespace proto {

} // namespace proto
Loading

0 comments on commit 5d98a62

Please sign in to comment.