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"
8#ifdef WEBCFACE_MESON
9#include "webcface-config.h"
10#else
11#include "webcface/common/webcface-config.h"
12#endif
13
15
21 std::size_t index;
22
23 public:
24 ValueElementRef(const Field &base, std::size_t index)
25 : Field(base), index(index) {}
26
34 const ValueElementRef &set(double v) const;
35 const ValueElementRef &operator=(double v) const { return set(v); }
36
42 std::optional<double> tryGet() const;
49 double get() const { return tryGet().value_or(0); }
50};
51
59class WEBCFACE_DLL Value : protected Field {
60 public:
61 Value() = default;
62 Value(const Field &base);
63 Value(const Field &base, const SharedString &field)
64 : Value(Field{base, field}) {}
65
66 using Field::lastName;
67 using Field::member;
68 using Field::name;
69 using Field::nameW;
74 Value child(std::string_view field) const {
75 return this->Field::child(field);
76 }
81 Value child(std::wstring_view field) const {
82 return this->Field::child(field);
83 }
88 [[deprecated]]
89 Value child(int index) const {
90 return this->Field::child(std::to_string(index));
91 }
96 Value operator[](std::string_view field) const { return child(field); }
101 Value operator[](std::wstring_view field) const { return child(field); }
106 Value operator[](const char *field) const { return child(field); }
110 Value operator[](const wchar_t *field) const { return child(field); }
122 template <typename T,
123 std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>
125 return ValueElementRef(*this, index);
126 }
131 ValueElementRef at(std::size_t index) const {
132 return ValueElementRef(*this, index);
133 }
138 Value parent() const { return this->Field::parent(); }
139
146 const Value &
147 onChange(std::function<void WEBCFACE_CALL_FP(Value)> callback) const;
154 template <typename F, typename std::enable_if_t<std::is_invocable_v<F>,
155 std::nullptr_t> = nullptr>
156 const Value &onChange(F callback) const {
157 return onChange(
158 [callback = std::move(callback)](const auto &) { callback(); });
159 }
167 template <typename T>
168 [[deprecated]] void appendListener(T &&callback) const {
169 onChange(std::forward<T>(callback));
170 }
171
179 const Value &set(double v) const;
185 const Value &set(std::vector<double> v) const;
195 template <typename R,
196 typename traits::ArrayLikeTrait<R>::ArrayLike = traits::TraitOk>
197 const Value &set(const R &range) const {
198 return set(traits::arrayLikeToVector(range));
199 }
204 const Value &resize(std::size_t size) const;
208 const Value &push_back(double v) const;
218 std::size_t size() const;
219
224 template <typename T>
225 const Value &operator=(T &&v) const {
226 this->set(std::forward<T>(v));
227 return *this;
228 }
237 const Value &operator=(std::vector<double> v) const {
238 this->set(std::move(v));
239 return *this;
240 }
241
247 const Value &request() const;
254 std::optional<double> tryGet() const;
259 std::optional<std::vector<double>> tryGetVec() const;
264 double get() const { return tryGet().value_or(0); }
269 std::vector<double> getVec() const {
270 return tryGetVec().value_or(std::vector<double>{});
271 }
272 operator double() const { return get(); }
273 operator std::vector<double>() const { return getVec(); }
274
283 bool exists() const;
288 [[deprecated]] std::chrono::system_clock::time_point time() const;
289
294 const Value &free() const;
295
296 const Value &operator+=(double rhs) const {
297 this->set(this->get() + rhs);
298 return *this;
299 }
300 const Value &operator-=(double rhs) const {
301 this->set(this->get() - rhs);
302 return *this;
303 }
304 const Value &operator*=(double rhs) const {
305 this->set(this->get() * rhs);
306 return *this;
307 }
308 const Value &operator/=(double rhs) const {
309 this->set(this->get() / rhs);
310 return *this;
311 }
312 const Value &operator%=(std::int32_t rhs) const {
313 this->set(static_cast<std::int32_t>(this->get()) % rhs);
314 return *this;
315 }
316 const Value &operator<<=(std::int32_t rhs) const {
317 // todo: int64_tかuint64_tにしたほうがいいかもしれない
318 this->set(static_cast<std::int32_t>(this->get()) << rhs);
319 return *this;
320 }
321 const Value &operator>>=(std::int32_t rhs) const {
322 this->set(static_cast<std::int32_t>(this->get()) >> rhs);
323 return *this;
324 }
325 const Value &operator&=(std::int32_t rhs) const {
326 this->set(static_cast<std::int32_t>(this->get()) & rhs);
327 return *this;
328 }
329 const Value &operator|=(std::int32_t rhs) const {
330 this->set(static_cast<std::int32_t>(this->get()) | rhs);
331 return *this;
332 }
333 const Value &operator^=(std::int32_t rhs) const {
334 this->set(static_cast<std::int32_t>(this->get()) ^ rhs);
335 return *this;
336 }
341 const Value &operator++() const { // ++s
342 this->set(this->get() + 1);
343 return *this;
344 }
349 double operator++(int) const { // s++
350 auto v = this->get();
351 this->set(v + 1);
352 return v;
353 }
358 const Value &operator--() const { // --const s
359 this->set(this->get() - 1);
360 return *this;
361 }
366 double operator--(int) const { // s--
367 auto v = this->get();
368 this->set(v - 1);
369 return v;
370 }
371
380 template <typename T, typename std::enable_if_t<std::is_same_v<T, Value>,
381 std::nullptr_t> = nullptr>
382 bool operator==(const T &other) const {
383 return static_cast<Field>(*this) == static_cast<Field>(other);
384 }
385 template <typename T, typename std::enable_if_t<std::is_same_v<T, Value>,
386 std::nullptr_t> = nullptr>
387 bool operator!=(const T &other) const {
388 return static_cast<Field>(*this) == static_cast<Field>(other);
389 }
390 bool operator<(const Value &) const = delete;
391 bool operator<=(const Value &) const = delete;
392 bool operator>(const Value &) const = delete;
393 bool operator>=(const Value &) const = delete;
394};
395
402WEBCFACE_DLL std::ostream &WEBCFACE_CALL operator<<(std::ostream &os,
403 const Value &data);
404
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:67
配列型のValueデータの一部の要素を指定するクラス
Definition value.h:20
ValueElementRef(const Field &base, std::size_t index)
Definition value.h:24
const ValueElementRef & operator=(double v) const
Definition value.h:35
double get() const
値があればその要素を返す
Definition value.h:49
実数値またはその配列の送受信データを表すクラス
Definition value.h:59
Value parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition value.h:138
const Value & operator/=(double rhs) const
Definition value.h:308
const Value & operator+=(double rhs) const
Definition value.h:296
const Value & operator|=(std::int32_t rhs) const
Definition value.h:329
bool operator==(const T &other) const
Valueの参照先を比較
Definition value.h:382
ValueElementRef operator[](T index) const
1次元配列型データの要素を参照する
Definition value.h:124
const Value & onChange(F callback) const
値が変化したときに呼び出されるコールバックを設定
Definition value.h:156
const Value & set(const R &range) const
配列型の値をセットする
Definition value.h:197
std::vector< double > getVec() const
値をvectorで返す
Definition value.h:269
const Value & operator%=(std::int32_t rhs) const
Definition value.h:312
const Value & operator>>=(std::int32_t rhs) const
Definition value.h:321
double operator--(int) const
1引いたものをsetし、足す前の値を返す
Definition value.h:366
Value operator[](std::string_view field) const
Definition value.h:96
Value child(std::string_view field) const
「(thisの名前).(追加の名前)」を新しい名前とするField
Definition value.h:74
bool operator<=(const Value &) const =delete
const Value & operator*=(double rhs) const
Definition value.h:304
Value child(int index) const
Definition value.h:89
bool operator!=(const T &other) const
Definition value.h:387
double operator++(int) const
1足したものをsetし、足す前の値を返す
Definition value.h:349
bool operator>=(const Value &) const =delete
Value child(std::wstring_view field) const
「(thisの名前).(追加の名前)」を新しい名前とするField (wstring)
Definition value.h:81
void appendListener(T &&callback) const
Definition value.h:168
const Value & operator&=(std::int32_t rhs) const
Definition value.h:325
const Value & operator--() const
1引いたものをsetした後自身を返す
Definition value.h:358
const Value & operator-=(double rhs) const
Definition value.h:300
Value()=default
bool operator>(const Value &) const =delete
const Value & operator=(T &&v) const
数値または配列をセットする
Definition value.h:225
const Value & operator^=(std::int32_t rhs) const
Definition value.h:333
Value operator[](std::wstring_view field) const
Definition value.h:101
const Value & operator=(std::vector< double > v) const
vector型配列をセットする
Definition value.h:237
ValueElementRef at(std::size_t index) const
1次元配列型データの要素を参照する
Definition value.h:131
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
double get() const
値を返す
Definition value.h:264
Value(const Field &base, const SharedString &field)
Definition value.h:63
const Value & operator<<=(std::int32_t rhs) const
Definition value.h:316
const Value & operator++() const
1足したものをsetした後自身を返す
Definition value.h:341
std::ostream & operator<<(std::ostream &os, const Arg &arg)
Definition func_info.cc:100
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:58
#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