@@ -62,19 +62,8 @@ class RENDER_PIPELINE_DECL Messenger : public TypedObject
6262 using EventFunction = std::function<void (const Event*)>;
6363
6464 using AcceptorType = std::pair<EventFunction, bool >; // { function, persistent }
65- using AcceptorList = std::list<AcceptorType>;
6665 using AcceptorMap = std::unordered_map<const DirectObject*, AcceptorType>;
67-
68- struct CallbackData
69- {
70- size_t size () const ;
71- bool empty () const ;
72-
73- AcceptorList callbacks;
74- AcceptorMap object_callbacks;
75- };
76-
77- using HooksType = std::unordered_map<EventName, CallbackData>;
66+ using HooksType = std::unordered_map<EventName, AcceptorMap>;
7867
7968public:
8069 static Messenger* get_global_instance ();
@@ -93,25 +82,16 @@ class RENDER_PIPELINE_DECL Messenger : public TypedObject
9382 *
9483 * @return Iterator of callbacks.
9584 */
96- void accept (const EventName& event_name, const EventFunction& method, bool persistent=true );
9785 void accept (const EventName& event_name, const EventFunction& method, const DirectObject* object,
9886 bool persistent = true );
9987
100- /* * Ignore the event has @event_name. */
101- void ignore (const EventName& event_name, const AcceptorList::const_iterator& iter);
102-
103- /* * Ignore the event has @event_name binding to DirectObject. */
104- void ignore (const EventName& event_name, const AcceptorMap::const_iterator& object);
105-
10688 /* * Ignore the event has @event_name binding to DirectObject. */
10789 void ignore (const EventName& event_name, const DirectObject* object);
10890
10991 /* *
11092 * Ignore all events has @event_name.
111- *
112- * If @include_object is false, only events not binded to DirectObject will be ignored.
11393 */
114- void ignore_all (const EventName& event_name, bool include_object= true );
94+ void ignore_all (const EventName& event_name);
11595
11696 /* * Ignore all events binding to DirectObject. */
11797 void ignore_all (const DirectObject* object);
@@ -123,7 +103,7 @@ class RENDER_PIPELINE_DECL Messenger : public TypedObject
123103 bool is_accepting (const EventName& event_name, const DirectObject* object) const ;
124104
125105 /* * Return objects accepting the given event. */
126- HooksType::const_iterator who_accepts (const EventName& event_name) const ;
106+ const AcceptorMap& who_accepts (const EventName& event_name) const ;
127107
128108 /* * Is this object ignoring this event? */
129109 bool is_ignoring (const EventName& event_name, const DirectObject* object) const ;
@@ -158,6 +138,8 @@ class RENDER_PIPELINE_DECL Messenger : public TypedObject
158138 EventHandler* handler_;
159139 HooksType hooks_;
160140
141+ static const AcceptorMap empty_acceptor_;
142+
161143public:
162144 static TypeHandle get_class_type ();
163145 static void init_type ();
@@ -170,10 +152,6 @@ class RENDER_PIPELINE_DECL Messenger : public TypedObject
170152
171153// ************************************************************************************************
172154
173- inline Messenger::Messenger () : handler_(EventHandler::get_global_event_handler())
174- {
175- }
176-
177155inline size_t Messenger::get_num_listners () const
178156{
179157 size_t count = 0 ;
@@ -196,59 +174,28 @@ inline AsyncFuture* Messenger::get_future(const EventName& event_name) const
196174 return handler_->get_future (event_name);
197175}
198176
199- inline void Messenger::accept (const EventName& event_name, const EventFunction& method,
200- bool persistent)
201- {
202- add_hook (event_name);
203- hooks_[event_name].callbacks .push_back (AcceptorType{ method, persistent });
204- }
205-
206- inline void Messenger::ignore (const EventName& event_name, const AcceptorList::const_iterator& iter)
207- {
208- auto found = hooks_.find (event_name);
209- if (found == hooks_.end ())
210- return ;
211-
212- found->second .callbacks .erase (iter);
213-
214- if (found->second .empty ())
215- remove_hook (event_name);
216- }
217-
218177inline void Messenger::ignore (const EventName& event_name, const DirectObject* object)
219178{
220179 auto found = hooks_.find (event_name);
221180 if (found == hooks_.end ())
222181 return ;
223182
224- if (object)
225- {
226- auto & object_callbacks = found->second .object_callbacks ;
227- auto object_callbacks_found = object_callbacks.find (object);
228- if (object_callbacks_found != object_callbacks.end ())
229- object_callbacks.erase (object_callbacks_found);
230- }
231- else
232- {
233- found->second .callbacks .clear ();
234- }
183+ auto & hook = found->second ;
184+ auto hook_found = hook.find (object);
185+ if (hook_found != hook.end ())
186+ hook.erase (hook_found);
235187
236- if (found-> second .empty ())
188+ if (hook .empty ())
237189 remove_hook (event_name);
238190}
239191
240- inline void Messenger::ignore_all (const EventName& event_name, bool include_object )
192+ inline void Messenger::ignore_all (const EventName& event_name)
241193{
242194 auto found = hooks_.find (event_name);
243195 if (found == hooks_.end ())
244196 return ;
245197
246- found->second .callbacks .clear ();
247- if (include_object)
248- found->second .object_callbacks .clear ();
249-
250- if (found->second .empty ())
251- remove_hook (event_name);
198+ remove_hook (event_name);
252199}
253200
254201inline void Messenger::ignore_all (const DirectObject* object)
@@ -257,17 +204,10 @@ inline void Messenger::ignore_all(const DirectObject* object)
257204 const auto iter_end = hooks_.end ();
258205 while (iter != iter_end)
259206 {
260- if (object)
261- {
262- auto & object_callbacks = iter->second .object_callbacks ;
263- auto found = object_callbacks.find (object);
264- if (found != object_callbacks.end ())
265- object_callbacks.erase (found);
266- }
267- else
268- {
269- iter->second .callbacks .clear ();
270- }
207+ auto & hook = iter->second ;
208+ auto found = hook.find (object);
209+ if (found != hook.end ())
210+ hook.erase (found);
271211
272212 // iterator becomes invalidated after erase.
273213 auto iter_tmp = iter;
@@ -280,11 +220,11 @@ inline void Messenger::ignore_all(const DirectObject* object)
280220inline auto Messenger::get_all_accepting (const DirectObject* object) const -> std::vector<EventName>
281221{
282222 std::vector<EventName> events;
283- for (const auto & hook : hooks_)
223+ for (const auto & kv : hooks_)
284224 {
285- const auto & object_callbacks = hook .second . object_callbacks ;
286- if (object_callbacks .find (object) != object_callbacks .end ())
287- events.push_back (hook .first );
225+ const auto & hook = kv .second ;
226+ if (hook .find (object) != hook .end ())
227+ events.push_back (kv .first );
288228 }
289229 return events;
290230}
@@ -294,22 +234,13 @@ inline bool Messenger::is_accepting(const EventName& event_name, const DirectObj
294234 auto found = hooks_.find (event_name);
295235 if (found != hooks_.end ())
296236 {
297- if (found->second .object_callbacks . find (object) != found->second .object_callbacks . cend ())
237+ if (found->second .find (object) != found->second .end ())
298238 return true ;
299239 }
300240
301241 return false ;
302242}
303243
304- inline auto Messenger::who_accepts (const EventName& event_name) const -> HooksType::const_iterator
305- {
306- auto found = hooks_.find (event_name);
307- if (found != hooks_.end ())
308- return found;
309- else
310- return hooks_.end ();
311- }
312-
313244inline bool Messenger::is_ignoring (const EventName& event_name, const DirectObject* object) const
314245{
315246 return !is_accepting (event_name, object);
@@ -350,17 +281,6 @@ inline void Messenger::send(const EventName& event_name, const EventParameter& p
350281 throw_event_directly (*handler_, event_name, p1, p2, p3);
351282}
352283
353- inline size_t Messenger::CallbackData::size () const
354- {
355- // NOTE: std::list::size has O(1) in C++11
356- return callbacks.size () + object_callbacks.size ();
357- }
358-
359- inline bool Messenger::CallbackData::empty () const
360- {
361- return callbacks.empty () && object_callbacks.empty ();
362- }
363-
364284inline void Messenger::add_hook (const EventName& event_name)
365285{
366286 if (!handler_->has_hook (event_name, &Messenger::process_event, this ))
0 commit comments