2020
2121/* **************************************/ REACT_IMPL_BEGIN /* *************************************/
2222
23- // /////////////////////////////////////////////////////////////////////////////////////////////////
24- // / Forward declarations
25- // /////////////////////////////////////////////////////////////////////////////////////////////////
26- template <typename L, typename R>
27- bool Equals (const L& lhs, const R& rhs);
28-
2923// /////////////////////////////////////////////////////////////////////////////////////////////////
3024// / StateNode
3125// /////////////////////////////////////////////////////////////////////////////////////////////////
3226template <typename S>
3327class StateNode : public NodeBase
3428{
3529public:
36- StateNode (StateNode&&) = default ;
37- StateNode& operator =(StateNode&&) = default ;
38-
39- StateNode (const StateNode&) = delete ;
40- StateNode& operator =(const StateNode&) = delete ;
41-
4230 explicit StateNode (const Group& group) :
4331 StateNode::NodeBase( group ),
4432 value_( )
@@ -50,6 +38,12 @@ class StateNode : public NodeBase
5038 value_ ( std::forward<T>(value) )
5139 { }
5240
41+ template <typename ... Ts>
42+ StateNode (InPlaceTag, const Group& group, Ts&& ... args) :
43+ StateNode::NodeBase ( group ),
44+ value_ ( std::forward<Ts>(args) ... )
45+ { }
46+
5347 S& Value ()
5448 { return value_; }
5549
@@ -153,7 +147,7 @@ class StateVarNode : public StateNode<S>
153147};
154148
155149// /////////////////////////////////////////////////////////////////////////////////////////////////
156- // / StateOpNode
150+ // / StateFuncNode
157151// /////////////////////////////////////////////////////////////////////////////////////////////////
158152template <typename S, typename F, typename ... TDeps>
159153class StateFuncNode : public StateNode <S>
@@ -344,6 +338,64 @@ class StateLinkNode : public StateNode<S>
344338 VirtualOutputNode linkOutput_;
345339};
346340
341+ // /////////////////////////////////////////////////////////////////////////////////////////////////
342+ // / ObjectStateNode
343+ // /////////////////////////////////////////////////////////////////////////////////////////////////
344+ template <typename S>
345+ class ObjectStateNode : public StateNode <ObjectContext<S>>
346+ {
347+ public:
348+ ObjectStateNode (const Group& group, S&& obj, const std::initializer_list<NodeId>& memberIds) :
349+ ObjectStateNode::StateNode ( group, std::move(obj) )
350+ {
351+ this ->RegisterMe ();
352+
353+ if (memberIds.size () == 0 )
354+ {
355+ apply ([this ] (const auto & ... members)
356+ {
357+ REACT_EXPAND_PACK (memberIds_.push_back (GetInternals (members).GetNodeId ()));
358+ REACT_EXPAND_PACK (this ->AttachToMe (GetInternals (members).GetNodeId ()));
359+ }, this ->Value ().GetObject ().GetReactiveMembers ());
360+ }
361+ else
362+ {
363+ memberIds_.reserve (memberIds.size ());
364+
365+ for (NodeId id : memberIds)
366+ {
367+ memberIds_.push_back (id);
368+ this ->AttachToMe (id);
369+ }
370+ }
371+ }
372+
373+ template <typename ... Us>
374+ ObjectStateNode (InPlaceTag, const Group& group, Us&& ... args) :
375+ ObjectStateNode::StateNode ( in_place, group, group, std::forward<Us>(args) ... )
376+ {
377+ this ->RegisterMe ();
378+
379+ apply ([this ] (const auto & ... members)
380+ { REACT_EXPAND_PACK (memberIds_.push_back (GetInternals (members).GetNodeId ())); }, this ->Value ().GetObject ().GetReactiveMembers ());
381+ }
382+
383+ ~ObjectStateNode ()
384+ {
385+ for (NodeId id : memberIds_)
386+ this ->DetachFromMe (id);
387+
388+ this ->UnregisterMe ();
389+ }
390+
391+ virtual UpdateResult Update (TurnId turnId) noexcept override
392+ {
393+ return UpdateResult::changed;
394+ }
395+
396+ private:
397+ std::vector<NodeId> memberIds_;
398+ };
347399
348400// /////////////////////////////////////////////////////////////////////////////////////////////////
349401// / StateInternals
@@ -352,6 +404,8 @@ template <typename S>
352404class StateInternals
353405{
354406public:
407+ StateInternals () = default ;
408+
355409 StateInternals (const StateInternals&) = default ;
356410 StateInternals& operator =(const StateInternals&) = default ;
357411
@@ -381,6 +435,18 @@ class StateInternals
381435 std::shared_ptr<StateNode<S>> nodePtr_;
382436};
383437
438+ // /////////////////////////////////////////////////////////////////////////////////////////////////
439+ // / SameGroupOrLink
440+ // /////////////////////////////////////////////////////////////////////////////////////////////////
441+ template <typename S>
442+ static State<S> SameGroupOrLink (const Group& targetGroup, const State<S>& dep)
443+ {
444+ if (dep.GetGroup () == targetGroup)
445+ return dep;
446+ else
447+ return StateLink<S>::Create (targetGroup, dep);
448+ }
449+
384450/* ***************************************/ REACT_IMPL_END /* **************************************/
385451
386452#endif // REACT_DETAIL_GRAPH_STATENODES_H_INCLUDED
0 commit comments