Skip to content

Commit a8f06ec

Browse files
committed
Added compile time dependency count for FunctionOpNode.
1 parent 826a708 commit a8f06ec

1 file changed

Lines changed: 32 additions & 89 deletions

File tree

include/react/graph/SignalNodes.h

Lines changed: 32 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -135,94 +135,6 @@ class VarNode :
135135
S newValue_;
136136
};
137137

138-
///////////////////////////////////////////////////////////////////////////////////////////////////
139-
/// FunctionNode
140-
///////////////////////////////////////////////////////////////////////////////////////////////////
141-
template
142-
<
143-
typename D,
144-
typename S,
145-
typename TFunc,
146-
typename ... TArgs
147-
>
148-
class FunctionNode : public SignalNode<D,S>
149-
{
150-
public:
151-
template <typename F>
152-
FunctionNode(F&& func, const SignalNodePtr<D,TArgs>& ... args) :
153-
SignalNode<D, S>(),
154-
deps_{ args ... },
155-
func_{ std::forward<F>(func) }
156-
{
157-
value_ = func_(args->ValueRef() ...);
158-
159-
Engine::OnNodeCreate(*this);
160-
161-
REACT_EXPAND_PACK(Engine::OnNodeAttach(*this, *args));
162-
}
163-
164-
~FunctionNode()
165-
{
166-
apply
167-
(
168-
[this] (const SignalNodePtr<D,TArgs>& ... args)
169-
{
170-
REACT_EXPAND_PACK(Engine::OnNodeDetach(*this, *args));
171-
},
172-
deps_
173-
);
174-
175-
Engine::OnNodeDestroy(*this);
176-
}
177-
178-
virtual const char* GetNodeType() const override { return "FunctionNode"; }
179-
180-
virtual void Tick(void* turnPtr) override
181-
{
182-
using TurnT = typename D::Engine::TurnT;
183-
TurnT& turn = *static_cast<TurnT*>(turnPtr);
184-
185-
REACT_LOG(D::Log().template Append<NodeEvaluateBeginEvent>(
186-
GetObjectId(*this), turn.Id()));
187-
188-
S newValue = evaluate();
189-
if (mergedOps_ != nullptr)
190-
newValue = applyMergedOps(std::move(newValue));
191-
192-
REACT_LOG(D::Log().template Append<NodeEvaluateEndEvent>(
193-
GetObjectId(*this), turn.Id()));
194-
195-
if (! impl::Equals(value_, newValue))
196-
{
197-
value_ = std::move(newValue);
198-
Engine::OnNodePulse(*this, turn);
199-
return;
200-
}
201-
else
202-
{
203-
Engine::OnNodeIdlePulse(*this, turn);
204-
return;
205-
}
206-
}
207-
208-
virtual int DependencyCount() const override { return sizeof ... (TArgs); }
209-
210-
private:
211-
std::tuple<SignalNodePtr<D,TArgs> ...> deps_;
212-
TFunc func_;
213-
214-
S evaluate() const
215-
{
216-
return apply(func_, apply(unpackValues, deps_));
217-
}
218-
219-
static inline auto unpackValues(const SignalNodePtr<D,TArgs>& ... args)
220-
-> decltype(std::tie(args->ValueRef() ...))
221-
{
222-
return std::tie(args->ValueRef() ...);
223-
}
224-
};
225-
226138
///////////////////////////////////////////////////////////////////////////////////////////////////
227139
/// FunctionOp
228140
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -234,6 +146,8 @@ template
234146
>
235147
class FunctionOp
236148
{
149+
private:
150+
237151
public:
238152
template <typename FIn, typename ... TArgsIn>
239153
FunctionOp(FIn&& func, TArgsIn&& ... args) :
@@ -288,6 +202,8 @@ class FunctionOp
288202
{
289203
return arg.Detach<D>(node);
290204
}
205+
206+
static const int count_value = T::dependency_count;
291207
};
292208

293209
template <typename T>
@@ -310,6 +226,8 @@ class FunctionOp
310226
{
311227
D::Engine::OnNodeDetach(node, *depPtr);
312228
}
229+
230+
static const int count_value = 1;
313231
};
314232

315233
struct EvalFunctor
@@ -353,8 +271,30 @@ class FunctionOp
353271
TNode& MyNode;
354272
};
355273

274+
private:
356275
std::tuple<TArgs ...> deps_;
357276
F func_;
277+
278+
// Dependency counting
279+
private:
280+
template <int N, typename... Args>
281+
struct DepCounter;
282+
283+
template <int N, typename First, typename... Args>
284+
struct DepCounter<N, First, Args...>
285+
{
286+
static int const Value =
287+
Helper<std::decay<First>::type>::count_value + DepCounter<N-1,Args...>::Value;
288+
};
289+
290+
template <>
291+
struct DepCounter<0>
292+
{
293+
static int const Value = 0;
294+
};
295+
296+
public:
297+
static const int dependency_count = DepCounter<sizeof...(TArgs), TArgs...>::Value;
358298
};
359299

360300
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -415,7 +355,10 @@ class OpSignalNode : public SignalNode<D,S>
415355
}
416356
}
417357

418-
virtual int DependencyCount() const override { return 1; }
358+
virtual int DependencyCount() const override
359+
{
360+
return TOp::dependency_count;
361+
}
419362

420363
TOp StealOp()
421364
{

0 commit comments

Comments
 (0)