pdf/plugin_impl/listeners.h

Go to the documentation of this file.
00001 /*
00002  * File Name: listeners.h
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-pdf.
00007  *
00008  * uds-plugin-pdf 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-pdf 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 #ifndef UTILS_LISTENER_H_
00028 #define UTILS_LISTENER_H_
00029 
00030 
00031 #include <vector>
00032 #include <map>
00033 #include "plugin_inc.h"
00034 
00035 namespace utils
00036 {
00037 
00038 /// Implement a event listener container.
00039 /// This class enables user to manage listeners for every
00040 /// PluginEvent type. The handler_id will never be zero,
00041 /// so listener can use zero as a special flag.
00042 class Listeners
00043 {
00044 public:
00045     Listeners(void);
00046     ~Listeners(void);
00047 
00048 public:
00049     /// Add listener. Repeated adding same callback function is
00050     /// allowed. So this function always returns the new added
00051     /// handler id.
00052     unsigned long add_listener(const PluginEvent event_type,
00053                                EventFunc callback,
00054                                void * user_data);
00055     
00056     /// Remove listener.
00057     bool remove_listener(unsigned long handler_id);
00058 
00059     /// Broadcast.
00060     void broadcast(IPluginUnknown *sender,
00061                    const PluginEvent event_type,
00062                    const PluginEventAttrs *plugin_data);
00063 
00064 private:
00065     // For every listener, it's necessary to maintain
00066     // callback function pointer, user_data and handler id.
00067     struct ListenerData
00068     {
00069         EventFunc callback;
00070         void * user_data;
00071         unsigned long handler_id;
00072     };
00073     typedef ListenerData * DataPtr;
00074     typedef std::vector<DataPtr> Datas;
00075     typedef std::vector<DataPtr>::iterator DatasIter;
00076     typedef std::map<PluginEvent, Datas> ListenerMap;
00077     typedef std::map<PluginEvent, Datas>::iterator ListenerMapIter;
00078     ListenerMap listeners;
00079     unsigned long handler_id;
00080 
00081 private:
00082     void broadcast_to_vector(Datas & listeners,
00083                              IPluginUnknown *sender,
00084                              const PluginEvent event_type,
00085                              const PluginEventAttrs *plugin_data);
00086 
00087     bool remove_from_vector(unsigned long handler_id,
00088                             Datas & listeners);
00089 
00090 };
00091 
00092 };  // namespace utils
00093 
00094 #endif  // UTILS_LISTENER_H_
00095 
Generated by  doxygen 1.6.2-20100208