comparison PyFeature.cpp @ 31:4f1894c7591b vampy2

Created Vampy2 branch
author fazekasgy
date Sun, 20 Sep 2009 17:31:20 +0000
parents
children
comparison
equal deleted inserted replaced
28:5139bf30f208 31:4f1894c7591b
1 #include <Python.h>
2 #include "PyExtensionModule.h"
3 #include "PyFeature.h"
4 #include "vamp-sdk/Plugin.h"
5 #include <string>
6 /*#include "PyTypeInterface.h"*/
7
8 using namespace std;
9 using namespace Vamp;
10 using Vamp::Plugin;
11
12 /* Feature Object's Methods */
13 //Feature objects have no callable methods
14
15 /* PyFeature methods implementing protocols */
16 // these functions are called by the interpreter automatically
17
18
19 /* Function to set basic attributes
20 static int
21 Feature_setattr(FeatureObject *self, char *name, PyObject *value)
22 {
23 std::string key = std::string(name);
24 if (self->ti.SetValue(*(self->feature),key,value)) return 0;
25 else return -1;
26 }*/
27
28 /* Function to get basic attributes
29 static PyObject *
30 Feature_getattr(FeatureObject *self, char *name)
31 {
32 std::string key = std::string(name);
33 PyObject* pyValue;
34 if (self->ti.GetValue(*(self->feature),key,pyValue))
35 return pyValue;
36 else return NULL;
37 }*/
38
39 /* Set attributes */
40 static int
41 Feature_setattr(FeatureObject *self, char *name, PyObject *v)
42 {
43 if (v == NULL) {
44 int rv = PyDict_DelItemString(self->dict, name);
45 if (rv < 0)
46 PyErr_SetString(PyExc_AttributeError,"non-existing Feature attribute");
47 return rv;
48 }
49 else
50 return PyDict_SetItemString(self->dict, name, v);
51 }
52
53
54 /* Get attributes */
55 static PyObject *
56 Feature_getattr(FeatureObject *self, char *name)
57 {
58 if (self->dict != NULL) {
59 PyObject *v = PyDict_GetItemString(self->dict, name);
60 if (v != NULL)
61 {
62 Py_INCREF(v);
63 return v;
64 }
65 }
66 return NULL;
67 }
68
69 /* New Feature object */
70 static PyObject *
71 Feature_new(PyTypeObject *type, PyObject *args, PyObject *kw)
72 {
73 /// TODO support kwargs e.g. Feature(values = val, timestamp = ts)
74 cerr << "FeatureObject new method called" << endl;
75 if (!PyArg_ParseTuple(args, ":Feature")) {
76 PyErr_SetString(PyExc_TypeError,
77 "Error: Feature initialised with arguments.");
78 return NULL;
79 }
80 FeatureObject *self = (FeatureObject*)type->tp_alloc(type, 0);
81 // FeatureObject *self = PyObject_New(FeatureObject, &Feature_Type);
82 if (self == NULL) return NULL;
83 self->dict = PyDict_New();
84 if (self->dict == NULL) return NULL;
85 return (PyObject *) self;
86 }
87
88 /* DESTRUCTOR: delete type object */
89 static void
90 FeatureObject_dealloc(FeatureObject *self)
91 {
92 Py_XDECREF(self->dict);
93 // delete self->feature; //delete the C object
94 // PyObject_Del(self); //delete the Python object
95 self->ob_type->tp_free((PyObject*)self);
96 cerr << "Feature object deallocated." << endl;
97 }
98
99
100 static int
101 Feature_init(FeatureObject *self, PyObject *args, PyObject *kwds)
102 {
103 cerr << "FeatureObject Init called" << endl;
104 return 0;
105 }
106
107 PyObject*
108 Feature_test(PyObject *self, PyObject *args, PyObject *kwds)
109 {
110 cerr << "FeatureObject TEST called" << endl;
111 return self;
112 }
113
114
115 /* String representation */
116 static PyObject *
117 Feature_repr(PyObject *self)
118 {
119 // if (PyFeature_CheckExact(self)) {}
120 // PyObject* intdict = self
121 return Py_BuildValue("s",
122 "not yet implemented");
123 // ((RealTimeObject*)self)->rt->toString().c_str());
124 }
125
126 #define Feature_alloc PyType_GenericAlloc
127 #define Feature_free PyObject_Del
128
129
130 /* FEATURE TYPE OBJECT */
131
132 PyTypeObject Feature_Type = {
133 PyObject_HEAD_INIT(NULL)
134 0, /*ob_size*/
135 "vampy.Feature", /*tp_name*/
136 sizeof(FeatureObject), /*tp_basicsize*/
137 0, /*tp_itemsize*/
138 (destructor)FeatureObject_dealloc, /*tp_dealloc*/
139 0, /*tp_print*/
140 (getattrfunc)Feature_getattr, /*tp_getattr*/
141 (setattrfunc)Feature_setattr, /*tp_setattr*/
142 0, /*tp_compare*/
143 Feature_repr, /*tp_repr*/
144 0, /*tp_as_number*/
145 0, /*tp_as_sequence*/
146 0, /*tp_as_mapping*/
147 0, /*tp_hash*/
148 Feature_test, /*tp_call*/ // call on an instance
149 0, /*tp_str*/
150 0, /*tp_getattro*/
151 0, /*tp_setattro*/
152 0, /*tp_as_buffer*/
153 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
154 0, /*tp_doc*/
155 0, /*tp_traverse*/
156 0, /*tp_clear*/
157 0, /*tp_richcompare*/
158 0, /*tp_weaklistoffset*/
159 0, /*tp_iter*/
160 0, /*tp_iternext*/
161 0, /*tp_methods*/ //TypeObject Methods
162 0, /*tp_members*/
163 0, /*tp_getset*/
164 0, /*tp_base*/
165 0, /*tp_dict*/
166 0, /*tp_descr_get*/
167 0, /*tp_descr_set*/
168 0, /*tp_dictoffset*/
169 0,//(initproc)Feature_init, /*tp_init*/
170 Feature_alloc, /*tp_alloc*/
171 Feature_new, /*tp_new*/
172 Feature_free, /*tp_free*/
173 0, /*tp_is_gc*/
174 };
175
176 /* PyRealTime C++ API */
177
178 /*Feature* from PyFeature
179 const Vamp::Plugin::Feature*
180 PyFeature_AsFeature (PyObject *self) {
181
182 FeatureObject *s = (FeatureObject*) self;
183
184 if (!PyFeature_Check(s)) {
185 PyErr_SetString(PyExc_TypeError, "Feature Object Expected.");
186 cerr << "in call PyFeature_AsPointer(): Feature Object Expected. " << endl;
187 return NULL; }
188 return s->feature;
189 };*/