Etterna 0.74.4
Loading...
Searching...
No Matches
SubscriptionManager.h
1/* SubscriptionManager - Object that accepts subscriptions. */
2
3#ifndef SubscriptionManager_H
4#define SubscriptionManager_H
5
6#include <set>
7#include <cassert>
8
9// Since this class has only POD types and no constructor, there's no
10// initialize order problem.
11template<class T>
13{
14 public:
15 // TRICKY: If we make this a global instead of a global pointer,
16 // then we'd have to be careful that the static constructors of all
17 // subscribers are called before the collection constructor. It's
18 // impossible to enfore that in C++. Instead, we'll allocate the
19 // collection ourself on first use. SubscriptionHandler itself is
20 // a POD type, so a static SubscriptionHandler will always have
21 // m_pSubscribers == NULL (before any static constructors are called).
22 std::set<T*>* m_pSubscribers;
23
24 // Use this to access m_pSubscribers, so you don't have to worry about
25 // it being NULL.
26 std::set<T*>& Get()
27 {
28 if (m_pSubscribers == nullptr)
29 m_pSubscribers = new std::set<T*>;
30 return *m_pSubscribers;
31 }
32
33 void Subscribe(T* p)
34 {
35 if (m_pSubscribers == nullptr)
36 m_pSubscribers = new std::set<T*>;
37#ifdef DEBUG
38 typename set<T*>::iterator iter = m_pSubscribers->find(p);
39 ASSERT_M(iter == m_pSubscribers->end(), "already subscribed");
40#endif
41 m_pSubscribers->insert(p);
42 }
43
44 void Unsubscribe(T* p)
45 {
46 auto iter = m_pSubscribers->find(p);
47 assert(iter != m_pSubscribers->end());
48 m_pSubscribers->erase(iter);
49 }
50};
51
52#endif
Definition SubscriptionManager.h:13