HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PY_AutoObject.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * COMMENTS:
7  * Use this class to automatically decrement the reference count on
8  * python objects.
9  *
10  * So many functions in the python API return a new reference
11  * that we don't increment the reference when you assign to this
12  * smart pointer. If you're calling a python function that returns
13  * a borrowed reference, use a PyObject* instead of this class.
14  *
15  * You should only need to include this file in .C files, not in .h files.
16  * For a class that is ok to include from .h files see PY_OpaqueObject.
17  */
18 
19 #ifndef __PY_AutoObject_h__
20 #define __PY_AutoObject_h__
21 
22 // Note that including this file may include the Python headers, which need
23 // to be included first to avoid gcc warnings.
24 #include "PY_CPythonAPI.h"
25 
26 // This class takes care of automatically decrementing the reference count
27 // on python objects.
29 {
30 public:
31  // This class will work with null py_object pointers because it calls
32  // PY_Py_XDECREF().
34  : myPyObject(py_object)
35  {}
36 
37  PY_AutoObject() : myPyObject(nullptr)
38  {
39  ;
40  }
41 
43  { PY_Py_XDECREF(myPyObject); }
44 
46  {
47  if (myPyObject != py_object)
48  {
49  PY_Py_XDECREF(myPyObject);
50  myPyObject = py_object;
51  }
52  return *this;
53  }
54 
55  operator PY_PyObject*()
56  { return myPyObject; }
57 
59  { return myPyObject; }
60 
62  { return myPyObject; }
63 
64  PY_PyObject const* ptr() const
65  { return myPyObject; }
66 
67 private:
68  PY_PyObject *myPyObject;
69 };
70 
71 #endif
PY_PyObject * ptr()
Definition: PY_AutoObject.h:61
PY_AutoObject & operator=(PY_PyObject *py_object)
Definition: PY_AutoObject.h:45
PY_PyObject const * ptr() const
Definition: PY_AutoObject.h:64
PY_AutoObject(PY_PyObject *py_object)
Definition: PY_AutoObject.h:33
#define PY_Py_XDECREF(op)
PY_PyObject * operator->()
Definition: PY_AutoObject.h:58
PY_PyObject