VideoTexture: use PyObjectPlus.h instead of Python.h for compatibility with Python2.3
[blender.git] / source / gameengine / VideoTexture / FilterBase.cpp
1 /* $Id$
2 -----------------------------------------------------------------------------
3 This source file is part of VideoTexture library
4
5 Copyright (c) 2007 The Zdeno Ash Miklas
6
7 This program is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the Free Software
9 Foundation; either version 2 of the License, or (at your option) any later
10 version.
11
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
19 http://www.gnu.org/copyleft/lesser.txt.
20 -----------------------------------------------------------------------------
21 */
22
23 #include "FilterBase.h"
24
25 #include <PyObjectPlus.h>
26 #include <structmember.h>
27
28
29 // FilterBase class implementation
30
31 // constructor
32 FilterBase::FilterBase (void) : m_previous(NULL) {}
33
34
35 // destructor
36 FilterBase::~FilterBase (void)
37 {
38         // release Python objects, if not released yet
39         release();
40 }
41
42
43 // release python objects
44 void FilterBase::release (void)
45 {
46         // release previous filter object
47         setPrevious(NULL);
48 }
49
50
51 // set new previous filter
52 void FilterBase::setPrevious (PyFilter * filt, bool useRefCnt)
53 {
54         // if reference counting has to be used
55         if (useRefCnt)
56         {
57                 // reference new filter
58                 if (filt != NULL) Py_INCREF(filt);
59                 // release old filter
60                 Py_XDECREF(m_previous);
61         }
62         // set new previous filter
63         m_previous = filt;
64 }
65
66
67 // find first filter
68 FilterBase * FilterBase::findFirst (void)
69 {
70         // find first filter in chain
71         FilterBase * frst;
72         for (frst = this; frst->m_previous != NULL; frst = frst->m_previous->m_filter);
73         // set first filter
74         return frst;
75 }
76
77
78
79 // list offilter types
80 PyTypeList pyFilterTypes;
81
82
83
84 // functions for python interface
85
86
87 // object allocation
88 PyObject * Filter_allocNew (PyTypeObject * type, PyObject * args, PyObject * kwds)
89 {
90         // allocate object
91         PyFilter * self = reinterpret_cast<PyFilter*>(type->tp_alloc(type, 0));
92         // initialize object structure
93         self->m_filter = NULL;
94         // return allocated object
95         return reinterpret_cast<PyObject*>(self);
96 }
97
98 // object deallocation
99 void Filter_dealloc (PyFilter * self)
100 {
101         // release object attributes
102         if (self->m_filter != NULL)
103         {
104                 self->m_filter->release();
105                 delete self->m_filter;
106                 self->m_filter = NULL;
107         }
108 }
109
110
111 // get previous pixel filter object
112 PyObject * Filter_getPrevious (PyFilter * self, void * closure)
113 {
114         // if filter object is available
115         if (self->m_filter != NULL)
116         {
117                 // pixel filter object
118                 PyObject * filt = reinterpret_cast<PyObject*>(self->m_filter->getPrevious());
119                 // if filter is present
120                 if (filt != NULL)
121                 {
122                         // return it
123                         Py_INCREF(filt);
124                         return filt;
125                 }
126         }
127         // otherwise return none
128         Py_RETURN_NONE;
129 }
130
131
132 // set previous pixel filter object
133 int Filter_setPrevious (PyFilter * self, PyObject * value, void * closure)
134 {
135         // if filter object is available
136         if (self->m_filter != NULL)
137         {
138                 // check new value
139                 if (value == NULL || !pyFilterTypes.in(value->ob_type))
140                 {
141                         // report value error
142                         PyErr_SetString(PyExc_TypeError, "Invalid type of value");
143                         return -1;
144                 }
145                 // set new value
146                 self->m_filter->setPrevious(reinterpret_cast<PyFilter*>(value));
147         }
148         // return success
149         return 0;
150 }