FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp.react/include/react/state.h at master · gitxyf/cpp.react · GitHub
gitxyf
/
cpp.react
Public
forked from
snakster/cpp.react
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp.react
/
include
/
react
/
state.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
299 lines (228 loc) · 9.81 KB
Breadcrumbs
cpp.react
/
include
/
react
/
state.h
Copy path
File metadata and controls
299 lines (228 loc) · 9.81 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
Copyright Sebastian Jeckel 2017.
//
Distributed under the Boost Software License, Version 1.0.
//
(See accompanying file LICENSE_1_0.txt or copy at
//
http://www.boost.org/LICENSE_1_0.txt)
#
ifndef
REACT_STATE_H_INCLUDED
#
define
REACT_STATE_H_INCLUDED
#
pragma
once
#
include
"
react/detail/defs.h
"
#
include
"
react/api.h
"
#
include
"
react/group.h
"
#
include
"
react/common/ptrcache.h
"
#
include
"
react/detail/state_nodes.h
"
#
include
<
memory
>
#
include
<
tuple
>
#
include
<
type_traits
>
#
include
<
utility
>
/*
***************************************
*/
REACT_BEGIN
/*
***************************************
*/
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ State
//
/////////////////////////////////////////////////////////////////////////////////////////////////
template
<
typename
S>
class
State
:
protected
REACT_IMPL
::StateInternals<S>
{
public:
//
Construct with explicit group
template
<
typename
F,
typename
T1
,
typename
... Ts>
static
State
Create
(
const
Group& group, F&& func,
const
State<
T1
>& dep1,
const
State<Ts>& ... deps)
{
return
CreateFuncNode
(group, std::forward<F>(func), dep1, deps ...); }
//
Construct with implicit group
template
<
typename
F,
typename
T1
,
typename
... Ts>
static
State
Create
(F&& func,
const
State<
T1
>& dep1,
const
State<Ts>& ... deps)
{
return
CreateFuncNode
(dep1.
GetGroup
(), std::forward<F>(func), dep1, deps ...); }
//
Construct with constant value
template
<
typename
T>
static
State
Create
(
const
Group& group, T&& init)
{
return
CreateFuncNode
(group, [value =
std::move
(init)] {
return
value; }); }
State
() =
default
;
State
(
const
State&) =
default
;
State&
operator
=(
const
State&) =
default
;
State
(State&&) =
default
;
State&
operator
=(State&&) =
default
;
auto
GetGroup
()
const
->
const
Group&
{
return
this
->
GetNodePtr
()->
GetGroup
(); }
auto
GetGroup
() -> Group&
{
return
this
->
GetNodePtr
()->
GetGroup
(); }
friend
bool
operator
==(
const
State<S>& a,
const
State<S>& b)
{
return
a.
GetNodePtr
() == b.
GetNodePtr
(); }
friend
bool
operator
!=(
const
State<S>& a,
const
State<S>& b)
{
return
!(a == b); }
friend
auto
GetInternals
(State<S>& s) -> REACT_IMPL::StateInternals<S>&
{
return
s; }
friend
auto
GetInternals
(
const
State<S>& s) ->
const
REACT_IMPL::StateInternals<S>&
{
return
s; }
protected:
State
(std::shared_ptr<
REACT_IMPL
::StateNode<S>>&& nodePtr) :
State::StateInternals
( std::move(nodePtr) )
{ }
private:
template
<
typename
F,
typename
T1
,
typename
... Ts>
static
auto
CreateFuncNode
(
const
Group& group, F&& func,
const
State<
T1
>& dep1,
const
State<Ts>& ... deps) -> decltype(
auto
)
{
using
REACT_IMPL
::StateFuncNode;
using
REACT_IMPL
::SameGroupOrLink;
return
std::make_shared<StateFuncNode<S,
typename
std::decay<F>::type,
T1
, Ts ...>>(
group, std::forward<F>(func),
SameGroupOrLink
(group, dep1),
SameGroupOrLink
(group, deps) ...);
}
template
<
typename
RET
,
typename
NODE
,
typename
...
ARGS
>
friend
RET
impl::CreateWrappedNode
(
ARGS
&& ... args);
};
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ StateVar
//
/////////////////////////////////////////////////////////////////////////////////////////////////
template
<
typename
S>
class
StateVar
:
public
State
<S>
{
public:
//
Construct with group + default
static
StateVar
Create
(
const
Group& group)
{
return
CreateVarNode
(group); }
//
Construct with group + value
template
<
typename
T>
static
StateVar
Create
(
const
Group& group, T&& value)
{
return
CreateVarNode
(group, std::forward<T>(value)); }
StateVar
() =
default
;
StateVar
(
const
StateVar&) =
default
;
StateVar&
operator
=(
const
StateVar&) =
default
;
StateVar
(StateVar&&) =
default
;
StateVar&
operator
=(StateVar&&) =
default
;
void
Set
(
const
S& newValue)
{
SetValue
(newValue); }
void
Set
(S&& newValue)
{
SetValue
(
std::move
(newValue)); }
template
<
typename
F>
void
Modify
(
const
F& func)
{
ModifyValue
(func); }
friend
bool
operator
==(
const
StateVar<S>& a, StateVar<S>& b)
{
return
a.
GetNodePtr
() == b.
GetNodePtr
(); }
friend
bool
operator
!=(
const
StateVar<S>& a, StateVar<S>& b)
{
return
!(a == b); }
S*
operator
->()
{
return
&
this
->
Value
(); }
protected:
StateVar
(std::shared_ptr<
REACT_IMPL
::StateNode<S>>&& nodePtr) :
StateVar::State
( std::move(nodePtr) )
{ }
private:
static
auto
CreateVarNode
(
const
Group& group) -> decltype(
auto
)
{
using
REACT_IMPL
::StateVarNode;
return
std::make_shared<StateVarNode<S>>(group);
}
template
<
typename
T>
static
auto
CreateVarNode
(
const
Group& group, T&& value) -> decltype(
auto
)
{
using
REACT_IMPL
::StateVarNode;
return
std::make_shared<StateVarNode<S>>(group, std::forward<T>(value));
}
template
<
typename
T>
void
SetValue
(T&& newValue)
{
using
REACT_IMPL
::NodeId;
using
VarNodeType =
REACT_IMPL
::StateVarNode<S>;
VarNodeType* castedPtr =
static_cast
<VarNodeType*>(
this
->
GetNodePtr
().
get
());
NodeId nodeId = castedPtr->
GetNodeId
();
auto
& graphPtr =
GetInternals
(
this
->
GetGroup
()).
GetGraphPtr
();
graphPtr->
PushInput
(nodeId, [castedPtr, &newValue] { castedPtr->
SetValue
(std::forward<T>(newValue)); });
}
template
<
typename
F>
void
ModifyValue
(
const
F& func)
{
using
REACT_IMPL
::NodeId;
using
VarNodeType =
REACT_IMPL
::StateVarNode<S>;
VarNodeType* castedPtr =
static_cast
<VarNodeType*>(
this
->
GetNodePtr
().
get
());
NodeId nodeId = castedPtr->
GetNodeId
();
auto
& graphPtr =
GetInternals
(
this
->
GetGroup
()).
GetGraphPtr
();
graphPtr->
PushInput
(nodeId, [castedPtr, &func] { castedPtr->
ModifyValue
(func); });
}
};
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ StateSlotBase
//
/////////////////////////////////////////////////////////////////////////////////////////////////
template
<
typename
S>
class
StateSlot
:
public
State
<S>
{
public:
//
Construct with explicit group
static
StateSlot
Create
(
const
Group& group,
const
State<S>& input)
{
return
CreateSlotNode
(group, input); }
//
Construct with implicit group
static
StateSlot
Create
(
const
State<S>& input)
{
return
CreateSlotNode
(input.
GetGroup
(), input); }
StateSlot
() =
default
;
StateSlot
(
const
StateSlot&) =
default
;
StateSlot&
operator
=(
const
StateSlot&) =
default
;
StateSlot
(StateSlot&&) =
default
;
StateSlot&
operator
=(StateSlot&&) =
default
;
void
Set
(
const
State<S>& newInput)
{
SetSlotInput
(newInput); }
protected:
StateSlot
(std::shared_ptr<
REACT_IMPL
::StateNode<S>>&& nodePtr) :
StateSlot::State
( std::move(nodePtr) )
{ }
private:
static
auto
CreateSlotNode
(
const
Group& group,
const
State<S>& input) -> decltype(
auto
)
{
using
REACT_IMPL
::StateSlotNode;
using
REACT_IMPL
::SameGroupOrLink;
return
std::make_shared<StateSlotNode<S>>(group,
SameGroupOrLink
(group, input));
}
void
SetSlotInput
(
const
State<S>& newInput)
{
using
REACT_IMPL
::NodeId;
using
REACT_IMPL
::StateSlotNode;
using
REACT_IMPL
::SameGroupOrLink;
auto
* castedPtr =
static_cast
<StateSlotNode<S>*>(
this
->
GetNodePtr
().
get
());
NodeId nodeId = castedPtr->
GetInputNodeId
();
auto
& graphPtr =
GetInternals
(
this
->
GetGroup
()).
GetGraphPtr
();
graphPtr->
PushInput
(nodeId, [
this
, castedPtr, &newInput] { castedPtr->
SetInput
(
SameGroupOrLink
(
GetGroup
(), newInput)); });
}
};
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ StateLink
//
/////////////////////////////////////////////////////////////////////////////////////////////////
template
<
typename
S>
class
StateLink
:
public
State
<S>
{
public:
//
Construct with group
static
StateLink
Create
(
const
Group& group,
const
State<S>& input)
{
return
GetOrCreateLinkNode
(group, input); }
StateLink
() =
default
;
StateLink
(
const
StateLink&) =
default
;
StateLink&
operator
=(
const
StateLink&) =
default
;
StateLink
(StateLink&&) =
default
;
StateLink&
operator
=(StateLink&&) =
default
;
protected:
StateLink
(std::shared_ptr<
REACT_IMPL
::StateNode<S>>&& nodePtr) :
StateLink::State
( std::move(nodePtr) )
{ }
private:
static
auto
GetOrCreateLinkNode
(
const
Group& group,
const
State<S>& input) -> decltype(
auto
)
{
using
REACT_IMPL
::StateLinkNode;
using
REACT_IMPL
::IReactNode;
using
REACT_IMPL
::ReactGraph;
IReactNode* k =
GetInternals
(input).
GetNodePtr
().
get
();
ReactGraph::LinkCache& linkCache =
GetInternals
(group).
GetGraphPtr
()->
GetLinkCache
();
std::shared_ptr<IReactNode> nodePtr = linkCache.
LookupOrCreate
(k, [&]
{
auto
nodePtr = std::make_shared<StateLinkNode<S>>(group, input);
nodePtr->
SetWeakSelfPtr
(std::weak_ptr<StateLinkNode<S>>{ nodePtr });
return
std::static_pointer_cast<IReactNode>(nodePtr);
});
return
std::static_pointer_cast<StateLinkNode<S>>(nodePtr);
}
};
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ CreateRef
//
/////////////////////////////////////////////////////////////////////////////////////////////////
template
<
typename
S>
auto
CreateRef
(
const
State<S>& state) -> State<Ref<S>>
{
using
REACT_IMPL
::StateRefNode;
using
REACT_IMPL
::CreateWrappedNode;
return
CreateWrappedNode<State<Ref<S>>, StateRefNode<S>>(state.
GetGroup
(), state);
}
/*
****************************************
*/
REACT_END
/*
****************************************
*/
#
endif
//
REACT_STATE_H_INCLUDED
Back
|
FazBrowse Home
|
New Git URL