OpenLexocad  27.1
PyExport.h
Go to the documentation of this file.
1 
8 #ifndef __PYEXPORT_H__
9 #define __PYEXPORT_H__
10 
11 // Std. configurations
12 
13 // needed header
14 #undef slots
15 
16 // (re-)defined in pyconfig.h
17 #if defined(_POSIX_C_SOURCE)
18 #undef _POSIX_C_SOURCE
19 #endif
20 
21 #include <Python.h>
22 #define slots
23 //#include <iostream>
24 
25 
26 
27 #include <base_defines.h>
28 
29 #include <typeinfo>
30 
31 
32 
33 // forward
34 // class FCInterpreter;
35 typedef struct _object PyObject;
36 
37 
38 namespace Base
39 {
40 class PyObjectBase;
41 
75 class CA_BASE_EXPORT PyHandler
76 {
77 public:
78  void IncRef(void);
79  void DecRef(void);
80  virtual PyObjectBase* GetPyObject(void) = 0;
81 };
82 
83 
95 template <class HandledType>
96 class PyHandle
97 {
98 public:
99  //**************************************************************************
100  // construction destruction
101 
107  PyHandle(HandledType* ToHandel = 0L) : _pHandels(ToHandel)
108  {
109  if (_pHandels)
110  _pHandels->IncRef();
111  }
112 
114  PyHandle(const PyHandle<HandledType>& ToHandel) : _pHandels(ToHandel._pHandels)
115  {
116  if (_pHandels)
117  _pHandels->IncRef();
118  }
119 
126  {
127  if (_pHandels)
128  _pHandels->DecRef();
129  }
130 
131  //**************************************************************************
132  // operator implementation
133 
134  // assign operator from a pointer
135  PyHandle<HandledType>& operator=(/*const*/ HandledType* other)
136  {
137  if (_pHandels)
138  _pHandels->DecRef();
139  // FIXME: Should be without "->_pHandels", shouldn't it? (Werner)
140  _pHandels = other; //_pHandels = other->_pHandels;
141  if (_pHandels)
142  _pHandels->IncRef();
143  return *this;
144  }
145  /*
146  // FIXME: Does this method make sense? I don't think so. (Werner)
147  // assign operator from a unknown pointer
148  PyHandle <HandledType> &operator=(void* other)
149  {
150  if(_pHandels)
151  _pHandels->DecRef();
152  if( PointsOn(other) )
153  // FIXME: Should be without "->_pHandels", shouldn't it? (Werner)
154  _pHandels = reinterpret_cast<HandledType*>(other);//_pHandels = other->_pHandels;
155  else
156  // invalid handle
157  _pHandels = 0L;
158  if(_pHandels)
159  _pHandels->IncRef();
160  return *this;
161  }
162  */
163  // assign operator from a handle
165  {
166  if (_pHandels)
167  _pHandels->DecRef();
168  _pHandels = other._pHandels;
169  if (_pHandels)
170  _pHandels->IncRef();
171  return *this;
172  }
173 
175  HandledType& operator*() { return *_pHandels; }
176 
178  HandledType* operator->() { return _pHandels; }
179 
181  const HandledType& operator*() const { return _pHandels; }
182 
184  const HandledType* operator->() const { return _pHandels; }
185 
189  bool operator<(const PyHandle<HandledType>& other) const
190  {
191  // return _pHandels<&other;
192  // FIXME: Shouldn't we compare both pointers?. (Werner)
193  return _pHandels < other._pHandels;
194  }
195 
197  bool operator==(const PyHandle<HandledType>& other) const
198  {
199  // return _pHandels==&other;
200  // FIXME: Shouldn't we compare both pointers?. (Werner)
201  return _pHandels == other._pHandels;
202  }
203 
206  {
207  // return (PyObject*) _pHandels;
208  // FIXME: Shouldn't we return the pointer's object?. (Werner)
209  return _pHandels->GetPyObject();
210  }
211  //**************************************************************************
212  // checking on the state
213 
215  bool IsValid(void) const { return _pHandels != 0; }
216 
218  bool IsNull(void) const { return _pHandels == 0; }
219  /*
221  bool IsLastRef(void) const
222  {
223  if(_pHandels && _pHadels->GetReferenceCount()==1)
224  return true;
225  return false;
226  }
227 
229  long GetReferenceCount(void) const
230  {
231  if(_pHandels)
232  return _pHadels->GetReferenceCount();
233  return 0;
234  }
235  */
240  // FIXME: Does this method make sense? I don't think so. (Werner)
241  /*
242  bool PointsOn(const void* other) const
243  {
244  if(!_pHandels)
245  if(other)
246  return false;
247  else
248  return true;
249  return typeid(*other) == typeid(HandledType) ;
250  }*/
251 
252 private:
254  HandledType* _pHandels;
255 };
256 
257 
258 } // namespace Base
259 
260 #endif
PyHandle(const PyHandle< HandledType > &ToHandel)
Copy constructor.
Definition: PyExport.h:114
PyHandle< HandledType > & operator=(const PyHandle< HandledType > &other)
Definition: PyExport.h:164
bool IsNull(void) const
Test if it not handels something.
Definition: PyExport.h:218
PyHandle(HandledType *ToHandel=0L)
Definition: PyExport.h:107
bool operator<(const PyHandle< HandledType > &other) const
Definition: PyExport.h:189
PyHandle< HandledType > & operator=(HandledType *other)
Definition: PyExport.h:135
~PyHandle()
Definition: PyExport.h:125
bool IsValid(void) const
Test if it handels something.
Definition: PyExport.h:215
bool operator==(const PyHandle< HandledType > &other) const
equal operator
Definition: PyExport.h:197
const HandledType * operator->() const
derefrence operators
Definition: PyExport.h:184
struct _object PyObject
Definition: PyExport.h:35
HandledType * operator->()
derefrence operators
Definition: PyExport.h:178
HandledType & operator *()
derefrence operators
Definition: PyExport.h:175
Definition: AbstractXMLReader.h:5
PyObject * GetPyObject(void)
returns the type as PyObject
Definition: PyExport.h:205
Definition: PyExport.h:75
Definition: PyExport.h:96