summaryrefslogtreecommitdiffstats
path: root/lib/kross/api/function.h
blob: edfab09418fa5c64808ffeb531ea1f45438bce9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/***************************************************************************
 * function.h
 * This file is part of the KDE project
 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 ***************************************************************************/

#ifndef KROSS_API_FUNCTION_H
#define KROSS_API_FUNCTION_H

#include "../main/krossconfig.h"
#include "object.h"
#include "list.h"

#include <tqstring.h>

namespace Kross { namespace Api {

    /**
     * The base class for functions. Classes like \a Function0 and
     * \a ProxyFunction inheritate this class.
     */
    class Function
    {
        public:

            /**
             * Each function needs to implement the call-method which will
             * be executed if the function itself should be executed.
             */
            virtual Object::Ptr call(List::Ptr) = 0;

    };

    /**
     * This class implements the most abstract way to work with functions. It
     * implements pointing to functions of the form
     * @code
     * Kross::Api::Object::Ptr myfunc(Kross::Api::List::Ptr)
     * @endcode
     * where a low-level \a Object got returned that represents the returnvalue
     * of the function-call, and a \a List instance is passed that may contain
     * optional \a Object instances as parameters.
     */
    template<class INSTANCE>
    class Function0 : public Function
    {
        private:
            typedef Object::Ptr(INSTANCE::*Method)(List::Ptr);
            INSTANCE* m_instance;
            Method m_method;
        public:
            Function0(INSTANCE* instance, Method method)
                : m_instance(instance), m_method(method) {}
            Object::Ptr call(List::Ptr args)
                { return (m_instance->*m_method)(args); }
    };

    /**
     * Specialization of the \a Function0 which takes as additional parameter
     * a const-value. This const-value will be hidden for the scripting backend
     * and is only passed through on function-call.
     *
     * So, this class could be as example used to point to a function like;
     * @code
     * Kross::Api::Object::Ptr myfunc(Kross::Api::List::Ptr, int myinteger)
     * @endcode
     * and then we are able to point to the function with something like
     * @code
     * this->addFunction("myfunctionname",
     *     new Kross::Api::Function1< MYCLASS, int >(
     *         this, // pointer to an instance of MYCLASS
     *         &MYCLASS::myfunction, // the method which should be wrapped
     *         17 // the const-value we like to pass to the function.
     *         ) );
     * @endcode
     * The defined integer myinteger which has the value 17 will be passed
     * transparently to myfunc. The scripting-backend won't know that there
     * exists such an additional integer at all. So, it's hidden and the user
     * aka the scripting code won't be able to manipulate that additional
     * value.
     */
    template<class INSTANCE, typename P1>
    class Function1 : public Function
    {
        private:
            typedef Object::Ptr(INSTANCE::*Method)(List::Ptr, P1);
            INSTANCE* m_instance;
            Method m_method;
            P1 m_p1;
        public:
            Function1(INSTANCE* instance, Method method, P1 p1)
                : m_instance(instance), m_method(method), m_p1(p1) {}
            Object::Ptr call(List::Ptr args)
                { return (m_instance->*m_method)(args, m_p1); }
    };

    /**
     * Same as \a Function1 but with 2 additional parameters.
     */
    template<class INSTANCE, typename P1, typename P2>
    class Function2 : public Function
    {
        private:
            typedef Object::Ptr(INSTANCE::*Method)(List::Ptr, P1, P2);
            INSTANCE* m_instance;
            Method m_method;
            P1 m_p1;
            P2 m_p2;
        public:
            Function2(INSTANCE* instance, Method method, P1 p1, P2 p2)
                : m_instance(instance), m_method(method), m_p1(p1), m_p2(p2) {}
            Object::Ptr call(List::Ptr args)
                { return (m_instance->*m_method)(args, m_p1, m_p2); }
    };

}}

#endif