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 "argument.h"
00026 #include "callable.h"
00027 #include "list.h"
00028 #include "exception.h"
00029 #include "function.h"
00030 #include "proxy.h"
00031 #include "variant.h"
00032 
00033 #include <qstring.h>
00034 #include <qvaluelist.h>
00035 #include <qmap.h>
00036 #include <kdebug.h>
00037 
00038 namespace Kross { namespace Api {
00039 
00046     template<class T>
00047     class Event : public Callable
00048     {
00049         private:
00050 
00054             typedef Object::Ptr(T::*FunctionPtr)(List::Ptr);
00055 
00060             QMap<QString, Function* > m_functions;
00061 
00062         public:
00063 
00071             Event(const QString& name, Object::Ptr parent)
00072                 : Callable(name, parent, ArgumentList())
00073             {
00074             }
00075 
00079             virtual ~Event()
00080             {
00081                 for(QMapIterator<QString, Function* > it = m_functions.begin(); it != m_functions.end(); ++it)
00082                     delete it.data();
00083             }
00084 
00102 //TODO remove this method as soon as there is no code using it any longer.
00103             void addFunction(const QString& name, FunctionPtr function, const ArgumentList& arglist = ArgumentList())
00104             {
00105                 m_functions.replace(name, new VarFunction0<T>(static_cast<T*>(this), function));
00106         Q_UNUSED(arglist);
00107             }
00108 
00121             void addFunction(const QString& name, Function* function)
00122             {
00123                 m_functions.replace(name, function);
00124             }
00125 
00130             template<class RET, class ARG1, class ARG2, class ARG3, class ARG4, class INSTANCE, typename METHOD>
00131             inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00132             {
00133                 m_functions.replace(name,
00134                     new Kross::Api::ProxyFunction <
00135                         INSTANCE, METHOD,
00136                         RET, ARG1, ARG2, ARG3, ARG4
00137                     > ( instance, method ) );
00138             }
00139 
00141             template<class RET, class ARG1, class ARG2, class ARG3, class INSTANCE, typename METHOD>
00142             inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00143             {
00144                 m_functions.replace(name,
00145                     new Kross::Api::ProxyFunction <
00146                         INSTANCE, METHOD,
00147                         RET, ARG1, ARG2, ARG3
00148                     > ( instance, method ) );
00149             }
00150 
00152             template<class RET, class ARG1, class ARG2, class INSTANCE, typename METHOD>
00153             inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00154             {
00155                 m_functions.replace(name,
00156                     new Kross::Api::ProxyFunction <
00157                         INSTANCE, METHOD,
00158                         RET, ARG1, ARG2
00159                     > ( instance, method ) );
00160             }
00161 
00163             template<class RET, class ARG1, class INSTANCE, typename METHOD>
00164             inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00165             {
00166                 m_functions.replace(name,
00167                     new Kross::Api::ProxyFunction <
00168                         INSTANCE, METHOD,
00169                         RET, ARG1
00170                     > ( instance, method ) );
00171             }
00172 
00174             template<class RET, class INSTANCE, typename METHOD>
00175             inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00176             {
00177                 m_functions.replace(name,
00178                     new Kross::Api::ProxyFunction <
00179                         INSTANCE, METHOD,
00180                         RET
00181                     > ( instance, method ) );
00182             }
00183 
00189             bool isAFunction(const QString & name) const
00190             {
00191                 return m_functions.contains(name);
00192             }
00193 
00212             virtual Object::Ptr call(const QString& name, List::Ptr arguments)
00213             {
00214 #ifdef KROSS_API_EVENT_CALL_DEBUG
00215                 kdDebug() << QString("Event::call() name='%1' getName()='%2'").arg(name).arg(getName()) << endl;
00216 #endif
00217 
00218                 Function* function = m_functions[name];
00219                 if(function) {
00220 #ifdef KROSS_API_EVENT_CALL_DEBUG
00221                     kdDebug() << QString("Event::call() name='%1' is a builtin function.").arg(name) << endl;
00222 #endif
00223 
00224                     //FIXME checkArguments(arguments);
00225                     return function->call(arguments);
00226                 }
00227 
00228                 if(name.isNull()) {
00229                     // If no name is defined, we return a reference to our instance.
00230                     return this;
00231                 }
00232 
00233                 // Redirect the call to the Kross::Api::Callable we are inheritated from.
00234                 return Callable::call(name, arguments);
00235             }
00236 
00237     };
00238 
00239 }}
00240 
00241 #endif
00242 
KDE Home | KDE Accessibility Home | Description of Access Keys