Honeycomb  0.1
Component-Model Framework
ListenerQueue.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
6 
7 namespace honey
8 {
10 namespace priv
11 {
12  struct SlotQueueBase
13  {
14  virtual void process() = 0;
15  virtual void clear() = 0;
16  };
17 
19  template<class T>
20  struct SlotQueueArg
21  {
22  typedef T StorageType;
23  template<class T2>
24  static void store(T& lhs, T2&& rhs) { lhs = forward<T2>(rhs); }
25  static T& load(T& lhs) { return lhs; }
26  };
27 
29  template<class T>
30  struct SlotQueueArg<T&>
31  {
32  typedef T* StorageType;
33  template<class T2>
34  static void store(T*& lhs, T2&& rhs) { lhs = &rhs; }
35  static T& load(T* lhs) { return *lhs; }
36  };
37 
38  template<class Signal, class F, class Seq = mt::make_idxseq<Signal::arity>>
39  class SlotQueue;
40  template<class Signal, class F, szt... Seq>
41  class SlotQueue<Signal,F,mt::idxseq<Seq...>> : public priv::SlotSignal<Signal>, public SlotQueueBase
42  {
43  typedef priv::SlotSignal<Signal> Super;
44  public:
45  SlotQueue(const Id& id, F&& f) : Super(id), _f(forward<F>(f)) {}
46 
47  virtual void operator()(const typename Signal::template param<Seq>&... args)
48  {
49  SpinLock::Scoped _(_lock);
50  _args.push_back(Args());
51  Args& queued = _args.back();
52  mt_unpackEval(priv::SlotQueueArg<typename Signal::template param<Seq>>::store(get<Seq>(queued), args));
53  }
54 
55  virtual void process()
56  {
57  SpinLock::Scoped _(_lock);
58  for (auto& args : _args)
59  _f(priv::SlotQueueArg<typename Signal::template param<Seq>>::load(get<Seq>(args))...);
60  _args.clear();
61  }
62 
63  virtual void clear()
64  {
65  SpinLock::Scoped _(_lock);
66  _args.clear();
67  }
68 
69  private:
70  typedef tuple<typename priv::SlotQueueArg<typename Signal::template param<Seq>>::StorageType...> Args;
71 
72  F _f;
73  vector<Args, SmallAllocator<Args>> _args;
74  SpinLock _lock;
75  };
76 }
78 //====================================================
79 // ListenerQueue
80 //====================================================
81 
83 
88 class ListenerQueue : public Listener
89 {
90 public:
93 
95  template<class Signal, class F>
96  static ListenerQueue& create(F&& f, const void* obj = nullptr, const Id& id = idnull)
97  {
98  return *new ListenerQueue(*new priv::SlotQueue<Signal,F>(id, forward<F>(f)), obj, id);
99  }
100 
102  void process() { _slot.process(); }
104  void clear() { _slot.clear(); }
105 
106 private:
107  template<class SlotQueue>
108  ListenerQueue(SlotQueue& slot, const void* obj, const Id& id) :
109  Listener(slot, obj, id), _slot(slot) {};
110 
111  priv::SlotQueueBase& _slot;
112 };
113 
114 }
SharedPtr< ListenerQueue > Ptr
Definition: ListenerQueue.h:91
Listener that holds queued slot for delayed processing of signals.
Definition: ListenerQueue.h:88
const void * obj() const
Get object instance for listener identification.
Definition: Listener.h:29
void clear()
Remove all signals stored in queue.
Definition: ListenerQueue.h:104
Combined intrusive/non-intrusive smart pointer. Can reference and share any object automatically...
Definition: SharedPtr.h:175
void process()
Dispatch all signals stored in queue, clears queue when done.
Definition: ListenerQueue.h:102
#define mt_unpackEval(...)
Unpack and evaluate a parameter pack expression. Ex. void foo(Args... args) { mt_unpackEval(func(args...
Definition: Meta.h:22
std::index_sequence< Ints... > idxseq
Shorthand for std::index_sequence.
Definition: Meta.h:111
static auto _
Definition: Module.cpp:8
Holds a slot that can receive signals.
Definition: Listener.h:11
static ListenerQueue & create(F &&f, const void *obj=nullptr, const Id &id=idnull)
Construct with slot to receive Signal using function F. The object instance and id are used together ...
Definition: ListenerQueue.h:96
#define idnull
Null id.
Definition: Id.h:124
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
UniqueLock< SpinLock > Scoped
Definition: Spin.h:20
Holds a name string and its hashed value for fast comparison ops. See String Identifier.
Definition: Id.h:25
SlotBase & slot() const
Get slot.
Definition: Listener.h:27
Global Honeycomb namespace.
SharedPtr< const ListenerQueue > ConstPtr
Definition: ListenerQueue.h:92