Skip to content

Commit

Permalink
Add custom formatters for use with fmtlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Malacath-92 committed Nov 7, 2024
1 parent eaba057 commit fcdb83b
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
51 changes: 51 additions & 0 deletions include/dla/fmt_format.h
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
60 changes: 60 additions & 0 deletions include/dla/fmt_format.inl
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));
}
2 changes: 1 addition & 1 deletion include/dla/to_string.inl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace dla {
return to_string(val.x);
}
else {
std::string list = std::to_string(val.x);
std::string list = to_string(val.x);
for (std::size_t i = 1; i < N; i++) {
list += ", " + to_string(val[i]);
}
Expand Down

0 comments on commit fcdb83b

Please sign in to comment.