forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomainBase.h
More file actions
265 lines (211 loc) · 7.96 KB
/
DomainBase.h
File metadata and controls
265 lines (211 loc) · 7.96 KB
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
// Copyright Sebastian Jeckel 2014.
// 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_DETAIL_DOMAINBASE_H_INCLUDED
#define REACT_DETAIL_DOMAINBASE_H_INCLUDED
#pragma once
#include <memory>
#include <utility>
#include "react/detail/Defs.h"
#include "react/detail/ReactiveBase.h"
#include "react/detail/IReactiveEngine.h"
#include "react/detail/graph/ContinuationNodes.h"
// Include all engines for convenience
#include "react/engine/PulsecountEngine.h"
#include "react/engine/SubtreeEngine.h"
#include "react/engine/ToposortEngine.h"
/*****************************************/ REACT_BEGIN /*****************************************/
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Forward declarations
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename D, typename S>
class Signal;
template <typename D, typename S>
class VarSignal;
template <typename D, typename E>
class Events;
template <typename D, typename E>
class EventSource;
enum class Token;
template <typename D>
class Observer;
template <typename D>
class ScopedObserver;
template <typename D>
class Reactor;
/******************************************/ REACT_END /******************************************/
/***************************************/ REACT_IMPL_BEGIN /**************************************/
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Common types & constants
///////////////////////////////////////////////////////////////////////////////////////////////////
// Domain modes
enum EDomainMode
{
sequential,
sequential_concurrent,
parallel,
parallel_concurrent
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// DomainBase
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename D, typename TPolicy>
class DomainBase
{
public:
using TurnT = typename TPolicy::Engine::TurnT;
DomainBase() = delete;
using Policy = TPolicy;
using Engine = REACT_IMPL::EngineInterface<D, typename Policy::Engine>;
///////////////////////////////////////////////////////////////////////////////////////////////
/// Domain traits
///////////////////////////////////////////////////////////////////////////////////////////////
static const bool uses_node_update_timer =
REACT_IMPL::NodeUpdateTimerEnabled<typename Policy::Engine>::value;
static const bool is_concurrent =
Policy::input_mode == REACT_IMPL::concurrent_input;
static const bool is_parallel =
Policy::propagation_mode == REACT_IMPL::parallel_propagation;
///////////////////////////////////////////////////////////////////////////////////////////////
/// Aliases for reactives of this domain
///////////////////////////////////////////////////////////////////////////////////////////////
template <typename S>
using SignalT = Signal<D,S>;
template <typename S>
using VarSignalT = VarSignal<D,S>;
template <typename E = Token>
using EventsT = Events<D,E>;
template <typename E = Token>
using EventSourceT = EventSource<D,E>;
using ObserverT = Observer<D>;
using ScopedObserverT = ScopedObserver<D>;
using ReactorT = Reactor<D>;
#ifdef REACT_ENABLE_LOGGING
///////////////////////////////////////////////////////////////////////////////////////////////
/// Log
///////////////////////////////////////////////////////////////////////////////////////////////
static EventLog& Log()
{
static EventLog instance;
return instance;
}
#endif //REACT_ENABLE_LOGGING
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ContinuationBase
///////////////////////////////////////////////////////////////////////////////////////////////////
template
<
typename D,
typename D2
>
class ContinuationBase : public MovableReactive<NodeBase<D>>
{
public:
ContinuationBase() = default;
template <typename T>
ContinuationBase(T&& t) :
ContinuationBase::MovableReactive( std::forward<T>(t) )
{}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ModeSelector - Translate domain mode to individual propagation and input modes
///////////////////////////////////////////////////////////////////////////////////////////////////
template <EDomainMode>
struct ModeSelector;
template <>
struct ModeSelector<sequential>
{
static const EInputMode input = consecutive_input;
static const EPropagationMode propagation = sequential_propagation;
};
template <>
struct ModeSelector<sequential_concurrent>
{
static const EInputMode input = concurrent_input;
static const EPropagationMode propagation = sequential_propagation;
};
template <>
struct ModeSelector<parallel>
{
static const EInputMode input = consecutive_input;
static const EPropagationMode propagation = parallel_propagation;
};
template <>
struct ModeSelector<parallel_concurrent>
{
static const EInputMode input = concurrent_input;
static const EPropagationMode propagation = parallel_propagation;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// GetDefaultEngine - Get default engine type for given propagation mode
///////////////////////////////////////////////////////////////////////////////////////////////////
template <EPropagationMode>
struct GetDefaultEngine;
template <>
struct GetDefaultEngine<sequential_propagation>
{
using Type = ToposortEngine<sequential_propagation>;
};
template <>
struct GetDefaultEngine<parallel_propagation>
{
using Type = SubtreeEngine<parallel_propagation>;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// EngineTypeBuilder - Instantiate the given template engine type with mode.
///////////////////////////////////////////////////////////////////////////////////////////////////
template <EPropagationMode>
struct DefaultEnginePlaceholder;
// Concrete engine template type
template
<
EPropagationMode mode,
template <EPropagationMode> class TTEngine
>
struct EngineTypeBuilder
{
using Type = TTEngine<mode>;
};
// Placeholder engine type - use default engine for given mode
template
<
EPropagationMode mode
>
struct EngineTypeBuilder<mode,DefaultEnginePlaceholder>
{
using Type = typename GetDefaultEngine<mode>::Type;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// DomainPolicy
///////////////////////////////////////////////////////////////////////////////////////////////////
template
<
EDomainMode mode,
template <EPropagationMode> class TTEngine = DefaultEnginePlaceholder
>
struct DomainPolicy
{
static const EInputMode input_mode = ModeSelector<mode>::input;
static const EPropagationMode propagation_mode = ModeSelector<mode>::propagation;
using Engine = typename EngineTypeBuilder<propagation_mode,TTEngine>::Type;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Ensure singletons are created immediately after domain declaration (TODO hax)
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename D>
class DomainInitializer
{
public:
DomainInitializer()
{
#ifdef REACT_ENABLE_LOGGING
D::Log();
#endif //REACT_ENABLE_LOGGING
D::Engine::Instance();
DomainSpecificInputManager<D>::Instance();
}
};
/****************************************/ REACT_IMPL_END /***************************************/
#endif // REACT_DETAIL_DOMAINBASE_H_INCLUDED