00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Side Effects Software Inc 00008 * 123 Front Street West, Suite 1401 00009 * Toronto, Ontario 00010 * Canada M5J 2M2 00011 * 416-504-9876 00012 * 00013 * COMMENTS: 00014 * Use this class to automatically decrement the reference count on 00015 * python objects. 00016 * 00017 * So many functions in the python API return a new reference 00018 * that we don't increment the reference when you assign to this 00019 * smart pointer. If you're calling a python function that returns 00020 * a borrowed reference, use a PyObject* instead of this class. 00021 * 00022 * You should only need to include this file in .C files, not in .h files. 00023 */ 00024 00025 #ifndef __PY_AutoObject_h__ 00026 #define __PY_AutoObject_h__ 00027 00028 // Note that including this file may include the Python headers, which need 00029 // to be included first to avoid gcc warnings. 00030 #include "PY_CPythonAPI.h" 00031 00032 // This class takes care of automatically decrementing the reference count 00033 // on python objects. 00034 class PY_AutoObject 00035 { 00036 public: 00037 // This class will work with null py_object pointers because it calls 00038 // PY_Py_XDECREF(). 00039 PY_AutoObject(PY_PyObject *py_object) 00040 : myPyObject(py_object) 00041 {} 00042 00043 ~PY_AutoObject() 00044 { PY_Py_XDECREF(myPyObject); } 00045 00046 PY_AutoObject& operator=(PY_PyObject *py_object) 00047 { 00048 if (myPyObject != py_object) 00049 { 00050 PY_Py_XDECREF(myPyObject); 00051 myPyObject = py_object; 00052 } 00053 return *this; 00054 } 00055 00056 operator PY_PyObject*() 00057 { return myPyObject; } 00058 00059 PY_PyObject *operator->() 00060 { return myPyObject; } 00061 00062 PY_PyObject* ptr() 00063 { return myPyObject; } 00064 00065 private: 00066 PY_PyObject *myPyObject; 00067 }; 00068 00069 #endif
1.5.9