1 //===-- PythonDataObjects.h----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_PythonDataObjects_h_
11 #define liblldb_PythonDataObjects_h_
16 // Other libraries and framework includes
18 #include "lldb/lldb-defines.h"
19 #include "lldb/Core/ConstString.h"
20 #include "lldb/Core/StructuredData.h"
21 #include "lldb/Core/Flags.h"
22 #include "lldb/Interpreter/OptionValue.h"
23 #include "lldb/lldb-python.h"
25 namespace lldb_private {
28 class PythonDictionary;
32 class StructuredPythonObject : public StructuredData::Generic
35 StructuredPythonObject()
36 : StructuredData::Generic()
40 StructuredPythonObject(void *obj)
41 : StructuredData::Generic(obj)
43 Py_XINCREF(GetValue());
46 virtual ~StructuredPythonObject()
48 if (Py_IsInitialized())
49 Py_XDECREF(GetValue());
54 IsValid() const override
56 return GetValue() && GetValue() != Py_None;
59 void Dump(Stream &s) const override;
62 DISALLOW_COPY_AND_ASSIGN(StructuredPythonObject);
65 enum class PyObjectType
83 explicit PythonObject (PyObject* py_obj) :
89 PythonObject (const PythonObject &rhs) :
102 Reset (const PythonObject &object)
104 return Reset(object.get());
108 Reset (PyObject* py_obj = NULL)
110 if (py_obj != m_py_obj)
112 if (Py_IsInitialized())
113 Py_XDECREF(m_py_obj);
115 if (Py_IsInitialized())
116 Py_XINCREF(m_py_obj);
125 _PyObject_Dump (m_py_obj);
131 Dump (Stream &strm) const;
139 PyObjectType GetObjectType() const;
147 explicit operator bool () const
149 return m_py_obj != NULL;
153 IsNULLOrNone () const;
155 StructuredData::ObjectSP CreateStructuredObject() const;
161 class PythonString: public PythonObject
165 PythonString (PyObject *o);
166 PythonString (const PythonObject &object);
167 PythonString (llvm::StringRef string);
168 PythonString (const char *string);
169 virtual ~PythonString ();
172 Reset (PyObject* py_obj = NULL);
180 void SetString(llvm::StringRef string);
182 StructuredData::StringSP CreateStructuredString() const;
185 class PythonInteger: public PythonObject
190 PythonInteger (PyObject* py_obj);
191 PythonInteger (const PythonObject &object);
192 PythonInteger (int64_t value);
193 virtual ~PythonInteger ();
196 Reset (PyObject* py_obj = NULL);
198 int64_t GetInteger() const;
201 SetInteger (int64_t value);
203 StructuredData::IntegerSP CreateStructuredInteger() const;
206 class PythonList: public PythonObject
210 PythonList (bool create_empty);
211 PythonList (PyObject* py_obj);
212 PythonList (const PythonObject &object);
213 PythonList (uint32_t count);
214 virtual ~PythonList ();
217 Reset (PyObject* py_obj = NULL);
219 uint32_t GetSize() const;
221 PythonObject GetItemAtIndex(uint32_t index) const;
224 SetItemAtIndex (uint32_t index, const PythonObject &object);
227 AppendItem (const PythonObject &object);
229 StructuredData::ArraySP CreateStructuredArray() const;
232 class PythonDictionary: public PythonObject
236 explicit PythonDictionary (bool create_empty);
237 PythonDictionary (PyObject* object);
238 PythonDictionary (const PythonObject &object);
239 virtual ~PythonDictionary ();
242 Reset (PyObject* object = NULL);
244 uint32_t GetSize() const;
247 GetItemForKey (const PythonString &key) const;
250 GetItemForKeyAsString (const PythonString &key, const char *fail_value = NULL) const;
253 GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value = 0) const;
256 GetItemForKey (const char *key) const;
258 typedef bool (*DictionaryIteratorCallback)(PythonString* key, PythonDictionary* dict);
264 GetKeyAtPosition (uint32_t pos) const;
267 GetValueAtPosition (uint32_t pos) const;
270 SetItemForKey (const PythonString &key, PyObject *value);
273 SetItemForKey (const PythonString &key, const PythonObject& value);
275 StructuredData::DictionarySP CreateStructuredDictionary() const;
278 } // namespace lldb_private
280 #endif // liblldb_PythonDataObjects_h_