1 //===-- PythonDataObjects.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 liblldb_PythonDataObjects_h_
11 #define liblldb_PythonDataObjects_h_
12 
13 // C Includes
14 // C++ Includes
15 
16 // Other libraries and framework includes
17 // Project includes
18 #include "lldb/lldb-defines.h"
19 #include "lldb/Core/ConstString.h"
20 #include "lldb/Core/Flags.h"
21 #include "lldb/Interpreter/OptionValue.h"
22 #if defined (__APPLE__)
23 #include <Python/Python.h>
24 #else
25 #include <Python.h>
26 #endif
27 
28 namespace lldb_private {
29 
30     class PythonObject
31     {
32     public:
PythonObject()33         PythonObject () :
34             m_py_obj(NULL)
35         {
36         }
37 
PythonObject(PyObject * py_obj)38         PythonObject (PyObject* py_obj) :
39             m_py_obj(NULL)
40         {
41             Reset (py_obj);
42         }
43 
PythonObject(const PythonObject & rhs)44         PythonObject (const PythonObject &rhs) :
45             m_py_obj(NULL)
46         {
47             Reset (rhs.m_py_obj);
48         }
49 
50         PythonObject (const lldb::ScriptInterpreterObjectSP &script_object_sp);
51 
52         virtual
~PythonObject()53         ~PythonObject ()
54         {
55             Reset (NULL);
56         }
57 
58         const PythonObject &
59         operator = (const PythonObject &rhs)
60         {
61             if (this != &rhs)
62                 Reset (rhs.m_py_obj);
63             return *this;
64         }
65 
66         bool
Reset(const PythonObject & object)67         Reset (const PythonObject &object)
68         {
69             return Reset(object.GetPythonObject());
70         }
71 
72         virtual bool
73         Reset (PyObject* py_obj = NULL)
74         {
75             if (py_obj != m_py_obj)
76             {
77                 Py_XDECREF(m_py_obj);
78                 m_py_obj = py_obj;
79                 Py_XINCREF(m_py_obj);
80             }
81             return true;
82         }
83 
84         void
Dump()85         Dump () const
86         {
87             if (m_py_obj)
88                 _PyObject_Dump (m_py_obj);
89             else
90                 puts ("NULL");
91         }
92 
93         void
94         Dump (Stream &strm) const;
95 
96         PyObject*
GetPythonObject()97         GetPythonObject () const
98         {
99             return m_py_obj;
100         }
101 
102         PythonString
103         Repr ();
104 
105         PythonString
106         Str ();
107 
108         operator bool () const
109         {
110             return m_py_obj != NULL;
111         }
112 
113     protected:
114         PyObject* m_py_obj;
115     };
116 
117     class PythonString: public PythonObject
118     {
119     public:
120 
121         PythonString ();
122         PythonString (PyObject *o);
123         PythonString (const PythonObject &object);
124         PythonString (const lldb::ScriptInterpreterObjectSP &script_object_sp);
125         PythonString (const char* string);
126         virtual ~PythonString ();
127 
128         virtual bool
129         Reset (PyObject* py_obj = NULL);
130 
131         const char*
132         GetString() const;
133 
134         size_t
135         GetSize() const;
136 
137         void
138         SetString (const char* string);
139     };
140 
141     class PythonInteger: public PythonObject
142     {
143     public:
144 
145         PythonInteger ();
146         PythonInteger (PyObject* py_obj);
147         PythonInteger (const PythonObject &object);
148         PythonInteger (const lldb::ScriptInterpreterObjectSP &script_object_sp);
149         PythonInteger (int64_t value);
150         virtual ~PythonInteger ();
151 
152         virtual bool
153         Reset (PyObject* py_obj = NULL);
154 
155         int64_t
156         GetInteger();
157 
158         void
159         SetInteger (int64_t value);
160     };
161 
162     class PythonList: public PythonObject
163     {
164     public:
165 
166         PythonList ();
167         PythonList (PyObject* py_obj);
168         PythonList (const PythonObject &object);
169         PythonList (const lldb::ScriptInterpreterObjectSP &script_object_sp);
170         PythonList (uint32_t count);
171         virtual ~PythonList ();
172 
173         virtual bool
174         Reset (PyObject* py_obj = NULL);
175 
176         uint32_t
177         GetSize();
178 
179         PythonObject
180         GetItemAtIndex (uint32_t index);
181 
182         void
183         SetItemAtIndex (uint32_t index, const PythonObject &object);
184 
185         void
186         AppendItem (const PythonObject &object);
187     };
188 
189     class PythonDictionary: public PythonObject
190     {
191     public:
192 
193         PythonDictionary ();
194         PythonDictionary (PyObject* object);
195         PythonDictionary (const PythonObject &object);
196         PythonDictionary (const lldb::ScriptInterpreterObjectSP &script_object_sp);
197         virtual ~PythonDictionary ();
198 
199         virtual bool
200         Reset (PyObject* object = NULL);
201 
202         uint32_t GetSize();
203 
204         PythonObject
205         GetItemForKey (const PythonString &key) const;
206 
207         const char *
208         GetItemForKeyAsString (const PythonString &key, const char *fail_value = NULL) const;
209 
210         int64_t
211         GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value = 0) const;
212 
213         PythonObject
214         GetItemForKey (const char *key) const;
215 
216         typedef bool (*DictionaryIteratorCallback)(PythonString* key, PythonDictionary* dict);
217 
218         PythonList
219         GetKeys () const;
220 
221         PythonString
222         GetKeyAtPosition (uint32_t pos) const;
223 
224         PythonObject
225         GetValueAtPosition (uint32_t pos) const;
226 
227         void
228         SetItemForKey (const PythonString &key, const PythonObject& value);
229     };
230 
231 } // namespace lldb_private
232 
233 #endif  // liblldb_PythonDataObjects_h_
234