aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/py/source/register.cpp
blob: 16e1df65547a24c172c00c4ea021270b90f2fce2 (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
/* 

py/pyext - python external object for PD and MaxMSP

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

*/

#include "main.h"


V py::Register(const C *regnm)
{
	if(module) {
		// add this to module registry

		PyObject *reg = PyDict_GetItemString(dict,(C *)regnm); // borrowed!!!
		PyObject *add = Py_BuildValue("[i]",(long)this);
		if(!reg || !PyList_Check(reg)) {
			if(PyDict_SetItemString(dict,(C *)regnm,add)) {
				post("%s - Could not set registry",thisName());
			}
			// Py_XDECREF(reg);
		}
		else {
			PySequence_InPlaceConcat(reg,add);
		}
	}

}

V py::Unregister(const C *regnm)
{
	if(module) {
		// remove this from module registry
		
		PyObject *reg = PyDict_GetItemString(dict,(C *)regnm); // borrowed!!!
		PyObject *add = Py_BuildValue("i",(int)this);
		if(!reg || !PySequence_Check(reg)) 
			post("%s - Registry not found!?",thisName());
		else {
			I ix = PySequence_Index(reg,add);
			if(ix < 0) {
				post("%s - This object not found in registry?!",thisName());
			}
			else {	
				PySequence_DelItem(reg,ix);
			}
		}
		Py_DECREF(add);
	}

}

V py::Reregister(const C *regnm)
{
	if(module) {
		// remove this from module registry
		
		PyObject *reg = PyDict_GetItemString(dict,(C *)regnm); // borrowed!!!

		if(!reg || !PySequence_Check(reg)) 
			post("%s - Registry not found!?",thisName());
		else {
			I cnt = PySequence_Size(reg);
			for(I i = 0; i < cnt; ++i) {
				PyObject *it = PySequence_GetItem(reg,i); // new reference
				if(!it || !PyInt_Check(it)) {
					post("%s - Corrupt registry?!",thisName());
				}
				else {
					py *th = (py *)PyInt_AsLong(it);
					th->module = module;
					th->dict = dict;
					th->Reload();
				}

                Py_XDECREF(it);
			}
		}
	}

}