Honeycomb  0.1
Component-Model Framework
ListenerList.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 {
9 
11 
17 {
18 public:
20  struct Callback
21  {
22  virtual void onAdd(const Listener& listener) { mt_unused(listener); }
23  virtual void onRemove(const Listener& listener) { mt_unused(listener); }
24  };
25 
26  typedef list<SlotBase*, SmallAllocator<SlotBase*>> SlotList;
28 
29  ListenerList() : _cb(nullptr) {}
30  virtual ~ListenerList() { clear(); }
31 
33  void add(const Listener& listener);
35  void remove(const Listener& listener);
37  void remove(const void* obj);
39  void remove(const void* obj, const Id& id);
41  void clear();
42 
44  const ObjMap& list() const { return _objMap; }
45 
47  template<class Signal>
48  const SlotList* slotList() const { return slotList(Signal::id()); }
49 
51  template<class Signal, class... Args>
52  void dispatch(Args&&... args) const
53  {
54  SpinLock::Scoped _(const_cast<ListenerList*>(this)->_lock);
55  auto slots = slotList(Signal::id());
56  if (!slots) return;
57  for (auto& e : *slots)
58  (static_cast<priv::SlotSignal<Signal>&>(*e))(args...);
59  }
60 
62  void setCallback(Callback* cb) { _cb = cb; }
63 
64 private:
66  struct SlotIndex
67  {
68  SlotList list;
69  SlotMap map;
70  };
71  typedef stdutil::unordered_map<Id, SlotIndex, SmallAllocator> SignalMap;
72 
73  const SlotList* slotList(const Id& signalId) const;
74 
75  ObjMap _objMap;
76  SignalMap _signalMap;
77  SpinLock _lock;
78  Callback* _cb;
79 };
80 
81 }
virtual void onRemove(const Listener &listener)
Definition: ListenerList.h:23
#define mt_unused(Param)
Remove the unused parameter warning.
Definition: Meta.h:20
stdutil::unordered_multimap< const void *, Listener::ConstPtr, SmallAllocator > ObjMap
Definition: ListenerList.h:27
void setCallback(Callback *cb)
Set callback to handle events from this class.
Definition: ListenerList.h:62
virtual void onAdd(const Listener &listener)
Definition: ListenerList.h:22
static auto _
Definition: Module.cpp:8
Holds a slot that can receive signals.
Definition: Listener.h:11
void dispatch(Args &&...args) const
Send a signal to all listeners.
Definition: ListenerList.h:52
const ObjMap & list() const
Get all listeners, ordered by object instance.
Definition: ListenerList.h:44
Multicast sender.
Definition: Signal.h:26
Callback to handle events from this class.
Definition: ListenerList.h:20
void add(const Listener &listener)
Add a listener shared reference.
Definition: ListenerList.cpp:8
virtual ~ListenerList()
Definition: ListenerList.h:30
std::unordered_multimap< Key, Value, std::hash< Key >, std::equal_to< Key >, Alloc< pair< const Key, Value >>> unordered_multimap
std::unordered_multimap with custom allocator
Definition: StdUtil.h:86
ListenerList()
Definition: ListenerList.h:29
Holds a name string and its hashed value for fast comparison ops. See String Identifier.
Definition: Id.h:25
const SlotList * slotList() const
Get slots that receive signal. Returns null if none found.
Definition: ListenerList.h:48
void clear()
Remove all listeners.
Definition: ListenerList.cpp:69
Collection of listeners.
Definition: ListenerList.h:16
Global Honeycomb namespace.
list< SlotBase *, SmallAllocator< SlotBase * > > SlotList
Definition: ListenerList.h:26
OutSeq && map(Range &&, Seqs &&..., OutSeq &&, Func &&)
Transform a series of sequences into an output.