images/plugin_impl/listeners.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: listeners.cpp
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-images.
00007  *
00008  * uds-plugin-images is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-images is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
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 /// Add listener. Repeated adding same callback function is
00042 /// allowed. So this function always returns the new added
00043 /// handler id.
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     // Have to search the whole map.
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 
Generated by  doxygen 1.6.2-20100208