WebCFace 3.2.0
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"
9#ifdef WEBCFACE_MESON
10#include "webcface-config.h"
11#else
12#include "webcface/common/webcface-config.h"
13#endif
14
16
22 std::size_t index;
23
24 public:
25 ValueElementRef(const Field &base, std::size_t index)
26 : Field(base), index(index) {}
27
36 const ValueElementRef &set(double v) const;
37 const ValueElementRef &operator=(double v) const { return set(v); }
38
44 std::optional<double> tryGet() const;
51 double get() const { return tryGet().value_or(0); }
52};
53
61class WEBCFACE_DLL Value : protected Field {
62 public:
63 Value() = default;
64 Value(const Field &base);
65 Value(const Field &base, const SharedString &field)
66 : Value(Field{base, field}) {}
67
68 friend class ValueElementRef;
69
70 using Field::lastName;
71 using Field::lastNameW;
72 using Field::member;
73 using Field::name;
74 using Field::nameW;
82 return this->Field::child(static_cast<SharedString &>(field));
83 }
88 [[deprecated]]
89 Value child(int index) const {
90 return this->Field::child(std::to_string(index));
91 }
100 return child(std::move(field));
101 }
106 Value operator[](const char *field) const { return child(field); }
110 Value operator[](const wchar_t *field) const { return child(field); }
115 template <typename CharT, std::size_t N,
116 typename std::enable_if_t<std::is_same_v<CharT, char> ||
117 std::is_same_v<CharT, wchar_t>,
118 std::nullptr_t> = nullptr>
119 Value operator[](const CharT (&static_str)[N]) {
120 return child(StringInitializer(static_str));
121 }
126 template <std::size_t N>
127 Value operator[](const wchar_t (&static_str)[N]) {
128 return child(StringInitializer(static_str));
129 }
141 template <typename T,
142 std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>
144 return ValueElementRef(*this, index);
145 }
150 ValueElementRef at(std::size_t index) const {
151 return ValueElementRef(*this, index);
152 }
157 Value parent() const { return this->Field::parent(); }
158
165 const Value &
166 onChange(std::function<void WEBCFACE_CALL_FP(Value)> callback) const;
173 template <typename F, typename std::enable_if_t<std::is_invocable_v<F>,
174 std::nullptr_t> = nullptr>
175 const Value &onChange(F callback) const {
176 return onChange(
177 [callback = std::move(callback)](const auto &) { callback(); });
178 }
179
187 const Value &set(double v) const;
193 const Value &set(std::vector<double> v) const;
203 template <typename R,
204 typename traits::ArrayLikeTrait<R>::ArrayLike = traits::TraitOk>
205 const Value &set(const R &range) const {
206 return set(traits::arrayLikeToVector(range));
207 }
212 const Value &resize(std::size_t size) const;
216 const Value &push_back(double v) const;
227 std::size_t size() const;
228
233 template <typename T>
234 const Value &operator=(T &&v) const {
235 this->set(std::forward<T>(v));
236 return *this;
237 }
246 const Value &operator=(std::vector<double> v) const {
247 this->set(std::move(v));
248 return *this;
249 }
250
256 const Value &request() const;
263 std::optional<double> tryGet() const;
271 std::optional<NumVector> tryGetVec() const;
276 double get() const { return tryGet().value_or(0); }
285 return tryGetVec().value_or(std::vector<double>{});
286 }
287 operator double() const { return get(); }
288 operator std::vector<double>() const { return getVec(); }
292 operator NumVector() const { return getVec(); }
293
302 bool exists() const;
303
308 const Value &free() const;
309
310 const Value &operator+=(double rhs) const {
311 this->set(this->get() + rhs);
312 return *this;
313 }
314 const Value &operator-=(double rhs) const {
315 this->set(this->get() - rhs);
316 return *this;
317 }
318 const Value &operator*=(double rhs) const {
319 this->set(this->get() * rhs);
320 return *this;
321 }
322 const Value &operator/=(double rhs) const {
323 this->set(this->get() / rhs);
324 return *this;
325 }
326 const Value &operator%=(std::int32_t rhs) const {
327 this->set(static_cast<std::int32_t>(this->get()) % rhs);
328 return *this;
329 }
330 const Value &operator<<=(std::int32_t rhs) const {
331 // todo: int64_tかuint64_tにしたほうがいいかもしれない
332 this->set(static_cast<std::int32_t>(this->get()) << rhs);
333 return *this;
334 }
335 const Value &operator>>=(std::int32_t rhs) const {
336 this->set(static_cast<std::int32_t>(this->get()) >> rhs);
337 return *this;
338 }
339 const Value &operator&=(std::int32_t rhs) const {
340 this->set(static_cast<std::int32_t>(this->get()) & rhs);
341 return *this;
342 }
343 const Value &operator|=(std::int32_t rhs) const {
344 this->set(static_cast<std::int32_t>(this->get()) | rhs);
345 return *this;
346 }
347 const Value &operator^=(std::int32_t rhs) const {
348 this->set(static_cast<std::int32_t>(this->get()) ^ rhs);
349 return *this;
350 }
355 const Value &operator++() const { // ++s
356 this->set(this->get() + 1);
357 return *this;
358 }
363 double operator++(int) const { // s++
364 auto v = this->get();
365 this->set(v + 1);
366 return v;
367 }
372 const Value &operator--() const { // --const s
373 this->set(this->get() - 1);
374 return *this;
375 }
380 double operator--(int) const { // s--
381 auto v = this->get();
382 this->set(v - 1);
383 return v;
384 }
385
394 template <typename T, typename std::enable_if_t<std::is_same_v<T, Value>,
395 std::nullptr_t> = nullptr>
396 bool operator==(const T &other) const {
397 return static_cast<Field>(*this) == static_cast<Field>(other);
398 }
399 template <typename T, typename std::enable_if_t<std::is_same_v<T, Value>,
400 std::nullptr_t> = nullptr>
401 bool operator!=(const T &other) const {
402 return static_cast<Field>(*this) == static_cast<Field>(other);
403 }
404 bool operator<(const Value &) const = delete;
405 bool operator<=(const Value &) const = delete;
406 bool operator>(const Value &) const = delete;
407 bool operator>=(const Value &) const = delete;
408};
409
416WEBCFACE_DLL std::ostream &WEBCFACE_CALL operator<<(std::ostream &os,
417 const Value &data);
418
shared_ptrで管理されているdoubleのvector
Definition num_vector.h:20
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:170
SharedString のpublicなコンストラクタインタフェース (入力専用)
Definition encoding.h:235
配列型のValueデータの一部の要素を指定するクラス
Definition value.h:21
ValueElementRef(const Field &base, std::size_t index)
Definition value.h:25
const ValueElementRef & operator=(double v) const
Definition value.h:37
double get() const
値があればその要素を返す
Definition value.h:51
実数値またはその配列の送受信データを表すクラス
Definition value.h:61
Value operator[](StringInitializer field) const
Definition value.h:99
Value parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition value.h:157
const Value & operator/=(double rhs) const
Definition value.h:322
const Value & operator+=(double rhs) const
Definition value.h:310
const Value & operator|=(std::int32_t rhs) const
Definition value.h:343
bool operator==(const T &other) const
Valueの参照先を比較
Definition value.h:396
ValueElementRef operator[](T index) const
1次元配列型データの要素を参照する
Definition value.h:143
const Value & onChange(F callback) const
値が変化したときに呼び出されるコールバックを設定
Definition value.h:175
const Value & set(const R &range) const
配列型の値をセットする
Definition value.h:205
const Value & operator%=(std::int32_t rhs) const
Definition value.h:326
const Value & operator>>=(std::int32_t rhs) const
Definition value.h:335
double operator--(int) const
1引いたものをsetし、足す前の値を返す
Definition value.h:380
bool operator<=(const Value &) const =delete
NumVector getVec() const
値をvectorで返す
Definition value.h:284
const Value & operator*=(double rhs) const
Definition value.h:318
Value child(int index) const
Definition value.h:89
bool operator!=(const T &other) const
Definition value.h:401
double operator++(int) const
1足したものをsetし、足す前の値を返す
Definition value.h:363
bool operator>=(const Value &) const =delete
const Value & operator&=(std::int32_t rhs) const
Definition value.h:339
const Value & operator--() const
1引いたものをsetした後自身を返す
Definition value.h:372
const Value & operator-=(double rhs) const
Definition value.h:314
Value child(StringInitializer field) const
「(thisの名前).(追加の名前)」を新しい名前とするField
Definition value.h:81
Value()=default
bool operator>(const Value &) const =delete
Value operator[](const wchar_t(&static_str)[N])
Definition value.h:127
const Value & operator=(T &&v) const
数値または配列をセットする
Definition value.h:234
const Value & operator^=(std::int32_t rhs) const
Definition value.h:347
const Value & operator=(std::vector< double > v) const
vector型配列をセットする
Definition value.h:246
ValueElementRef at(std::size_t index) const
1次元配列型データの要素を参照する
Definition value.h:150
bool operator<(const Value &) const =delete
Value operator[](const char *field) const
Definition value.h:106
Value operator[](const wchar_t *field) const
Definition value.h:110
Value operator[](const CharT(&static_str)[N])
Definition value.h:119
double get() const
値を返す
Definition value.h:276
Value(const Field &base, const SharedString &field)
Definition value.h:65
const Value & operator<<=(std::int32_t rhs) const
Definition value.h:330
const Value & operator++() const
1足したものをsetした後自身を返す
Definition value.h:355
std::ostream & operator<<(std::ostream &os, const Arg &arg)
Definition func_info.cc:99
const auto & get(const AxisAngle &aa)
Definition transform.h:227
ClientDataの参照とメンバ名とデータ名を持つクラス
Definition field.h:68
Field parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition field.cc:39
Field child(const SharedString &field) const
Definition field.cc:48
Definition array_like.h:59
#define WEBCFACE_DLL
Definition webcface-config.h:69
#define WEBCFACE_CALL
Definition webcface-config.h:106
#define WEBCFACE_NS_END
Definition webcface-config.h:113
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:112
#define WEBCFACE_CALL_FP
Definition webcface-config.h:107