]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/lldb/include/lldb/Utility/PythonPointer.h
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / lldb / include / lldb / Utility / PythonPointer.h
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 #include "lldb/lldb-python.h"
16
17 namespace lldb_private {
18
19 template<class T>
20 class PythonPointer
21 {
22 public: 
23     typedef PyObject* element_type; 
24 private:
25     element_type*      ptr_;
26     bool my_ref;
27 public:
28     
29     PythonPointer(element_type p, bool steal_ref = false) :
30     ptr_(p),
31     my_ref(!steal_ref)
32     {
33         if (my_ref)
34             Py_INCREF(ptr_);
35     }
36     
37     PythonPointer(const PythonPointer& r, bool steal_ref = false) :
38     ptr_(r.ptr_),
39     my_ref(!steal_ref)
40     {
41         if (my_ref)
42             Py_INCREF(ptr_);
43     }
44
45     ~PythonPointer()
46     {
47         if (my_ref)
48             Py_XDECREF(ptr_);
49     }
50     
51     PythonPointer
52     StealReference()
53     {
54         return PythonPointer(ptr_,true);
55     }
56     
57     PythonPointer
58     DuplicateReference()
59     {
60         return PythonPointer(ptr_, false);
61     }
62
63     element_type get() const {return ptr_;}
64     
65     bool IsNull() { return ptr_ == NULL; }
66     bool IsNone() { return ptr_ == Py_None; }
67     
68     operator PyObject* () { return ptr_; }
69 };
70
71 } // namespace lldb
72
73 #endif  // utility_PythonPointer_h_