OSSIA
Open Scenario System for Interactive Application
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>
12 FMT_BEGIN_NAMESPACE
13 namespace detail {
14 template <typename T> class is_container_adaptor_like<boost::container::flat_set<T>>
15  : public std::false_type {
16 
17 };
18 }
19 FMT_END_NAMESPACE
20 namespace ossia
21 {
22 using fmt_ctx = fmt::basic_format_context<fmt::appender, char>;
23 
24 struct value_prettyprint_visitor
25 {
26  fmt_ctx& ctx;
27  fmt_ctx::iterator operator()(impulse) const;
28  fmt_ctx::iterator operator()(int32_t i) const;
29  fmt_ctx::iterator operator()(float f) const;
30  fmt_ctx::iterator operator()(bool b) const;
31  fmt_ctx::iterator operator()(char c) const;
32  fmt_ctx::iterator operator()(std::string str) const;
33  fmt_ctx::iterator operator()(vec2f vec) const;
34  fmt_ctx::iterator operator()(vec3f vec) const;
35  fmt_ctx::iterator operator()(vec4f vec) const;
36  fmt_ctx::iterator operator()(const std::vector<ossia::value>& t) const;
37  fmt_ctx::iterator operator()(const value_map_type& t) const;
38  fmt_ctx::iterator operator()() const;
39 };
40 }
41 
42 namespace fmt
43 {
44 template <typename T>
45 struct formatter<std::optional<T>>
46 {
47  template <typename ParseContext>
48  constexpr auto parse(ParseContext& ctx)
49  {
50  return ctx.begin();
51  }
52 
53  template <typename FormatContext>
54  auto format(const std::optional<T>& n, FormatContext& ctx) const
55  {
56  if(n)
57  {
58  return fmt::format_to(ctx.out(), "{}", *n);
59  }
60  else
61  {
62  return fmt::format_to(ctx.out(), "none");
63  }
64  }
65 };
66 
67 template <>
68 struct formatter<ossia::value>
69 {
70  template <typename ParseContext>
71  constexpr auto parse(ParseContext& ctx)
72  {
73  return ctx.begin();
74  }
75 
76  template <typename FormatContext>
77  auto format(const ossia::value& v, FormatContext& ctx) const
78  {
79  return v.apply(ossia::value_prettyprint_visitor{ctx});
80  }
81 };
82 
83 }
84 
85 namespace ossia
86 {
87 
88 inline fmt_ctx::iterator value_prettyprint_visitor::operator()(impulse) const
89 {
90  return fmt::format_to(ctx.out(), "impulse");
91 }
92 
93 inline fmt_ctx::iterator value_prettyprint_visitor::operator()(int32_t i) const
94 {
95  return fmt::format_to(ctx.out(), "int: {}", i);
96 }
97 
98 inline fmt_ctx::iterator value_prettyprint_visitor::operator()(float f) const
99 {
100  return fmt::format_to(ctx.out(), "float: {}", f);
101 }
102 
103 inline fmt_ctx::iterator value_prettyprint_visitor::operator()(bool b) const
104 {
105  return fmt::format_to(ctx.out(), "bool: {}", b ? "true" : "false");
106 }
107 
108 inline fmt_ctx::iterator value_prettyprint_visitor::operator()(char c) const
109 {
110  return fmt::format_to(ctx.out(), "char: '{}'", c);
111 }
112 
113 inline fmt_ctx::iterator value_prettyprint_visitor::operator()(std::string str) const
114 {
115  boost::algorithm::replace_all(str, "\"", "\\\"");
116  return fmt::format_to(ctx.out(), "string: \"{}\"", str);
117 }
118 
119 inline fmt_ctx::iterator value_prettyprint_visitor::operator()(vec2f vec) const
120 {
121  return fmt::format_to(ctx.out(), "vec2f: {}", vec);
122 }
123 
124 inline fmt_ctx::iterator value_prettyprint_visitor::operator()(vec3f vec) const
125 {
126  return fmt::format_to(ctx.out(), "vec3f: {}", vec);
127 }
128 
129 inline fmt_ctx::iterator value_prettyprint_visitor::operator()(vec4f vec) const
130 {
131  return fmt::format_to(ctx.out(), "vec4f: {}", vec);
132 }
133 
134 inline fmt_ctx::iterator
135 value_prettyprint_visitor::operator()(const std::vector<value>& t) const
136 {
137  return fmt::format_to(ctx.out(), "list: {}", t);
138 }
139 
140 inline fmt_ctx::iterator
141 value_prettyprint_visitor::operator()(const value_map_type& t) const
142 {
143  return fmt::format_to(ctx.out(), "map: {}", t);
144 }
145 
146 inline fmt_ctx::iterator value_prettyprint_visitor::operator()() const
147 {
148  return fmt::format_to(ctx.out(), "invalid");
149 }
150 
151 }
152 #endif
The value class.
Definition: value.hpp:173
Definition: git_info.h:7