-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom formatters for use with fmtlib
- Loading branch information
1 parent
eaba057
commit fcdb83b
Showing
3 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma once | ||
|
||
#if not defined DLA_FMT_FORMAT and defined __has_include | ||
#if __has_include(<fmt/format.h>) | ||
#define DLA_FMT_FORMAT | ||
#endif | ||
#endif | ||
|
||
#ifdef DLA_FMT_FORMAT | ||
#include <dla/units.h> | ||
|
||
#include <fmt/format.h> | ||
|
||
template<class Tag> | ||
struct fmt::formatter<dla::base_unit<Tag>> | ||
{ | ||
template<typename ParseContext> | ||
constexpr auto parse(ParseContext& ctx); | ||
template<typename FormatContext> | ||
constexpr auto format(const dla::base_unit<Tag>& val, FormatContext& ctx) const; | ||
}; | ||
|
||
template<class... Tags> | ||
struct fmt::formatter<dla::comp_unit<Tags...>> | ||
{ | ||
template<typename ParseContext> | ||
constexpr auto parse(ParseContext& ctx); | ||
template<typename FormatContext> | ||
constexpr auto format(const dla::comp_unit<Tags...>& val, FormatContext& ctx) const; | ||
}; | ||
|
||
template<class T, std::size_t N> | ||
struct fmt::formatter<dla::vec<T, N>> | ||
{ | ||
template<typename ParseContext> | ||
constexpr auto parse(ParseContext& ctx); | ||
template<typename FormatContext> | ||
constexpr auto format(const dla::vec<T, N>& val, FormatContext& ctx) const; | ||
}; | ||
|
||
template<class T, std::size_t N, std::size_t M> | ||
struct fmt::formatter<dla::mat<T, N, M>> | ||
{ | ||
template<typename ParseContext> | ||
constexpr auto parse(ParseContext& ctx); | ||
template<typename FormatContext> | ||
constexpr auto format(const dla::mat<T, N, M>& val, FormatContext& ctx) const; | ||
}; | ||
|
||
#include "fmt_format.inl" | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#pragma once | ||
|
||
#include "fmt_format.h" | ||
#include "to_string.h" | ||
|
||
template<class Tag> | ||
template<typename ParseContext> | ||
constexpr auto fmt::formatter<dla::base_unit<Tag>>::parse(ParseContext& ctx) | ||
{ | ||
return ctx.begin(); | ||
} | ||
|
||
template<class Tag> | ||
template<typename FormatContext> | ||
constexpr auto fmt::formatter<dla::base_unit<Tag>>::format(const dla::base_unit<Tag>& val, FormatContext& ctx) const | ||
{ | ||
return fmt::format_to(ctx.out(), "{}", dla::to_string(val)); | ||
} | ||
|
||
template<class... Tags> | ||
template<typename ParseContext> | ||
constexpr auto fmt::formatter<dla::comp_unit<Tags...>>::parse(ParseContext& ctx) | ||
{ | ||
return ctx.begin(); | ||
} | ||
|
||
template<class... Tags> | ||
template<typename FormatContext> | ||
constexpr auto fmt::formatter<dla::comp_unit<Tags...>>::format(const dla::comp_unit<Tags...>& val, FormatContext& ctx) const | ||
{ | ||
return fmt::format_to(ctx.out(), "{}", dla::to_string(val)); | ||
} | ||
|
||
template<class T, std::size_t N> | ||
template<typename ParseContext> | ||
constexpr auto fmt::formatter<dla::vec<T, N>>::parse(ParseContext& ctx) | ||
{ | ||
return ctx.begin(); | ||
} | ||
|
||
template<class T, std::size_t N> | ||
template<typename FormatContext> | ||
constexpr auto fmt::formatter<dla::vec<T, N>>::format(const dla::vec<T, N>& val, FormatContext& ctx) const | ||
{ | ||
return fmt::format_to(ctx.out(), "{}", dla::to_string(val)); | ||
} | ||
|
||
template<class T, std::size_t N, std::size_t M> | ||
template<typename ParseContext> | ||
constexpr auto fmt::formatter<dla::mat<T, N, M>>::parse(ParseContext& ctx) | ||
{ | ||
return ctx.begin(); | ||
} | ||
|
||
template<class T, std::size_t N, std::size_t M> | ||
template<typename FormatContext> | ||
constexpr auto fmt::formatter<dla::mat<T, N, M>>::format(const dla::mat<T, N, M>& val, FormatContext& ctx) const | ||
{ | ||
return fmt::format_to(ctx.out(), "{}", dla::to_string(val)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters