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