pdf/plugin_impl/listeners.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "listeners.h"
00028
00029 namespace utils
00030 {
00031
00032 Listeners::Listeners(void)
00033 : handler_id(0)
00034 {
00035 }
00036
00037 Listeners::~Listeners(void)
00038 {
00039 }
00040
00041
00042
00043
00044 unsigned long Listeners::add_listener(const PluginEvent event_type,
00045 EventFunc callback,
00046 void * user_data)
00047 {
00048 ++handler_id;
00049 ListenerData * data = new ListenerData;
00050 data->callback = callback;
00051 data->user_data = user_data;
00052 data->handler_id = handler_id;
00053 listeners[event_type].push_back(data);
00054 return handler_id;
00055 }
00056
00057
00058 bool Listeners::remove_listener(unsigned long handler_id)
00059 {
00060
00061 ListenerMapIter begin = listeners.begin();
00062 ListenerMapIter end = listeners.end();
00063 for(ListenerMapIter it = begin; it != end; ++it)
00064 {
00065 if (remove_from_vector(handler_id, it->second))
00066 {
00067 return true;
00068 }
00069 }
00070 return false;
00071 }
00072
00073
00074 void Listeners::broadcast(IPluginUnknown *sender,
00075 const PluginEvent event_type,
00076 const PluginEventAttrs *plugin_data)
00077 {
00078 broadcast_to_vector(listeners[event_type], sender,
00079 event_type, plugin_data);
00080 }
00081
00082 void Listeners::broadcast_to_vector(Datas & listeners,
00083 IPluginUnknown *sender,
00084 const PluginEvent event_type,
00085 const PluginEventAttrs *plugin_data)
00086 {
00087 DatasIter begin = listeners.begin();
00088 DatasIter end = listeners.end();
00089 for(DatasIter it = begin; it != end; ++it)
00090 {
00091 (*(*it)->callback)( sender,
00092 (*it)->handler_id,
00093 event_type,
00094 (*it)->user_data,
00095 plugin_data );
00096 }
00097 }
00098
00099 bool Listeners::remove_from_vector(unsigned long handler_id,
00100 Datas & listeners)
00101 {
00102 DatasIter begin = listeners.begin();
00103 DatasIter end = listeners.end();
00104 for(DatasIter it = begin; it != end; ++it)
00105 {
00106 if ((*it)->handler_id == handler_id)
00107 {
00108 delete *it;
00109 listeners.erase(it);
00110 return true;
00111 }
00112 }
00113 return false;
00114 }
00115
00116 }
00117