aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/py/source/bound.cpp
blob: a5ace4eebe49e396fa59147d09d31525672c07da (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* 

py/pyext - python external object for PD and MaxMSP

Copyright (c)2002-2005 Thomas Grill (gr@grrrr.org)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

*/

#include "pyext.h"
#include "flinternal.h"

#include <set>

class MethodCompare:
    public std::less<PyObject *>
{
public:
    bool operator()(PyObject *a,PyObject *b) const
    {
        if(PyMethod_Check(a))
            if(PyMethod_Check(b)) {
                // both are methods
                PyObject *sa = PyMethod_GET_SELF(a);
                PyObject *sb = PyMethod_GET_SELF(b);
                if(sa)
                    if(sb) {
                        // both have self
                        if(sa == sb)
                            return PyMethod_GET_FUNCTION(a) < PyMethod_GET_FUNCTION(b);
                        else
                            return sa < sb;
                    }
                    else 
                        return false;
                else
                    if(sb)
                        return true;
                    else 
                        return PyMethod_GET_FUNCTION(a) < PyMethod_GET_FUNCTION(b);
            }
            else
                return false;
        else
            if(PyMethod_Check(b))
                return true;
            else
                // both are functions
                return a < b;
    }
};

typedef std::set<PyObject *,MethodCompare> FuncSet;

struct bounddata 
{ 
    PyObject *self;
    FuncSet funcs;
};

bool pyext::boundmeth(flext_base *th,t_symbol *sym,int argc,t_atom *argv,void *data)
{
    bounddata *obj = (bounddata *)data;
    pyext *pyth = static_cast<pyext *>(th);

	PyThreadState *state = pyth->PyLock();

	PyObject *args = MakePyArgs(sym,argc,argv,-1,obj->self != NULL);

    // call all functions bound by this symbol
    for(FuncSet::iterator it = obj->funcs.begin(); it != obj->funcs.end(); ++it) {
	    PyObject *ret = PyObject_CallObject(*it,args);
	    if(!ret)
		    PyErr_Print();
        else
    	    Py_DECREF(ret);
    }

    Py_XDECREF(args);

	pyth->PyUnlock(state);
    return true;
}

PyObject *pyext::pyext_bind(PyObject *,PyObject *args)
{
    PyObject *self,*meth,*name;
    if(!PyArg_ParseTuple(args, "OOO:pyext_bind", &self,&name,&meth)) // borrowed references
		post("py/pyext - Wrong arguments!");
	else if(!PyInstance_Check(self) || !(PyMethod_Check(meth) || PyFunction_Check(meth))) {
		post("py/pyext - Wrong argument types!");
    }
	else {
        pyext *th = GetThis(self);
        FLEXT_ASSERT(th);

		const t_symbol *recv = pyObject_AsSymbol(name);

        void *data = NULL;
        if(recv && th->GetBoundMethod(recv,boundmeth,data)) {
            // already bound to that symbol and function
            bounddata *bdt = (bounddata *)data;
            FLEXT_ASSERT(bdt != NULL && bdt->self == self);

            FuncSet::iterator it = bdt->funcs.find(meth);
            if(it == bdt->funcs.end()) {
                bdt->funcs.insert(meth);
                Py_INCREF(meth);
            }
        }
        else {
    		Py_INCREF(self); // self is borrowed reference
            Py_INCREF(meth);

            bounddata *data = new bounddata;
            data->self = self;
            data->funcs.insert(meth);

            th->BindMethod(recv,boundmeth,data);
        }
	}

    Py_INCREF(Py_None);
    return Py_None;
}

PyObject *pyext::pyext_unbind(PyObject *,PyObject *args)
{
    PyObject *self,*meth,*name;
    if(!PyArg_ParseTuple(args, "OOO:pyext_bind", &self,&name,&meth))  // borrowed references
		post("py/pyext - Wrong arguments!");
	else if(!PyInstance_Check(self) || !(PyMethod_Check(meth) || PyFunction_Check(meth))) {
		post("py/pyext - Wrong argument types!");
    }
	else {
        pyext *th = GetThis(self);
        FLEXT_ASSERT(th);

		const t_symbol *recv = pyObject_AsSymbol(name);

        void *data = NULL;
        if(recv && th->GetBoundMethod(recv,boundmeth,data)) {
            bounddata *bdt = (bounddata *)data;
            FLEXT_ASSERT(bdt != NULL);

            // erase from map
            // ATTENTION: meth is different from the element found in the map
            // it just points to the same instance method
            FuncSet::iterator it = bdt->funcs.find(meth);
            if(it != bdt->funcs.end()) {
    	        Py_DECREF(*it);
                bdt->funcs.erase(it);               
            }
            else
                post("py/pyext - Function to unbind couldn't be found");

            if(bdt->funcs.empty()) {
    		    Py_DECREF(bdt->self);
                delete bdt; 

                th->UnbindMethod(recv,boundmeth,NULL);
            }
        }
	}

    Py_INCREF(Py_None);
    return Py_None;
}


void pyext::ClearBinding()
{
    // in case the object couldn't be constructed...
    if(!pyobj) return;

    pyext *th = GetThis(pyobj);
    FLEXT_ASSERT(th);

    void *data = NULL;
    const t_symbol *sym = NULL;

    // unbind all 
    while(th->UnbindMethod(sym,NULL,&data)) {
        bounddata *bdt = (bounddata *)data; 
        if(bdt) {
            for(FuncSet::iterator it = bdt->funcs.begin(); it != bdt->funcs.end(); ++it) 
                Py_DECREF(*it);

		    Py_DECREF(bdt->self);
            delete bdt; 
        }
    }
}