Skip to content

Commit 20f0149

Browse files
committed
done with link cache
1 parent d24b490 commit 20f0149

6 files changed

Lines changed: 79 additions & 30 deletions

File tree

examples/src/Main.cpp

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ template <typename T> T IterFunc2(EventRange<T> evts, T v, T a1, T a2)
159159
return v + 1;
160160
}
161161

162-
int main()
162+
int main1()
163163
{
164164
Group group;
165165

@@ -232,10 +232,13 @@ int main()
232232

233233
EventSlot<int> slot{ group };
234234

235-
Observer eventObs{ PrintEvents<int>, anyButton };
235+
Observer eventObs{ PrintEvents<int>, slot };
236+
237+
slot.Add(s1);
238+
slot.Add(s2);
236239

237-
slot.AddInput(s1);
238-
slot.AddInput(s2);
240+
slot.Remove(s1);
241+
slot.RemoveAll();
239242
}
240243

241244
// Links
@@ -327,7 +330,44 @@ int main()
327330
}
328331

329332

333+
int main()
334+
{
335+
Group group;
336+
337+
Group extGroup;
338+
339+
// Dynamic events
340+
EventSource<int> s1{ group };
341+
EventSource<int> s2{ group };
342+
EventSource<int> s3{ extGroup };
343+
//EventSource<int> s4{ extGroup };
344+
345+
EventSlot<int> slot{ group };
346+
347+
Observer eventObs{ PrintEvents<int>, slot };
348+
//Observer extObs{ PrintEvents<int>, link1 };
349+
350+
//slot.Add(s1);
351+
//slot.Add(s2);
330352

353+
group.DoTransaction([&]
354+
{
355+
s1.Emit(1);
356+
s2.Emit(2);
357+
358+
359+
slot.Add(s3);
360+
slot.Add(s3);
361+
slot.Add(s3);
362+
slot.Add(s3);
363+
});
364+
365+
s3.Emit(3);
366+
367+
std::this_thread::sleep_for(std::chrono::seconds(5));
368+
369+
return 0;
370+
}
331371

332372

333373

include/react/Event.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ class EventSlot : public Event<E>
271271
NodeId nodeId = castedPtr->GetInputNodeId();
272272
auto& graphPtr = GetInternals(this->GetGroup()).GetGraphPtr();
273273

274-
graphPtr->AddInput(nodeId, [castedPtr, &input] { castedPtr->AddInput(input); });
274+
graphPtr->AddInput(nodeId, [this, castedPtr, &input] { castedPtr->AddInput(SameGroupOrLink(GetGroup(), input)); });
275275
}
276276

277277
void RemoveInput(const Event<E>& input)
@@ -284,7 +284,7 @@ class EventSlot : public Event<E>
284284
NodeId nodeId = castedPtr->GetInputNodeId();
285285
auto& graphPtr = GetInternals(this->GetGroup()).GetGraphPtr();
286286

287-
graphPtr->AddInput(nodeId, [castedPtr, &input] { castedPtr->RemoveInput(input); });
287+
graphPtr->AddInput(nodeId, [this, castedPtr, &input] { castedPtr->RemoveInput(SameGroupOrLink(GetGroup(), input)); });
288288
}
289289

290290
void RemoveAllInputs()
@@ -314,28 +314,22 @@ class EventLink : public Event<E>
314314
EventLink(EventLink&&) = default;
315315
EventLink& operator=(EventLink&&) = default;
316316

317-
// Construct with explicit group
317+
// Construct with group
318318
EventLink(const Group& group, const Event<E>& input) :
319319
EventLink::Event( REACT_IMPL::CtorTag{ }, GetOrCreateLinkNode(group, input) )
320320
{ }
321321

322-
~EventLink()
323-
{
324-
auto nodePtr = GetNodePtr();
325-
}
326-
327322
protected:
328323
static auto GetOrCreateLinkNode(const Group& group, const Event<E>& input) -> decltype(auto)
329324
{
330325
using REACT_IMPL::EventLinkNode;
331326

332-
auto targetGraphPtr = GetInternals(group).GetGraphPtr();
327+
auto& targetGraphPtr = GetInternals(group).GetGraphPtr();
328+
auto& linkCache = targetGraphPtr->GetLinkCache();
333329

334330
void* k1 = GetInternals(input.GetGroup()).GetGraphPtr().get();
335331
void* k2 = GetInternals(input).GetNodePtr().get();
336332

337-
auto& linkCache = targetGraphPtr->GetLinkCache();
338-
339333
auto nodePtr = linkCache.LookupOrCreate<EventLinkNode<E>>(
340334
{ k1, k2 },
341335
[&]
@@ -495,7 +489,7 @@ static Event<E> SameGroupOrLink(const Group& targetGroup, const Event<E>& dep)
495489
if (dep.GetGroup() == targetGroup)
496490
return dep;
497491
else
498-
return EventLink<E>( targetGroup, dep );
492+
return EventLink<E>{ targetGroup, dep };
499493
}
500494

501495
/****************************************/ REACT_IMPL_END /***************************************/

include/react/Signal.h

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class SignalSlot : public Signal<S>
258258
NodeId nodeId = castedPtr->GetInputNodeId();
259259
auto& graphPtr = GetInternals(this->GetGroup()).GetGraphPtr();
260260

261-
graphPtr->AddInput(nodeId, [castedPtr, &newInput] { castedPtr->SetInput(newInput); });
261+
graphPtr->AddInput(nodeId, [this, castedPtr, &newInput] { castedPtr->SetInput(SameGroupOrLink(GetGroup(), newInput)); });
262262
}
263263
};
264264

@@ -275,24 +275,33 @@ class SignalLink : public Signal<S>
275275
SignalLink(SignalLink&&) = default;
276276
SignalLink& operator=(SignalLink&&) = default;
277277

278-
// Construct with explicit group
278+
// Construct with group
279279
SignalLink(const Group& group, const Signal<S>& input) :
280-
SignalLink::Signal( REACT_IMPL::CtorTag{ }, CreateLinkNode(group, input) )
281-
{ }
282-
283-
// Construct with implicit group
284-
explicit SignalLink(const Signal<S>& input) :
285-
SignalLink::Signal( REACT_IMPL::CtorTag{ }, CreateLinkNode(input.GetGroup(), input) )
280+
SignalLink::Signal( REACT_IMPL::CtorTag{ }, GetOrCreateLinkNode(group, input) )
286281
{ }
287282

288283
protected:
289-
static auto CreateLinkNode(const Group& group, const Signal<S>& input) -> decltype(auto)
284+
static auto GetOrCreateLinkNode(const Group& group, const Signal<S>& input) -> decltype(auto)
290285
{
291286
using REACT_IMPL::SignalLinkNode;
292287

293-
auto node = std::make_shared<SignalLinkNode<S>>(group, input);
294-
node->SetWeakSelfPtr(std::weak_ptr<SignalLinkNode<S>>{ node });
295-
return node;
288+
auto targetGraphPtr = GetInternals(group).GetGraphPtr();
289+
290+
void* k1 = GetInternals(input.GetGroup()).GetGraphPtr().get();
291+
void* k2 = GetInternals(input).GetNodePtr().get();
292+
293+
auto& linkCache = targetGraphPtr->GetLinkCache();
294+
295+
auto nodePtr = linkCache.LookupOrCreate<SignalLinkNode<S>>(
296+
{ k1, k2 },
297+
[&]
298+
{
299+
auto nodePtr = std::make_shared<SignalLinkNode<S>>(group, input);
300+
nodePtr->SetWeakSelfPtr(std::weak_ptr<SignalLinkNode<S>>{ nodePtr });
301+
return nodePtr;
302+
});
303+
304+
return nodePtr;
296305
}
297306
};
298307

include/react/detail/graph/EventNodes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,9 @@ class EventLinkNode : public EventStreamNode<E>
562562

563563
~EventLinkNode()
564564
{
565+
auto& linkCache = GetGraphPtr()->GetLinkCache();
566+
linkCache.Erase(this);
567+
565568
this->UnregisterMe();
566569
}
567570

include/react/detail/graph/PropagationST.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ class PtrCache
5454
}
5555

5656
template <typename V>
57-
void Erase(const std::shared_ptr<V>& ptr)
57+
void Erase(V* ptr)
5858
{
5959
std::lock_guard<std::mutex> scopedLock(mutex_);
6060

61-
auto it = map2_.find((void*)ptr.get());
61+
auto it = map2_.find((void*)ptr);
6262

6363
if (it != map2_.end())
6464
{

include/react/detail/graph/SignalNodes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ class SignalLinkNode : public SignalNode<S>
308308

309309
~SignalLinkNode()
310310
{
311+
auto& linkCache = GetGraphPtr()->GetLinkCache();
312+
linkCache.Erase(this);
313+
311314
this->UnregisterMe();
312315
}
313316

0 commit comments

Comments
 (0)