OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
format_value.hpp
1#pragma once
2#include <ossia/detail/config.hpp>
3#if defined(OSSIA_HAS_FMT)
4#include <ossia/detail/flat_set.hpp>
5#include <ossia/detail/fmt.hpp>
6#include <ossia/detail/optional.hpp>
7#include <ossia/network/value/value.hpp>
8
9#include <boost/algorithm/string/replace.hpp>
10
11#include <fmt/ranges.h>
12FMT_BEGIN_NAMESPACE
13#if FMT_VERSION >= 120200
14// fmt 12 renamed detail::is_container_adaptor_like -> is_container_adaptor and
15// promoted it out of the detail namespace into fmt itself.
16template <typename T> struct is_container_adaptor<boost::container::flat_set<T>>
17 : std::false_type {
18
19};
20#elif FMT_VERSION >= 100000
21namespace detail {
22template <typename T> class is_container_adaptor_like<boost::container::flat_set<T>>
23 : public std::false_type {
24
25};
26}
27#endif
28FMT_END_NAMESPACE
29namespace ossia
30{
31template <typename FmtCtx>
32struct value_prettyprint_visitor
33{
34 FmtCtx& ctx;
35 using iterator = typename FmtCtx::iterator;
36 iterator operator()(impulse) const;
37 iterator operator()(int32_t i) const;
38 iterator operator()(float f) const;
39 iterator operator()(bool b) const;
40 iterator operator()(char c) const;
41 iterator operator()(std::string str) const;
42 iterator operator()(vec2f vec) const;
43 iterator operator()(vec3f vec) const;
44 iterator operator()(vec4f vec) const;
45 iterator operator()(const std::vector<ossia::value>& t) const;
46 iterator operator()(const value_map_type& t) const;
47 iterator operator()() const;
48};
49}
50
51namespace fmt
52{
53template <typename T>
54struct formatter<std::optional<T>>
55{
56 template <typename ParseContext>
57 constexpr auto parse(ParseContext& ctx)
58 {
59 return ctx.begin();
60 }
61
62 template <typename FormatContext>
63 auto format(const std::optional<T>& n, FormatContext& ctx) const
64 {
65 if(n)
66 {
67 return fmt::format_to(ctx.out(), "{}", *n);
68 }
69 else
70 {
71 return fmt::format_to(ctx.out(), "none");
72 }
73 }
74};
75
76template <>
77struct formatter<ossia::value>
78{
79 template <typename ParseContext>
80 constexpr auto parse(ParseContext& ctx)
81 {
82 return ctx.begin();
83 }
84
85 template <typename FormatContext>
86 auto format(const ossia::value& v, FormatContext& ctx) const
87 {
88 return v.apply(ossia::value_prettyprint_visitor<FormatContext>{ctx});
89 }
90};
91
92}
93
94namespace ossia
95{
96
97template <typename FmtCtx>
98inline typename FmtCtx::iterator
99value_prettyprint_visitor<FmtCtx>::operator()(impulse) const
100{
101 return fmt::format_to(ctx.out(), "impulse");
102}
103
104template <typename FmtCtx>
105inline typename FmtCtx::iterator
106value_prettyprint_visitor<FmtCtx>::operator()(int32_t i) const
107{
108 return fmt::format_to(ctx.out(), "int: {}", i);
109}
110
111template <typename FmtCtx>
112inline typename FmtCtx::iterator
113value_prettyprint_visitor<FmtCtx>::operator()(float f) const
114{
115 return fmt::format_to(ctx.out(), "float: {:.2f}", f);
116}
117
118template <typename FmtCtx>
119inline typename FmtCtx::iterator
120value_prettyprint_visitor<FmtCtx>::operator()(bool b) const
121{
122 return fmt::format_to(ctx.out(), "bool: {}", b ? "true" : "false");
123}
124
125template <typename FmtCtx>
126inline typename FmtCtx::iterator
127value_prettyprint_visitor<FmtCtx>::operator()(char c) const
128{
129 return fmt::format_to(ctx.out(), "char: '{}'", c);
130}
131
132template <typename FmtCtx>
133inline typename FmtCtx::iterator
134value_prettyprint_visitor<FmtCtx>::operator()(std::string str) const
135{
136 boost::algorithm::replace_all(str, "\"", "\\\"");
137 return fmt::format_to(ctx.out(), "string: \"{}\"", str);
138}
139
140template <typename FmtCtx>
141inline typename FmtCtx::iterator
142value_prettyprint_visitor<FmtCtx>::operator()(vec2f vec) const
143{
144 return fmt::format_to(ctx.out(), "vec2f: [{:.2f}, {:.2f}]", vec[0], vec[1]);
145}
146
147template <typename FmtCtx>
148inline typename FmtCtx::iterator
149value_prettyprint_visitor<FmtCtx>::operator()(vec3f vec) const
150{
151 return fmt::format_to(
152 ctx.out(), "vec3f: [{:.2f}, {:.2f}, {:.2f}]", vec[0], vec[1], vec[2]);
153}
154
155template <typename FmtCtx>
156inline typename FmtCtx::iterator
157value_prettyprint_visitor<FmtCtx>::operator()(vec4f vec) const
158{
159 return fmt::format_to(
160 ctx.out(), "vec4f: [{:.2f}, {:.2f}, {:.2f}, {:.2f}]", vec[0], vec[1], vec[2],
161 vec[3]);
162}
163template <typename FmtCtx>
164inline typename FmtCtx::iterator
165value_prettyprint_visitor<FmtCtx>::operator()(const std::vector<value>& t) const
166{
167 fmt::format_to(ctx.out(), "list: [");
168 int i = 0;
169 for(auto& v : t)
170 {
171 v.apply(*this);
172 if(++i < std::ssize(t))
173 fmt::format_to(ctx.out(), ", ");
174 }
175 return fmt::format_to(ctx.out(), "]");
176}
177
178template <typename FmtCtx>
179inline typename FmtCtx::iterator
180value_prettyprint_visitor<FmtCtx>::operator()(const value_map_type& t) const
181{
182 fmt::format_to(ctx.out(), "map: {{");
183 int i = 0;
184 for(auto& v : t)
185 {
186 fmt::format_to(ctx.out(), "\"{}\": ", v.first);
187 v.second.apply(*this);
188 if(++i < std::ssize(t))
189 fmt::format_to(ctx.out(), ", ");
190 }
191 return fmt::format_to(ctx.out(), "}}");
192}
193
194template <typename FmtCtx>
195inline typename FmtCtx::iterator value_prettyprint_visitor<FmtCtx>::operator()() const
196{
197 return fmt::format_to(ctx.out(), "invalid");
198}
199
200}
201#endif
The value class.
Definition value.hpp:173
Definition git_info.h:7