Skip to content

Commit 6eb759c

Browse files
committed
Refactoring of object creation: -Ctor +static Create.
Cleanup. Object state.
1 parent 55fbe9b commit 6eb759c

15 files changed

Lines changed: 594 additions & 371 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ include_directories ("${PROJECT_SOURCE_DIR}/include")
1313

1414
### CppReact
1515
add_library(CppReact
16-
src/engine/PulsecountEngine.cpp
17-
src/engine/SubtreeEngine.cpp
18-
src/engine/ToposortEngine.cpp
19-
src/logging/EventLog.cpp
20-
src/logging/EventRecords.cpp)
16+
src/detail/graph_impl.cpp)
2117

2218
target_link_libraries(CppReact tbb)
2319

‎benchmarks/src/Main.cpp‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,18 @@ int main()
222222

223223
}
224224

225+
struct Data
226+
{
227+
State<int> a;
228+
State<int> b;
229+
230+
State<Data*> (a, b);
231+
};
232+
225233
#endif
226234

227235
#include "react/common/expected.h"
228-
#include "react/signal.h"
236+
#include "react/state.h"
229237
#include "react/event.h"
230238
#include "react/algorithm.h"
231239
#include <iostream>

‎include/react/api.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#pragma once
1111

12+
#include <vector>
13+
1214
#include "react/detail/defs.h"
1315
#include "react/common/utility.h"
1416

@@ -35,6 +37,14 @@ REACT_DEFINE_BITMASK_OPERATORS(TransactionFlags)
3537

3638
enum class Token { value };
3739

40+
enum class InPlaceTag
41+
{
42+
value = 1
43+
};
44+
45+
static constexpr InPlaceTag in_place = InPlaceTag::value;
46+
47+
3848
///////////////////////////////////////////////////////////////////////////////////////////////////
3949
/// API types
4050
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -55,6 +65,13 @@ class StateSlot;
5565
template <typename S>
5666
class StateLink;
5767

68+
// Object state
69+
template <typename S>
70+
class ObjectContext;
71+
72+
template <typename T>
73+
class ObjectState;
74+
5875
// Event
5976
enum class Token;
6077

@@ -67,6 +84,12 @@ class EventSource;
6784
template <typename E>
6885
class EventSlot;
6986

87+
template <typename E = Token>
88+
using EventValueList = std::vector<E>;
89+
90+
template <typename E>
91+
using EventValueSink = std::back_insert_iterator<std::vector<E>>;
92+
7093
// Observer
7194
class Observer;
7295

‎include/react/common/syncpoint.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <condition_variable>
1717
#include <iterator>
1818
#include <mutex>
19+
#include <vector>
1920

2021

2122
/*****************************************/ REACT_BEGIN /*****************************************/
@@ -29,8 +30,6 @@ class SyncPoint
2930
class Dependency;
3031

3132
private:
32-
33-
3433
class ISyncTarget
3534
{
3635
public:

‎include/react/detail/event_nodes.h‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@
2727
///////////////////////////////////////////////////////////////////////////////////////////////////
2828
/// Iterators for event processing
2929
///////////////////////////////////////////////////////////////////////////////////////////////////
30-
template <typename E = Token>
31-
using EventValueList = std::vector<E>;
32-
33-
template <typename E>
34-
using EventValueSink = std::back_insert_iterator<std::vector<E>>;
3530

3631
/******************************************/ REACT_END /******************************************/
3732

@@ -493,6 +488,8 @@ template <typename E>
493488
class EventInternals
494489
{
495490
public:
491+
EventInternals() = default;
492+
496493
EventInternals(const EventInternals&) = default;
497494
EventInternals& operator=(const EventInternals&) = default;
498495

‎include/react/detail/state_nodes.h‎

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,13 @@
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
///////////////////////////////////////////////////////////////////////////////////////////////////
3226
template <typename S>
3327
class StateNode : public NodeBase
3428
{
3529
public:
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
///////////////////////////////////////////////////////////////////////////////////////////////////
158152
template <typename S, typename F, typename ... TDeps>
159153
class 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>
352404
class StateInternals
353405
{
354406
public:
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

Comments
 (0)