WebCFace 2.9.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"
7#include "array_like.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::member;
72 using Field::name;
73 using Field::nameW;
81 return this->Field::child(static_cast<SharedString &>(field));
82 }
87 [[deprecated]]
88 Value child(int index) const {
89 return this->Field::child(std::to_string(index));
90 }
98 Value operator[](StringInitializer field) const { return child(std::move(field)); }
103 Value operator[](const char *field) const { return child(field); }
107 Value operator[](const wchar_t *field) const { return child(field); }
112 template <std::size_t N>
113 Value operator[](const char (&static_str)[N]) {
114 return child(StringInitializer(static_str));
115 }
120 template <std::size_t N>
121 Value operator[](const wchar_t (&static_str)[N]) {
122 return child(StringInitializer(static_str));
123 }
135 template <typename T,
136 std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>
138 return ValueElementRef(*this, index);
139 }
144 ValueElementRef at(std::size_t index) const {
145 return ValueElementRef(*this, index);
146 }
151 Value parent() const { return this->Field::parent(); }
152
159 const Value &
160 onChange(std::function<void WEBCFACE_CALL_FP(Value)> callback) const;
167 template <typename F, typename std::enable_if_t<std::is_invocable_v<F>,
168 std::nullptr_t> = nullptr>
169 const Value &onChange(F callback) const {
170 return onChange(
171 [callback = std::move(callback)](const auto &) { callback(); });
172 }
180 template <typename T>
181 [[deprecated]]
182 void appendListener(T &&callback) const {
183 onChange(std::forward<T>(callback));
184 }
185
193 const Value &set(double v) const;
199 const Value &set(std::vector<double> v) const;
209 template <typename R,
210 typename traits::ArrayLikeTrait<R>::ArrayLike = traits::TraitOk>
211 const Value &set(const R &range) const {
212 return set(traits::arrayLikeToVector(range));
213 }
218 const Value &resize(std::size_t size) const;
222 const Value &push_back(double v) const;
233 std::size_t size() const;
234
239 template <typename T>
240 const Value &operator=(T &&v) const {
241 this->set(std::forward<T>(v));
242 return *this;
243 }
252 const Value &operator=(std::vector<double> v) const {
253 this->set(std::move(v));
254 return *this;
255 }
256
262 const Value &request() const;
269 std::optional<double> tryGet() const;
277 std::optional<NumVector> tryGetVec() const;
282 double get() const { return tryGet().value_or(0); }
291 return tryGetVec().value_or(std::vector<double>{});
292 }
293 operator double() const { return get(); }
294 operator std::vector<double>() const { return getVec(); }
298 operator NumVector() const { return getVec(); }
299
308 bool exists() const;
313 [[deprecated]]
314 std::chrono::system_clock::time_point time() const;
315
320 const Value &free() const;
321
322 const Value &operator+=(double rhs) const {
323 this->set(this->get() + rhs);
324 return *this;
325 }
326 const Value &operator-=(double rhs) const {
327 this->set(this->get() - rhs);
328 return *this;
329 }
330 const Value &operator*=(double rhs) const {
331 this->set(this->get() * rhs);
332 return *this;
333 }
334 const Value &operator/=(double rhs) const {
335 this->set(this->get() / rhs);
336 return *this;
337 }
338 const Value &operator%=(std::int32_t rhs) const {
339 this->set(static_cast<std::int32_t>(this->get()) % rhs);
340 return *this;
341 }
342 const Value &operator<<=(std::int32_t rhs) const {
343 // todo: int64_tかuint64_tにしたほうがいいかもしれない
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 }
351 const Value &operator&=(std::int32_t rhs) const {
352 this->set(static_cast<std::int32_t>(this->get()) & rhs);
353 return *this;
354 }
355 const Value &operator|=(std::int32_t rhs) const {
356 this->set(static_cast<std::int32_t>(this->get()) | rhs);
357 return *this;
358 }
359 const Value &operator^=(std::int32_t rhs) const {
360 this->set(static_cast<std::int32_t>(this->get()) ^ rhs);
361 return *this;
362 }
367 const Value &operator++() const { // ++s
368 this->set(this->get() + 1);
369 return *this;
370 }
375 double operator++(int) const { // s++
376 auto v = this->get();
377 this->set(v + 1);
378 return v;
379 }
384 const Value &operator--() const { // --const s
385 this->set(this->get() - 1);
386 return *this;
387 }
392 double operator--(int) const { // s--
393 auto v = this->get();
394 this->set(v - 1);
395 return v;
396 }
397
406 template <typename T, typename std::enable_if_t<std::is_same_v<T, Value>,
407 std::nullptr_t> = nullptr>
408 bool operator==(const T &other) const {
409 return static_cast<Field>(*this) == static_cast<Field>(other);
410 }
411 template <typename T, typename std::enable_if_t<std::is_same_v<T, Value>,
412 std::nullptr_t> = nullptr>
413 bool operator!=(const T &other) const {
414 return static_cast<Field>(*this) == static_cast<Field>(other);
415 }
416 bool operator<(const Value &) const = delete;
417 bool operator<=(const Value &) const = delete;
418 bool operator>(const Value &) const = delete;
419 bool operator>=(const Value &) const = delete;
420};
421
428WEBCFACE_DLL std::ostream &WEBCFACE_CALL operator<<(std::ostream &os,
429 const Value &data);
430
shared_ptrで管理されているdoubleのvector
Definition num_vector.h:20
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:159
SharedString のpublicなコンストラクタインタフェース (入力専用)
Definition encoding.h:215
配列型の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:98
Value parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition value.h:151
const Value & operator/=(double rhs) const
Definition value.h:334
const Value & operator+=(double rhs) const
Definition value.h:322
const Value & operator|=(std::int32_t rhs) const
Definition value.h:355
bool operator==(const T &other) const
Valueの参照先を比較
Definition value.h:408
ValueElementRef operator[](T index) const
1次元配列型データの要素を参照する
Definition value.h:137
const Value & onChange(F callback) const
値が変化したときに呼び出されるコールバックを設定
Definition value.h:169
const Value & set(const R &range) const
配列型の値をセットする
Definition value.h:211
Value operator[](const char(&static_str)[N])
Definition value.h:113
const Value & operator%=(std::int32_t rhs) const
Definition value.h:338
const Value & operator>>=(std::int32_t rhs) const
Definition value.h:347
double operator--(int) const
1引いたものをsetし、足す前の値を返す
Definition value.h:392
bool operator<=(const Value &) const =delete
NumVector getVec() const
値をvectorで返す
Definition value.h:290
const Value & operator*=(double rhs) const
Definition value.h:330
Value child(int index) const
Definition value.h:88
bool operator!=(const T &other) const
Definition value.h:413
double operator++(int) const
1足したものをsetし、足す前の値を返す
Definition value.h:375
bool operator>=(const Value &) const =delete
void appendListener(T &&callback) const
Definition value.h:182
const Value & operator&=(std::int32_t rhs) const
Definition value.h:351
const Value & operator--() const
1引いたものをsetした後自身を返す
Definition value.h:384
const Value & operator-=(double rhs) const
Definition value.h:326
Value child(StringInitializer field) const
「(thisの名前).(追加の名前)」を新しい名前とするField
Definition value.h:80
Value()=default
bool operator>(const Value &) const =delete
Value operator[](const wchar_t(&static_str)[N])
Definition value.h:121
const Value & operator=(T &&v) const
数値または配列をセットする
Definition value.h:240
const Value & operator^=(std::int32_t rhs) const
Definition value.h:359
const Value & operator=(std::vector< double > v) const
vector型配列をセットする
Definition value.h:252
ValueElementRef at(std::size_t index) const
1次元配列型データの要素を参照する
Definition value.h:144
bool operator<(const Value &) const =delete
Value operator[](const char *field) const
Definition value.h:103
Value operator[](const wchar_t *field) const
Definition value.h:107
double get() const
値を返す
Definition value.h:282
Value(const Field &base, const SharedString &field)
Definition value.h:65
const Value & operator<<=(std::int32_t rhs) const
Definition value.h:342
const Value & operator++() const
1足したものをsetした後自身を返す
Definition value.h:367
std::ostream & operator<<(std::ostream &os, const Arg &arg)
Definition func_info.cc:98
const auto & get(const AxisAngle &aa)
Definition transform.h:231
ClientDataの参照とメンバ名とデータ名を持つクラス
Definition field.h:71
Field parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition field.cc:33
Field child(const SharedString &field) const
Definition field.cc:42
Definition array_like.h:57
#define WEBCFACE_DLL
Definition webcface-config.h:69
#define WEBCFACE_CALL
Definition webcface-config.h:106
#define WEBCFACE_NS_END
Definition webcface-config.h:118
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:117
#define WEBCFACE_CALL_FP
Definition webcface-config.h:107