lib

function.h

00001 /***************************************************************************
00002  * function.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_FUNCTION_H
00021 #define KROSS_API_FUNCTION_H
00022 
00023 #include "../main/krossconfig.h"
00024 #include "object.h"
00025 #include "list.h"
00026 
00027 #include <qstring.h>
00028 
00029 namespace Kross { namespace Api {
00030 
00031     class Function
00032     {
00033         public:
00034             virtual Object::Ptr call(List::Ptr) = 0;
00035     };
00036 
00037     template<class INSTANCE>
00038     class ConstFunction0 : public Function
00039     {
00040         private:
00041             typedef Object::Ptr(INSTANCE::*Method)();
00042             INSTANCE* m_instance;
00043             Method m_method;
00044         public:
00045             ConstFunction0(INSTANCE* instance, Method method)
00046                 : m_instance(instance), m_method(method) {}
00047             Object::Ptr call(List::Ptr)
00048                 { return (m_instance->*m_method)(); }
00049     };
00050 
00051     template<class INSTANCE, typename P1>
00052     class ConstFunction1 : public Function
00053     {
00054         private:
00055             typedef Object::Ptr(INSTANCE::*Method)(P1);
00056             INSTANCE* m_instance;
00057             Method m_method;
00058             P1 m_p1;
00059         public:
00060             ConstFunction1(INSTANCE* instance, Method method, P1 p1)
00061                 : m_instance(instance), m_method(method), m_p1(p1) {}
00062             Object::Ptr call(List::Ptr)
00063                 { return (m_instance->*m_method)(m_p1); }
00064     };
00065 
00066     template<class INSTANCE, typename P1, typename P2>
00067     class ConstFunction2 : public Function
00068     {
00069         private:
00070             typedef Object::Ptr(INSTANCE::*Method)(P1, P2);
00071             INSTANCE* m_instance;
00072             Method m_method;
00073             P1 m_p1;
00074             P2 m_p2;
00075         public:
00076             ConstFunction2(INSTANCE* instance, Method method, P1 p1, P2 p2)
00077                 : m_instance(instance), m_method(method), m_p1(p1), m_p2(p2) {}
00078             Object::Ptr call(List::Ptr)
00079                 { return (m_instance->*m_method)(m_p1, m_p2); }
00080     };
00081 
00082     template<class INSTANCE>
00083     class VarFunction0 : public Function
00084     {
00085         private:
00086             typedef Object::Ptr(INSTANCE::*Method)(List::Ptr);
00087             INSTANCE* m_instance;
00088             Method m_method;
00089         public:
00090             VarFunction0(INSTANCE* instance, Method method)
00091                 : m_instance(instance), m_method(method) {}
00092             Object::Ptr call(List::Ptr args)
00093                 { return (m_instance->*m_method)(args); }
00094     };
00095 
00096     template<class INSTANCE, typename P1>
00097     class VarFunction1 : public Function
00098     {
00099         private:
00100             typedef Object::Ptr(INSTANCE::*Method)(List::Ptr, P1);
00101             INSTANCE* m_instance;
00102             Method m_method;
00103             P1 m_p1;
00104         public:
00105             VarFunction1(INSTANCE* instance, Method method, P1 p1)
00106                 : m_instance(instance), m_method(method), m_p1(p1) {}
00107             Object::Ptr call(List::Ptr args)
00108                 { return (m_instance->*m_method)(args, m_p1); }
00109     };
00110 
00111     template<class INSTANCE, typename P1, typename P2>
00112     class VarFunction2 : public Function
00113     {
00114         private:
00115             typedef Object::Ptr(INSTANCE::*Method)(List::Ptr, P1, P2);
00116             INSTANCE* m_instance;
00117             Method m_method;
00118             P1 m_p1;
00119             P2 m_p2;
00120         public:
00121             VarFunction2(INSTANCE* instance, Method method, P1 p1, P2 p2)
00122                 : m_instance(instance), m_method(method), m_p1(p1), m_p2(p2) {}
00123             Object::Ptr call(List::Ptr args)
00124                 { return (m_instance->*m_method)(args, m_p1, m_p2); }
00125     };
00126 
00127 }}
00128 
00129 #endif
00130 
KDE Home | KDE Accessibility Home | Description of Access Keys