lib

event.h

00001 /***************************************************************************
00002  * event.h
00003  * This file is part of the KDE project
00004  * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 #ifndef KROSS_API_EVENT_H
00021 #define KROSS_API_EVENT_H
00022 
00023 #include "../main/krossconfig.h"
00024 #include "object.h"
00025 #include "callable.h"
00026 #include "list.h"
00027 #include "exception.h"
00028 #include "function.h"
00029 #include "proxy.h"
00030 #include "variant.h"
00031 
00032 #include <qstring.h>
00033 #include <qvaluelist.h>
00034 #include <qmap.h>
00035 
00036 namespace Kross { namespace Api {
00037 
00044     template<class T>
00045     class Event : public Callable
00046     {
00047         private:
00048 
00052             typedef Object::Ptr(T::*FunctionPtr)(List::Ptr);
00053 
00058             QMap<QString, Function* > m_functions;
00059 
00060         public:
00061 
00067             Event(const QString& name)
00068                 : Callable(name)
00069             {
00070             }
00071 
00075             virtual ~Event()
00076             {
00077                 QMapConstIterator<QString, Function* > endit = m_functions.constEnd();
00078                 for(QMapConstIterator<QString, Function* > it = m_functions.constBegin(); it != endit; ++it)
00079                     delete it.data();
00080             }
00081 
00098             inline void addFunction(const QString& name, FunctionPtr function)
00099             {
00100                 m_functions.replace(name, new Function0<T>(static_cast<T*>(this), function));
00101             }
00102 
00115             inline void addFunction(const QString& name, Function* function)
00116             {
00117                 m_functions.replace(name, function);
00118             }
00119 
00124             template<class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class ARG3OBJ, class ARG4OBJ, class INSTANCE, typename METHOD>
00125             inline void addFunction4(const QString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0, ARG2OBJ* arg2 = 0, ARG3OBJ* arg3 = 0, ARG4OBJ* arg4 = 0)
00126             {
00127                 m_functions.replace(name,
00128                     new Kross::Api::ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ, ARG2OBJ, ARG3OBJ, ARG4OBJ>
00129                         (instance, method, arg1, arg2, arg3, arg4)
00130                 );
00131             }
00132 
00134             template<class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class ARG3OBJ, class INSTANCE, typename METHOD>
00135             inline void addFunction3(const QString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0, ARG2OBJ* arg2 = 0, ARG3OBJ* arg3 = 0)
00136             {
00137                 m_functions.replace(name,
00138                     new Kross::Api::ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ, ARG2OBJ, ARG3OBJ>
00139                         (instance, method, arg1, arg2, arg3)
00140                 );
00141             }
00142 
00144             template<class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class INSTANCE, typename METHOD>
00145             inline void addFunction2(const QString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0, ARG2OBJ* arg2 = 0)
00146             {
00147                 m_functions.replace(name,
00148                     new Kross::Api::ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ, ARG2OBJ>
00149                         (instance, method, arg1, arg2)
00150                 );
00151             }
00152 
00154             template<class RETURNOBJ, class ARG1OBJ, class INSTANCE, typename METHOD>
00155             inline void addFunction1(const QString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0)
00156             {
00157                 m_functions.replace(name,
00158                     new Kross::Api::ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ>
00159                         (instance, method, arg1)
00160                 );
00161             }
00162 
00164             template<class RETURNOBJ, class INSTANCE, typename METHOD>
00165             inline void addFunction0(const QString& name, INSTANCE* instance, METHOD method)
00166             {
00167                 m_functions.replace(name,
00168                     new Kross::Api::ProxyFunction<INSTANCE, METHOD, RETURNOBJ>
00169                         (instance, method)
00170                 );
00171             }
00172 
00178             bool isAFunction(const QString & name) const
00179             {
00180                 return m_functions.contains(name);
00181             }
00182 
00201             virtual Object::Ptr call(const QString& name, List::Ptr arguments)
00202             {
00203 #ifdef KROSS_API_EVENT_CALL_DEBUG
00204                 krossdebug( QString("Event::call() name='%1' getName()='%2'").arg(name).arg(getName()) );
00205 #endif
00206 
00207                 Function* function = m_functions[name];
00208                 if(function) {
00209 #ifdef KROSS_API_EVENT_CALL_DEBUG
00210                     krossdebug( QString("Event::call() name='%1' is a builtin function.").arg(name) );
00211 #endif
00212                     return function->call(arguments);
00213                 }
00214 
00215                 if(name.isNull()) {
00216                     // If no name is defined, we return a reference to our instance.
00217                     return this;
00218                 }
00219 
00220                 // Redirect the call to the Kross::Api::Callable we are inherited from.
00221                 return Callable::call(name, arguments);
00222             }
00223 
00224     };
00225 
00226 }}
00227 
00228 #endif
00229 
KDE Home | KDE Accessibility Home | Description of Access Keys