Skip to content

Commit cba953f

Browse files
committed
Remove functions without DirectObject in Messenger
1 parent 1c6d909 commit cba953f

File tree

2 files changed

+40
-122
lines changed

2 files changed

+40
-122
lines changed

render_pipeline/rppanda/showbase/messenger.hpp

Lines changed: 21 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -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

7968
public:
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+
161143
public:
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-
177155
inline 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-
218177
inline 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

254201
inline 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)
280220
inline 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-
313244
inline 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-
364284
inline void Messenger::add_hook(const EventName& event_name)
365285
{
366286
if (!handler_->has_hook(event_name, &Messenger::process_event, this))

src/rppanda/showbase/messenger.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@
4141

4242
namespace rppanda {
4343

44+
const Messenger::AcceptorMap Messenger::empty_acceptor_;
45+
4446
TypeHandle Messenger::type_handle_;
4547

48+
Messenger::Messenger() : handler_(EventHandler::get_global_event_handler())
49+
{
50+
}
51+
4652
Messenger::~Messenger()
4753
{
4854
clear();
@@ -65,7 +71,7 @@ void Messenger::accept(const EventName& event_name, const EventFunction& method,
6571

6672
add_hook(event_name);
6773

68-
auto& object_callbacks = hooks_[event_name].object_callbacks;
74+
auto& object_callbacks = hooks_[event_name];
6975

7076
auto found = object_callbacks.find(object);
7177
if (found == object_callbacks.end())
@@ -81,41 +87,33 @@ void Messenger::accept(const EventName& event_name, const EventFunction& method,
8187
}
8288
}
8389

90+
auto Messenger::who_accepts(const EventName& event_name) const -> const AcceptorMap&
91+
{
92+
auto found = hooks_.find(event_name);
93+
if (found != hooks_.end())
94+
return found->second;
95+
else
96+
return empty_acceptor_;
97+
}
98+
8499
void Messenger::process_event(const Event* ev, void* user_data)
85100
{
86101
const auto& event_name = ev->get_name();
87102

88103
auto self = reinterpret_cast<Messenger*>(user_data);
89104
auto& hook = self->hooks_.at(event_name);
90105

91-
// call in callbacks
92-
{
93-
auto iter = hook.callbacks.cbegin();
94-
95-
// NOTE: the end iterator is virtual end, so it indicates the end of list although new callbacks are inserted.
96-
// Therefore, this iterator will call also the new callbacks.
97-
auto iter_end = hook.callbacks.cend();
98-
for (; iter != iter_end; ++iter)
99-
{
100-
iter->first(ev);
101-
102-
// NOTE: the erased element is only invalidated, so we can use the iterator.
103-
if (!iter->second)
104-
hook.callbacks.erase(iter);
105-
}
106-
}
107-
108106
// call in object_callbacks
109107
{
110-
auto iter = hook.object_callbacks.cbegin();
111-
auto iter_end = hook.object_callbacks.cend();
108+
auto iter = hook.cbegin();
109+
auto iter_end = hook.cend();
112110
for (; iter != iter_end; ++iter)
113111
{
114112
iter->second.first(ev);
115113

116114
// NOTE: the erased element is only invalidated, so we can use the iterator.
117115
if (!iter->second.second)
118-
hook.object_callbacks.erase(iter);
116+
hook.erase(iter);
119117
}
120118
}
121119

0 commit comments

Comments
 (0)