Valkyrie 2026
Loading...
Searching...
No Matches
Formatters.h
1#pragma once
2
3#include "valkyrie/util/eigenunit/UnitMap.h"
4
8namespace std {
13template <typename... UnitTags>
14struct formatter<valor::EigenUnit::UnitVector<UnitTags...>> {
16 using Vec = valor::EigenUnit::UnitVector<UnitTags...>;
19
23 constexpr auto parse(format_parse_context& ctx) { return spec.parse(ctx); }
24
28 template <typename FormatContext>
29 auto format(const Vec& vec, FormatContext& ctx) const {
30 auto out = ctx.out();
31 *out++ = '[';
32 format_impl(vec, out, std::make_index_sequence<sizeof...(UnitTags)>{});
33 *out++ = ']';
34 return out;
35 }
36
37 private:
41 template <typename Out, std::size_t... Is>
42 void format_impl(const Vec& vec, Out& out, std::index_sequence<Is...>) const {
43 ((format_element<Is>(vec, out), (Is < sizeof...(UnitTags) - 1 ? (*out++ = ',', *out++ = ' ', void()) : void())), ...);
44 }
45
49 template <std::size_t I, typename Out>
50 void format_element(const Vec& vec, Out& out) const {
51 using Tag = std::tuple_element_t<I, std::tuple<UnitTags...>>;
52 std::string s = spec.format_value(vec.template Get<I>().value());
53 if (!spec.values_only)
54 s += std::string(" ") + units::abbreviation(units::unit_t<Tag>{});
55 out = std::ranges::copy(s, out).out;
56 }
57};
58
63template <typename... UnitTags>
64struct formatter<valor::EigenUnit::UnitMap<UnitTags...>> {
66 using Map = valor::EigenUnit::UnitMap<UnitTags...>;
69
73 constexpr auto parse(format_parse_context& ctx) { return spec.parse(ctx); }
74
78 template <typename FormatContext>
79 auto format(const Map& m, FormatContext& ctx) const {
80 return std::formatter<valor::EigenUnit::UnitVector<UnitTags...>>{}.format(m.ToVector(), ctx);
81 }
82};
83
88template <typename... UnitTags>
89struct formatter<valor::EigenUnit::ConstUnitMap<UnitTags...>> {
94
98 constexpr auto parse(format_parse_context& ctx) { return spec.parse(ctx); }
99
103 template <typename FormatContext>
104 auto format(const CMap& m, FormatContext& ctx) const {
105 return std::formatter<valor::EigenUnit::UnitVector<UnitTags...>>{}.format(m.ToVector(), ctx);
106 }
107};
108} // namespace std
A non-owning, read-only unit-safe view over a const double* buffer.
Definition UnitMap.h:202
UnitVector< UnitTags... > ToVector() const
Produce an owned UnitVector copy of the mapped data.
Definition UnitMap.h:250
A non-owning, unit-safe view over a raw double* buffer.
Definition UnitMap.h:18
UnitVector< UnitTags... > ToVector() const
Produce an owned UnitVector copy of the mapped data.
Definition UnitMap.h:107
A vector wrapper that enforces units for each element.
Definition UnitVector.h:22
Template specializations for std::tuple_size and std::tuple_element to support UnitVector.
Definition Formatters.h:8
constexpr auto parse(format_parse_context &ctx)
Parses the format specifier.
Definition Formatters.h:98
valor::EigenUnit::ConstUnitMap< UnitTags... > CMap
The ConstUnitMap type being formatted.
Definition Formatters.h:91
valor::EigenUnit::detail::UnitFormatSpec spec
Internal format specification.
Definition Formatters.h:93
auto format(const CMap &m, FormatContext &ctx) const
Formats the ConstUnitMap into the output context.
Definition Formatters.h:104
valor::EigenUnit::detail::UnitFormatSpec spec
Internal format specification.
Definition Formatters.h:68
auto format(const Map &m, FormatContext &ctx) const
Formats the UnitMap into the output context.
Definition Formatters.h:79
valor::EigenUnit::UnitMap< UnitTags... > Map
The UnitMap type being formatted.
Definition Formatters.h:66
constexpr auto parse(format_parse_context &ctx)
Parses the format specifier.
Definition Formatters.h:73
constexpr auto parse(format_parse_context &ctx)
Parses the format specifier.
Definition Formatters.h:23
auto format(const Vec &vec, FormatContext &ctx) const
Formats the UnitVector into the output context.
Definition Formatters.h:29
valor::EigenUnit::UnitVector< UnitTags... > Vec
The UnitVector type being formatted.
Definition Formatters.h:16
valor::EigenUnit::detail::UnitFormatSpec spec
Internal format specification.
Definition Formatters.h:18
Parses a shared format spec used by UnitVector, UnitMap, ConstUnitMap.
Definition Common.h:140
std::string format_value(double v) const
Formats a single double value according to the spec.
Definition Common.h:180
bool values_only
If true, only values are printed (no unit abbreviations).
Definition Common.h:144