Anklang 0.3.0-460-gc4ef46ba
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
value.hh
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#ifndef __ASE_VALUE_HH__
3#define __ASE_VALUE_HH__
4
5#include <ase/defs.hh>
6#include <variant>
7
8namespace Ase {
9
10struct ValueS : std::vector<ValueP> {
13 void push_back (const Value &v) { push_back (std::make_shared<Value> (v));}
14 void push_back (Value &&v) { push_back (std::make_shared<Value> (std::move (v)));}
15 String repr () const;
16 ValueS ();
18 static const ValueS empty_array;
19 friend bool operator== (const ValuePVector&, const ValuePVector&);
20 friend bool operator!= (const ValuePVector &a, const ValuePVector &b) { return !(a == b); }
21};
22
23struct ValueField {
24 std::string name;
25 ValueP value;
26 ValueField ();
27 ValueField (const String &nam, const Value &val);
28 ValueField (const String &nam, Value &&val);
29 ValueField (const String &nam, ValueP val);
30 bool operator== (const ValueField &other) const;
31 bool operator!= (const ValueField &other) const { return !(other == *this); }
32};
33
34struct ValueR : std::vector<ValueField> {
36 using ValueFieldVector::operator[];
37 Value& operator[] (const String &name);
38 const Value& operator[] (const String &name) const;
39 ValueP valuep (const String &name, bool front);
40 ValueP peek (const String &name) const;
41 String repr () const;
42 ValueR ();
44 static const ValueR empty_record;
45};
46
49
50template<size_t I, size_t J> using IDX = typename ::std::enable_if<I == J, bool>::type;
51template<class T, class C> using TYP = typename ::std::enable_if<std::is_same<T, C>::value, bool>::type;
52
55 using ValueVariant::ValueVariant;
56 enum Type { NONE, BOOL, INT64, DOUBLE, STRING, ARRAY, RECORD, INSTANCE };
57 constexpr Type index () const { return Type (ValueVariant::index()); }
58 size_t count () const;
59 int64 as_int () const;
60 double as_double () const;
61 String as_string () const;
62 const ValueS& as_array () const;
63 const ValueR& as_record () const;
64 String repr () const;
65 StringS keys () const;
66 bool has (const String &key) const;
67 void filter (const std::function<bool (const ValueField&)> &pred);
68 bool is_numeric (bool boolisnumeric = true) const;
69 bool is_string () const { return index() == Type::STRING; }
70 void operator= (bool v) { ValueVariant::operator= (v); }
71 void operator= (int64 v) { ValueVariant::operator= (v); }
72 void operator= (int32 v) { ValueVariant::operator= (int64 (v)); }
73 void operator= (uint32 v) { ValueVariant::operator= (int64 (v)); }
74 void operator= (double v) { ValueVariant::operator= (v); }
75 void operator= (const char *v) { ValueVariant::operator= (String (v)); }
76 void operator= (const String &v) { ValueVariant::operator= (v); }
77 void operator= (const ValueS &v) { ValueVariant::operator= (v); }
78 void operator= (ValueS &&v) { ValueVariant::operator= (std::move (v)); }
79 void operator= (const ValueR &v) { ValueVariant::operator= (v); }
80 void operator= (ValueR &&v) { ValueVariant::operator= (std::move (v)); }
81 void operator= (const InstanceP &v) { ValueVariant::operator= (v); }
82 Value& operator[] (size_t i);
83 const Value& operator[] (size_t i) const;
84 Value& operator[] (const String &name);
85 const Value& operator[] (const String &name) const;
86 Value () {}
87 Value (bool v) { this->operator= (v); }
88 Value (int64 v) { this->operator= (v); }
89 Value (int32 v) { this->operator= (int64 (v)); }
90 Value (uint32 v) { this->operator= (int64 (v)); }
91 Value (double v) { this->operator= (v); }
92 Value (const char *v) { this->operator= (String (v)); }
93 Value (const String &v) { this->operator= (v); }
94 bool operator== (const Value &other) const;
95 bool operator!= (const Value &other) const { return !(other == *this); }
96 static const Value empty_value;
97};
98
99// == EnumInfo ==
101struct EnumInfo {
102 String label;
103 String blurb;
104 template<class E> static EnumInfo value_info (E value);
105 static EnumInfo value_info (const std::type_info &enumtype, int64 value);
106 template<class E> static bool impl (EnumInfo (*enuminfo) (E));
107private:
108 static void impl (const std::type_info&, const std::function<EnumInfo(int64)>&);
109};
110
111// == Event ==
113struct Event : ValueR {
114 String type() const { return (*this)["type"].as_string(); }
115 String detail() const { return (*this)["detail"].as_string(); }
116 using ValueR::operator[];
117 Event ();
118 Event (const String &type, const String &detail, std::initializer_list<ValueField> il = {});
119};
120
121// == JsTrigger ==
124 class Impl;
126 void call (ValueS &&args) const;
127 using VoidArgsFunc = std::function<void (ValueS)>;
128 using VoidFunc = std::function<void()>;
129public:
130 static JsTrigger create (const String &id, const VoidArgsFunc &f);
131 String id () const;
132 void ondestroy (const VoidFunc &vf);
133 void destroy ();
134 explicit operator bool () const noexcept;
135 template<class ...A> void
136 operator() (const A &...a) const
137 {
138 ValueS args;
139 (args.push_back (a), ...); // C++17 fold expression
140 call (std::move (args));
141 }
142};
143
144// == Implementations ==
145inline bool
146Value::operator== (const Value &other) const
147{
148 const ValueVariant &v1 = *this, &v2 = other;
149 return v1 == v2;
150}
151
152inline bool
153operator== (const std::vector<ValueP> &v1, const std::vector<ValueP> &v2)
154{
155 if (v1.size() != v2.size())
156 return false;
157 for (size_t i = 0; i < v1.size(); i++)
158 {
159 const ValueP p1 = v1[i], p2 = v2[i];
160 if (p1 && p2)
161 {
162 if (*p1 != *p2)
163 return false;
164 }
165 else if (p1 != p2)
166 return false;
167 }
168 return true;
169}
170
171inline bool
172ValueField::operator== (const ValueField &other) const
173{
174 if (other.name != name)
175 return false;
176 else if (value && other.value)
177 return *value == *other.value;
178 else
179 return value == other.value;
180}
181
182template<class E> inline EnumInfo
183EnumInfo::value_info (E value)
184{
185 return value_info (typeid (E), int64 (value));
186}
187
188template<class E> inline bool
189EnumInfo::impl (EnumInfo (*enuminfo) (E))
190{
191 ASE_ASSERT_RETURN (enuminfo != nullptr, true);
192 impl (typeid (E), [enuminfo] (int64 v) { return enuminfo (E (v)); });
193 return false;
194}
195
196} // Ase
197
198#endif // __ASE_VALUE_HH__
Callback mechanism for Jsonapi/Jsonipc.
Definition value.hh:123
#define ASE_ASSERT_RETURN(expr,...)
Return from the current function if expr evaluates to false and issue an assertion warning.
Definition cxxaux.hh:82
T index(T... args)
The Anklang C++ API namespace.
Definition api.hh:9
int32_t int32
A 32-bit signed integer.
Definition cxxaux.hh:28
int64_t int64
A 64-bit unsigned integer, use PRI*64 in format strings.
Definition cxxaux.hh:29
uint32_t uint32
A 32-bit unsigned integer.
Definition cxxaux.hh:24
T operator=(T... args)
T push_back(T... args)
Get auxiallary enum information.
Definition value.hh:101
Structure for callback based notifications.
Definition value.hh:113
Value type used to interface with various property types.
Definition value.hh:54
double as_double() const
Convert Value to double or return 0.
Definition value.cc:77
String repr() const
Convert Value to a string representation, useful for debugging.
Definition value.cc:243
bool is_numeric(bool boolisnumeric=true) const
Checks if Value is a DOUBLE, INT64, or BOOL.
Definition value.cc:51
int64 as_int() const
Convert Value to int64 or return 0.
Definition value.cc:59
size_t count() const
Number of elements in a RECORD or ARRAY Value.
Definition value.cc:15
bool has(const String &key) const
Check for a named field in a RECORD.
Definition value.cc:28
String as_string() const
Convert Value to a string, not very useful for RECORD or ARRAY.
Definition value.cc:95
void filter(const std::function< bool(const ValueField &)> &pred)
Recursively purge/remove RECORD elements iff to pred (recordfield) == true.
Definition value.cc:262
StringS keys() const
List the field names of a RECORD Value.
Definition value.cc:39
const ValueS & as_array() const
Retrive a non-empty array if Value contains a non-empty array.
Definition value.cc:135