00001 // LICENSE: (Please see the file COPYING for details) 00002 // 00003 // NUS - Nemesis Utilities System: A C++ application development framework 00004 // Copyright (C) 2006, 2008 Otavio Rodolfo Piske 00005 // 00006 // This file is part of NUS 00007 // 00008 // This library is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU Lesser General Public 00010 // License as published by the Free Software Foundation version 2.1 00011 // of the License. 00012 // 00013 // This library 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 GNU 00016 // Lesser General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU Lesser General Public 00019 // License along with this library; if not, write to the Free Software 00020 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 // 00022 #ifndef NEVENT1_HPP 00023 #define NEVENT1_HPP 00024 00035 template <typename Param1> 00036 class NEvent1 { 00037 public: 00041 typedef NMap<nint32, NEventHandlerBase1<Param1> *> HandlersMap; 00042 00046 NEvent1(); 00047 00051 virtual ~NEvent1(); 00052 00059 template <typename ListenerT> 00060 NEventId attach(ListenerT* object, 00061 void (ListenerT::*member)(Param1)); 00062 00068 bool detach(NEventId id); 00069 00075 bool isAttached(NEventId id); 00076 00081 void notify(Param1 param) const; 00082 00083 private: 00084 HandlersMap m_handlers; 00085 int m_count; 00086 }; 00087 00088 template <typename Param1> 00089 NEvent1<Param1>::NEvent1() 00090 : m_handlers(), 00091 m_count(0) 00092 {} 00093 00094 template <typename Param1> 00095 NEvent1<Param1>::~NEvent1() { 00096 00097 for(typename HandlersMap::iterator it = m_handlers.begin(); 00098 it != m_handlers.end(); 00099 ++it) 00100 { 00101 delete it.value(); 00102 } 00103 } 00104 00105 00106 template <typename Param1> 00107 template <typename ListenerT> 00108 NEventId NEvent1<Param1>::attach(ListenerT* object, 00109 void (ListenerT::*member)(Param1)) 00110 { 00111 typedef void (ListenerT::*PtrMember)(Param1); 00112 00113 NEventHandler1<ListenerT, Param1> *handle = 00114 new NEventHandler1<ListenerT, Param1>(object, member); 00115 00116 m_handlers.insert(m_count, handle); 00117 m_count++; 00118 return m_count - 1; 00119 } 00120 00121 00122 // Finds the handler and, if it exists, remove it 00123 template <typename Param1> 00124 bool NEvent1<Param1>::detach(NEventId id) { 00125 typename HandlersMap::iterator it = m_handlers.find(id); 00126 00127 if(it == m_handlers.end()) { 00128 return false; 00129 } 00130 00131 if (m_handlers.erase(it)) { 00132 delete it.value(); 00133 } 00134 00135 00136 return true; 00137 } 00138 00139 template <typename Param1> 00140 bool NEvent1<Param1>::isAttached(NEventId id) { 00141 return m_handlers.find(id) != m_handlers.end(); 00142 } 00143 00144 template <typename Param1> 00145 void NEvent1<Param1>::notify(Param1 param) const { 00146 00147 for(typename HandlersMap::const_iterator it = m_handlers.constBegin(); 00148 it != m_handlers.constEnd(); it++) 00149 { 00150 it.value()->notify(param); 00151 } 00152 } 00153 00154 #endif // NEVENT1_HPP