1//===---------------------PythonPointer.h ------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef utility_PythonPointer_h_ 11#define utility_PythonPointer_h_ 12 13#include <algorithm> 14 15#if defined (__APPLE__) 16#include <Python/Python.h> 17#else 18#include <Python.h> 19#endif 20 21namespace lldb_private { 22 23template<class T> 24class PythonPointer 25{ 26public: 27 typedef PyObject* element_type; 28private: 29 element_type* ptr_; 30 bool my_ref; 31public: 32 33 PythonPointer(element_type p, bool steal_ref = false) : 34 ptr_(p), 35 my_ref(!steal_ref) 36 { 37 if (my_ref) 38 Py_INCREF(ptr_); 39 } 40 41 PythonPointer(const PythonPointer& r, bool steal_ref = false) : 42 ptr_(r.ptr_), 43 my_ref(!steal_ref) 44 { 45 if (my_ref) 46 Py_INCREF(ptr_); 47 } 48 49 ~PythonPointer() 50 { 51 if (my_ref) 52 Py_XDECREF(ptr_); 53 } 54 55 PythonPointer 56 StealReference() 57 { 58 return PythonPointer(ptr_,true); 59 } 60 61 PythonPointer 62 DuplicateReference() 63 { 64 return PythonPointer(ptr_, false); 65 } 66 67 element_type get() const {return ptr_;} 68 69 bool IsNull() { return ptr_ == NULL; } 70 bool IsNone() { return ptr_ == Py_None; } 71 72 operator PyObject* () { return ptr_; } 73}; 74 75} // namespace lldb 76 77#endif // utility_PythonPointer_h_ 78