InspIRCd  3.0
event.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2015 Attila Molnar <[email protected]>
5  *
6  * This file is part of InspIRCd. InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 
20 #pragma once
21 
22 namespace Events
23 {
24  class ModuleEventListener;
25  class ModuleEventProvider;
26 }
27 
35 {
36  public:
37  typedef std::vector<ModuleEventListener*> SubscriberList;
38 
43  ModuleEventProvider(Module* mod, const std::string& eventid)
44  : ServiceProvider(mod, eventid, SERVICE_DATA)
45  , prov(mod, eventid)
46  {
47  prov.SetCaptureHook(this);
48  }
49 
53  const SubscriberList& GetSubscribers() const { return prov->subscribers; }
54 
55  friend class ModuleEventListener;
56 
57  private:
58  void OnCapture() CXX11_OVERRIDE
59  {
60  // If someone else holds the list from now on, clear mine. See below for more info.
61  if (*prov != this)
62  subscribers.clear();
63  }
64 
71 
75  SubscriberList subscribers;
76 };
77 
82 {
86 
89  void OnCapture() CXX11_OVERRIDE
90  {
91  prov->subscribers.push_back(this);
92  }
93 
94  public:
99  ModuleEventListener(Module* mod, const std::string& eventid)
100  : prov(mod, eventid)
101  {
102  prov.SetCaptureHook(this);
103  // If the dynamic_reference resolved at construction our capture handler wasn't called
104  if (prov)
105  ModuleEventListener::OnCapture();
106  }
107 
109  {
110  if (prov)
111  stdalgo::erase(prov->subscribers, this);
112  }
113 };
114 
120 #define FOREACH_MOD_CUSTOM(prov, listenerclass, func, params) do { \
121  const Events::ModuleEventProvider::SubscriberList& _handlers = (prov).GetSubscribers(); \
122  for (Events::ModuleEventProvider::SubscriberList::const_iterator _i = _handlers.begin(); _i != _handlers.end(); ++_i) \
123  { \
124  listenerclass* _t = static_cast<listenerclass*>(*_i); \
125  _t->func params ; \
126  } \
127 } while (0);
128 
136 #define FIRST_MOD_RESULT_CUSTOM(prov, listenerclass, func, result, params) do { \
137  result = MOD_RES_PASSTHRU; \
138  const Events::ModuleEventProvider::SubscriberList& _handlers = (prov).GetSubscribers(); \
139  for (Events::ModuleEventProvider::SubscriberList::const_iterator _i = _handlers.begin(); _i != _handlers.end(); ++_i) \
140  { \
141  listenerclass* _t = static_cast<listenerclass*>(*_i); \
142  result = _t->func params ; \
143  if (result != MOD_RES_PASSTHRU) \
144  break; \
145  } \
146 } while (0);
ModuleEventListener(Module *mod, const std::string &eventid)
Definition: event.h:99
ModuleEventProvider(Module *mod, const std::string &eventid)
Definition: event.h:43
Definition: dynref.h:95
Definition: modules.h:245
Definition: event.h:22
const SubscriberList & GetSubscribers() const
Definition: event.h:53
Definition: base.h:244
void SetCaptureHook(CaptureHook *h)
Definition: dynref.h:52
Definition: event.h:34
Definition: event.h:81