OSSIA
Open Scenario System for Interactive Application
value_comparison.hpp
1 #pragma once
2 #include <ossia/network/base/node.hpp>
3 #include <ossia/network/base/parameter.hpp>
4 #include <ossia/network/value/value.hpp>
5 
6 namespace ossia::comparisons
7 {
8 struct Impulse_T
9 {
10  template <typename T>
11  friend bool operator==(const T&, Impulse_T)
12  {
13  return true;
14  }
15  template <typename T>
16  friend bool operator!=(const T&, Impulse_T)
17  {
18  return false;
19  }
20  template <typename T>
21  friend bool operator<=(const T&, Impulse_T)
22  {
23  return true;
24  }
25  template <typename T>
26  friend bool operator>=(const T&, Impulse_T)
27  {
28  return true;
29  }
30  template <typename T>
31  friend bool operator<(const T&, Impulse_T)
32  {
33  return false;
34  }
35  template <typename T>
36  friend bool operator>(const T&, Impulse_T)
37  {
38  return false;
39  }
40 };
41 struct String_T
42 {
43  template <typename T>
44  friend bool operator==(const T&, String_T)
45  {
46  return false;
47  }
48  template <typename T>
49  friend bool operator!=(const T&, String_T)
50  {
51  return true;
52  }
53  template <typename T>
54  friend bool operator<=(const T&, String_T)
55  {
56  return false;
57  }
58  template <typename T>
59  friend bool operator>=(const T&, String_T)
60  {
61  return false;
62  }
63  template <typename T>
64  friend bool operator<(const T&, String_T)
65  {
66  return false;
67  }
68  template <typename T>
69  friend bool operator>(const T&, String_T)
70  {
71  return false;
72  }
73 };
74 
75 struct NumericValue
76 {
77  template <typename T, typename Fun>
78  static bool apply(const T& lhs, const ossia::value& val, Fun fun)
79  {
80  struct visitor
81  {
82  const T& lhs;
83  Fun fun;
84 
85  public:
86  bool operator()(impulse) const { return fun(lhs, Impulse_T{}); }
87  bool operator()(int32_t v) const { return fun(lhs, v); }
88  bool operator()(float v) const { return fun(lhs, v); }
89  bool operator()(bool v) const { return fun(lhs, v); }
90  bool operator()(char v) const { return fun(lhs, v); }
91  bool operator()(const std::vector<ossia::value>& v) const
92  {
93  return (v.size() == 1) && (fun(lhs, v[0]));
94  }
95 
96  bool operator()(const std::string& v) const { return fun(lhs, String_T{}); }
97  bool operator()(vec2f v) const { return false; }
98  bool operator()(vec3f v) const { return false; }
99  bool operator()(vec4f v) const { return false; }
100 
101  bool operator()() const { return false; }
102 
103  } vis{lhs, fun};
104 
105  return val.apply(vis);
106  }
107 };
108 
109 struct StringValue
110 {
111  template <typename Fun>
112  static bool apply(const std::string& lhs, const ossia::value& val, Fun fun)
113  {
114  struct visitor
115  {
116  const std::string& lhs;
117  Fun fun;
118 
119  public:
120  bool operator()(impulse) const { return fun(lhs, Impulse_T{}); }
121  bool operator()(const std::string& v) const { return fun(lhs, v); }
122  bool operator()(int32_t v) const { return fun(v, String_T{}); }
123  bool operator()(float v) const { return fun(v, String_T{}); }
124  bool operator()(bool v) const { return fun(v, String_T{}); }
125  bool operator()(char v) const { return fun(v, String_T{}); }
126  bool operator()(const std::vector<ossia::value>& v) const
127  {
128  return (v.size() == 1) && (fun(lhs, v[0]));
129  }
130  bool operator()(vec2f v) const { return fun(v, String_T{}); }
131  bool operator()(vec3f v) const { return fun(v, String_T{}); }
132  bool operator()(vec4f v) const { return fun(v, String_T{}); }
133 
134  bool operator()() const { return false; }
135 
136  } vis{lhs, fun};
137 
138  return val.apply(vis);
139  }
140 };
141 
142 template <typename Fun>
143 struct ListVisitor
144 {
145  const std::vector<ossia::value>& lhs;
146  const ossia::value& rhs;
147  Fun fun;
148 
149 public:
150  bool operator()(impulse) const { return fun(lhs, Impulse_T{}); }
151  bool operator()(const std::vector<ossia::value>& t) const
152  {
153  if(lhs.size() != t.size())
154  return false;
155 
156  bool result = true;
157  auto tit = t.begin();
158  for(const auto& val : lhs)
159  {
160  result &= fun(val, *tit);
161  if(!result)
162  break;
163  tit++;
164  }
165 
166  return result;
167  }
168 
169  template <typename T>
170  bool operator()(const T& v) const
171  {
172  if(lhs.size() == 1)
173  return fun(lhs[0], rhs);
174 
175  return false;
176  }
177 
178  bool operator()() const { return false; }
179 };
180 template <typename Fun>
181 auto make_list_visitor(
182  const std::vector<ossia::value>& lhs, const ossia::value& val, Fun f)
183 {
184  return ListVisitor<Fun>{lhs, val, f};
185 }
186 
187 struct ListValue
188 {
189  template <typename Fun>
190  static bool
191  apply(const std::vector<ossia::value>& lhs, const ossia::value& val, Fun fun)
192  {
193  auto vis = make_list_visitor(lhs, val, fun);
194 
195  return val.apply(vis);
196  }
197 };
198 
199 template <typename Fun>
200 struct DestinationVisitor
201 {
202  const destination& lhs;
203  const ossia::value& rhs;
204  Fun fun;
205 
206 public:
207  bool operator()(impulse) const { return fun(lhs.value.get(), Impulse_T{}); }
208 
209  template <typename T>
210  bool operator()(const T& v) const
211  {
212  return fun(lhs.address().value(lhs.index), rhs);
213  }
214 
215  bool operator()() const { return false; }
216 };
217 
218 template <typename Fun>
219 auto make_destination_visitor(const destination& lhs, const ossia::value& val, Fun f)
220 {
221  return DestinationVisitor<Fun>{lhs, val, f};
222 }
223 
224 struct DestinationValue
225 {
226  template <typename Fun>
227  static bool apply(const destination& lhs, const ossia::value& val, Fun fun)
228  {
229  return val.apply(make_destination_visitor(lhs, val, fun));
230  }
231 };
232 
233 template <std::size_t N, typename Fun>
234 struct VecVisitor
235 {
236  const std::array<float, N>& lhs;
237  Fun fun;
238 
239 public:
240  bool operator()(impulse) const { return fun(lhs, Impulse_T{}); }
241  bool operator()(const std::array<float, N>& d) const { return fun(lhs, d); }
242 
243  template <typename T>
244  bool operator()(const T& v) const
245  {
246  return false;
247  }
248 
249  bool operator()() const { return false; }
250 };
251 
252 template <typename Vec_T, typename Fun>
253 auto make_vec_visitor(const Vec_T& lhs, Fun f)
254 {
255  return VecVisitor<Vec_T::size_value, Fun>{lhs, f};
256 }
257 
258 struct VecValue
259 {
260  template <typename Vec_T, typename Fun>
261  static bool apply(const Vec_T& lhs, const ossia::value& val, Fun fun)
262  {
263  return val.apply(make_vec_visitor(lhs, fun));
264  }
265 };
266 }
The value class.
Definition: value.hpp:173