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