WebCFace 2.5.2
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
func_internal.h
Go to the documentation of this file.
1#pragma once
2#include <unordered_map>
3#include <mutex>
5#include "webcface/func.h"
6
8namespace message {
9struct FuncInfo;
10struct Call;
11} // namespace message
12
13namespace internal {
18struct PromiseData : public std::enable_shared_from_this<PromiseData> {
23 std::mutex m;
24 std::condition_variable cond;
25
26 std::function<void(Promise)> reach_event;
27 std::function<void(Promise)> finish_event;
28 bool reach_event_done = false;
29 bool finish_event_done = false;
30
31 bool reached = false;
32 bool found = false;
33 bool finished = false;
34 bool is_error = false;
37
38 std::promise<bool> started_p;
39 std::promise<ValAdaptor> result_p;
40 std::shared_future<bool> started_f;
41 std::shared_future<ValAdaptor> result_f;
42
44 std::size_t caller_id;
45 const std::vector<ValAdaptor> args_;
46 std::variant<int, std::vector<wcfMultiVal>, std::vector<wcfMultiValW>>
48
52 void callReachEvent();
56 void callFinishEvent();
57
61 explicit PromiseData(const Field &base, std::vector<ValAdaptor> &&args,
62 std::size_t caller_id = 0)
63 : reach_event(), finish_event(), started_p(), result_p(),
64 started_f(started_p.get_future().share()),
65 result_f(result_p.get_future().share()), base(base),
66 caller_id(caller_id), args_(std::move(args)) {}
67
72 template <std::size_t v_index, typename CVal>
73 std::vector<CVal> &initCArgs();
74
75 Promise getter();
76 CallHandle setter();
77
78 std::size_t callerId() const { return caller_id; }
79};
80
89 std::mutex mtx;
90 std::unordered_map<std::size_t, std::shared_ptr<PromiseData>> promises;
91 std::size_t next_caller_id = 0;
92
93 public:
98 std::shared_ptr<PromiseData> addResult(const Field &base) {
99 std::lock_guard lock(mtx);
100 std::size_t caller_id = next_caller_id++;
101 auto state = std::make_shared<PromiseData>(
102 base, std::vector<ValAdaptor>{}, caller_id);
103 promises.emplace(caller_id, state);
104 return state;
105 }
110 std::shared_ptr<PromiseData> getResult(std::size_t caller_id) {
111 std::lock_guard lock(mtx);
112 auto it = promises.find(caller_id);
113 if (it != promises.end()) {
114 return it->second;
115 } else {
116 throw std::out_of_range("caller id not found");
117 }
118 }
123 void removeResult(std::size_t caller_id) {
124 std::lock_guard lock(mtx);
125 auto it = promises.find(caller_id);
126 if (it != promises.end()) {
127 promises.erase(it);
128 } else {
129 throw std::out_of_range("caller id not found");
130 }
131 }
132};
133
142struct FuncInfo : public Field {
144 std::optional<std::vector<Arg>> args;
145 std::function<Func::FuncType> func_impl;
146
153 void run(const CallHandle &handle) {
154 if (func_impl) {
155 func_impl(handle);
156 } else {
157 throw std::runtime_error("func_impl is null");
158 }
159 }
160
166 void run(webcface::message::Call &&call);
167
168 FuncInfo() : Field(), return_type(ValType::none_), args(), func_impl() {}
169 FuncInfo(const Field &base, ValType return_type, std::optional<std::vector<Arg>> &&args,
170 std::function<Func::FuncType> &&func_impl)
171 : Field(base), return_type(return_type), args(std::move(args)),
172 func_impl(std::move(func_impl)) {}
173 FuncInfo(const message::FuncInfo &m);
174 message::FuncInfo toMessage(const SharedString &field);
175};
176
177} // namespace internal
呼び出された関数の引数の取得と戻り値のセットをするインタフェース
Definition func_result.h:323
非同期で実行した関数の実行結果を取得するインタフェース。
Definition func_result.h:50
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:69
数値、文字列などの値を相互変換するクラス
Definition val_adaptor.h:87
PromiseDataのリストを保持する。
Definition func_internal.h:88
std::shared_ptr< PromiseData > addResult(const Field &base)
新しいcaller_idを振って新しいAsyncFuncStateを生成しそれを返す
Definition func_internal.h:98
std::shared_ptr< PromiseData > getResult(std::size_t caller_id)
AsyncFuncStateを取得
Definition func_internal.h:110
void removeResult(std::size_t caller_id)
resultを設定し終わったstateを削除
Definition func_internal.h:123
ValType
引数や戻り値の型を表すenum
Definition val_adaptor.h:21
ClientDataの参照とメンバ名とデータ名を持つクラス
Definition field.h:70
関数1つの情報を表す。関数の実体も持つ
Definition func_internal.h:142
std::function< Func::FuncType > func_impl
Definition func_internal.h:145
std::optional< std::vector< Arg > > args
Definition func_internal.h:144
FuncInfo()
Definition func_internal.h:168
ValType return_type
Definition func_internal.h:143
FuncInfo(const Field &base, ValType return_type, std::optional< std::vector< Arg > > &&args, std::function< Func::FuncType > &&func_impl)
Definition func_internal.h:169
void run(const CallHandle &handle)
func_implをこのスレッドで実行
Definition func_internal.h:153
PromiseとCallHandleのデータを保持するクラス
Definition func_internal.h:18
std::condition_variable cond
Definition func_internal.h:24
std::size_t callerId() const
Definition func_internal.h:78
std::size_t caller_id
Definition func_internal.h:44
std::function< void(Promise)> reach_event
Definition func_internal.h:26
ValAdaptor response
Definition func_internal.h:35
std::shared_future< bool > started_f
Definition func_internal.h:40
Field base
Definition func_internal.h:43
ValAdaptor rejection
Definition func_internal.h:36
std::function< void(Promise)> finish_event
Definition func_internal.h:27
std::shared_future< ValAdaptor > result_f
Definition func_internal.h:41
const std::vector< ValAdaptor > args_
Definition func_internal.h:45
PromiseData(const Field &base, std::vector< ValAdaptor > &&args, std::size_t caller_id=0)
Definition func_internal.h:61
std::mutex m
Definition func_internal.h:23
std::promise< ValAdaptor > result_p
Definition func_internal.h:39
std::variant< int, std::vector< wcfMultiVal >, std::vector< wcfMultiValW > > c_args_
Definition func_internal.h:47
std::promise< bool > started_p
Definition func_internal.h:38
関数呼び出し (client(caller)->server->client(receiver))
Definition func.h:23
Definition func.h:107
#define WEBCFACE_NS_END
Definition webcface-config.h:104
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:103