WebCFace 2.5.2
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
value.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <ostream>
4#include <optional>
5#include <chrono>
6#include "field.h"
7#include "array_like.h"
8#ifdef WEBCFACE_MESON
9#include "webcface-config.h"
10#else
11#include "webcface/common/webcface-config.h"
12#endif
13
15
23class WEBCFACE_DLL Value : protected Field {
24 public:
25 Value() = default;
26 Value(const Field &base);
27 Value(const Field &base, const SharedString &field)
28 : Value(Field{base, field}) {}
29
30 using Field::lastName;
31 using Field::member;
32 using Field::name;
33 using Field::nameW;
38 Value child(std::string_view field) const {
39 return this->Field::child(field);
40 }
45 Value child(std::wstring_view field) const {
46 return this->Field::child(field);
47 }
51 Value child(int index) const { return this->Field::child(index); }
56 Value operator[](std::string_view field) const { return child(field); }
61 Value operator[](std::wstring_view field) const { return child(field); }
66 Value operator[](const char *field) const { return child(field); }
70 Value operator[](const wchar_t *field) const { return child(field); }
75 Value operator[](int index) const { return child(index); }
80 Value parent() const { return this->Field::parent(); }
81
88 const Value &
89 onChange(std::function<void WEBCFACE_CALL_FP(Value)> callback) const;
96 template <typename F, typename std::enable_if_t<std::is_invocable_v<F>,
97 std::nullptr_t> = nullptr>
98 const Value &onChange(F callback) const {
99 return onChange(
100 [callback = std::move(callback)](const auto &) { callback(); });
101 }
109 template <typename T>
110 [[deprecated]] void appendListener(T &&callback) const {
111 onChange(std::forward<T>(callback));
112 }
113
121 const Value &set(double v) const;
127 const Value &set(std::vector<double> v) const;
137 template <typename R,
138 typename traits::ArrayLikeTrait<R>::ArrayLike = traits::TraitOk>
139 const Value &set(const R &range) const {
140 return set(traits::arrayLikeToVector(range));
141 }
146 const Value &resize(std::size_t size) const;
150 const Value &push_back(double v) const;
151
156 template <typename T>
157 const Value &operator=(T &&v) const {
158 this->set(std::forward<T>(v));
159 return *this;
160 }
169 const Value &operator=(std::vector<double> v) const {
170 this->set(std::move(v));
171 return *this;
172 }
173
179 const Value &request() const;
186 std::optional<double> tryGet() const;
191 std::optional<std::vector<double>> tryGetVec() const;
196 double get() const { return tryGet().value_or(0); }
201 std::vector<double> getVec() const {
202 return tryGetVec().value_or(std::vector<double>{});
203 }
204 operator double() const { return get(); }
205 operator std::vector<double>() const { return getVec(); }
206
215 bool exists() const;
220 [[deprecated]] std::chrono::system_clock::time_point time() const;
221
226 const Value &free() const;
227
228 const Value &operator+=(double rhs) const {
229 this->set(this->get() + rhs);
230 return *this;
231 }
232 const Value &operator-=(double rhs) const {
233 this->set(this->get() - rhs);
234 return *this;
235 }
236 const Value &operator*=(double rhs) const {
237 this->set(this->get() * rhs);
238 return *this;
239 }
240 const Value &operator/=(double rhs) const {
241 this->set(this->get() / rhs);
242 return *this;
243 }
244 const Value &operator%=(std::int32_t rhs) const {
245 this->set(static_cast<std::int32_t>(this->get()) % rhs);
246 return *this;
247 }
248 const Value &operator<<=(std::int32_t rhs) const {
249 // todo: int64_tかuint64_tにしたほうがいいかもしれない
250 this->set(static_cast<std::int32_t>(this->get()) << rhs);
251 return *this;
252 }
253 const Value &operator>>=(std::int32_t rhs) const {
254 this->set(static_cast<std::int32_t>(this->get()) >> rhs);
255 return *this;
256 }
257 const Value &operator&=(std::int32_t rhs) const {
258 this->set(static_cast<std::int32_t>(this->get()) & rhs);
259 return *this;
260 }
261 const Value &operator|=(std::int32_t rhs) const {
262 this->set(static_cast<std::int32_t>(this->get()) | rhs);
263 return *this;
264 }
265 const Value &operator^=(std::int32_t rhs) const {
266 this->set(static_cast<std::int32_t>(this->get()) ^ rhs);
267 return *this;
268 }
273 const Value &operator++() const { // ++s
274 this->set(this->get() + 1);
275 return *this;
276 }
281 double operator++(int) const { // s++
282 auto v = this->get();
283 this->set(v + 1);
284 return v;
285 }
290 const Value &operator--() const { // --const s
291 this->set(this->get() - 1);
292 return *this;
293 }
298 double operator--(int) const { // s--
299 auto v = this->get();
300 this->set(v - 1);
301 return v;
302 }
303
312 template <typename T, typename std::enable_if_t<std::is_same_v<T, Value>,
313 std::nullptr_t> = nullptr>
314 bool operator==(const T &other) const {
315 return static_cast<Field>(*this) == static_cast<Field>(other);
316 }
317 template <typename T, typename std::enable_if_t<std::is_same_v<T, Value>,
318 std::nullptr_t> = nullptr>
319 bool operator!=(const T &other) const {
320 return static_cast<Field>(*this) == static_cast<Field>(other);
321 }
322 bool operator<(const Value &) const = delete;
323 bool operator<=(const Value &) const = delete;
324 bool operator>(const Value &) const = delete;
325 bool operator>=(const Value &) const = delete;
326};
327
334WEBCFACE_DLL std::ostream &WEBCFACE_CALL operator<<(std::ostream &os,
335 const Value &data);
336
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:69
実数値またはその配列の送受信データを表すクラス
Definition value.h:23
Value parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition value.h:80
const Value & operator/=(double rhs) const
Definition value.h:240
const Value & operator+=(double rhs) const
Definition value.h:228
const Value & operator|=(std::int32_t rhs) const
Definition value.h:261
bool operator==(const T &other) const
Valueの参照先を比較
Definition value.h:314
const Value & onChange(F callback) const
値が変化したときに呼び出されるコールバックを設定
Definition value.h:98
const Value & set(const R &range) const
配列型の値をセットする
Definition value.h:139
std::vector< double > getVec() const
値をvectorで返す
Definition value.h:201
const Value & operator%=(std::int32_t rhs) const
Definition value.h:244
const Value & operator>>=(std::int32_t rhs) const
Definition value.h:253
double operator--(int) const
1引いたものをsetし、足す前の値を返す
Definition value.h:298
Value operator[](std::string_view field) const
Definition value.h:56
Value child(std::string_view field) const
「(thisの名前).(追加の名前)」を新しい名前とするField
Definition value.h:38
bool operator<=(const Value &) const =delete
const Value & operator*=(double rhs) const
Definition value.h:236
Value child(int index) const
Definition value.h:51
bool operator!=(const T &other) const
Definition value.h:319
Value operator[](int index) const
Definition value.h:75
double operator++(int) const
1足したものをsetし、足す前の値を返す
Definition value.h:281
bool operator>=(const Value &) const =delete
Value child(std::wstring_view field) const
「(thisの名前).(追加の名前)」を新しい名前とするField (wstring)
Definition value.h:45
void appendListener(T &&callback) const
Definition value.h:110
const Value & operator&=(std::int32_t rhs) const
Definition value.h:257
const Value & operator--() const
1引いたものをsetした後自身を返す
Definition value.h:290
const Value & operator-=(double rhs) const
Definition value.h:232
Value()=default
bool operator>(const Value &) const =delete
const Value & operator=(T &&v) const
数値または配列をセットする
Definition value.h:157
const Value & operator^=(std::int32_t rhs) const
Definition value.h:265
Value operator[](std::wstring_view field) const
Definition value.h:61
const Value & operator=(std::vector< double > v) const
vector型配列をセットする
Definition value.h:169
bool operator<(const Value &) const =delete
Value operator[](const char *field) const
Definition value.h:66
Value operator[](const wchar_t *field) const
Definition value.h:70
double get() const
値を返す
Definition value.h:196
Value(const Field &base, const SharedString &field)
Definition value.h:27
const Value & operator<<=(std::int32_t rhs) const
Definition value.h:248
const Value & operator++() const
1足したものをsetした後自身を返す
Definition value.h:273
std::ostream & operator<<(std::ostream &os, const Arg &arg)
Definition func_info.cc:96
ClientDataの参照とメンバ名とデータ名を持つクラス
Definition field.h:70
Field parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition field.cc:33
Field child(const SharedString &field) const
Definition field.cc:42
Definition array_like.h:58
#define WEBCFACE_DLL
Definition webcface-config.h:60
#define WEBCFACE_CALL
Definition webcface-config.h:97
#define WEBCFACE_NS_END
Definition webcface-config.h:104
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:103
#define WEBCFACE_CALL_FP
Definition webcface-config.h:98