Files
llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
T

280 lines
6.3 KiB
C++
Raw Normal View History

//===-- PythonDataObjects.h----------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/lldb-defines.h"
#include "lldb/Core/ConstString.h"
2015-03-17 20:04:04 +00:00
#include "lldb/Core/StructuredData.h"
#include "lldb/Core/Flags.h"
#include "lldb/Interpreter/OptionValue.h"
namespace lldb_private {
2015-03-17 20:04:04 +00:00
class PythonString;
class PythonList;
class PythonDictionary;
class PythonObject;
class PythonInteger;
class StructuredPythonObject : public StructuredData::Generic
{
public:
StructuredPythonObject()
: StructuredData::Generic()
{
}
StructuredPythonObject(void *obj)
: StructuredData::Generic(obj)
{
Py_XINCREF(GetValue());
}
virtual ~StructuredPythonObject()
{
if (Py_IsInitialized())
Py_XDECREF(GetValue());
SetValue(nullptr);
}
bool
IsValid() const override
{
return GetValue() && GetValue() != Py_None;
}
void Dump(Stream &s) const override;
private:
DISALLOW_COPY_AND_ASSIGN(StructuredPythonObject);
};
enum class PyObjectType
{
Unknown,
None,
Integer,
Dictionary,
List,
String
};
2013-01-18 23:41:08 +00:00
class PythonObject
{
public:
2013-01-18 23:41:08 +00:00
PythonObject () :
m_py_obj(NULL)
{
}
explicit PythonObject (PyObject* py_obj) :
2013-01-18 23:41:08 +00:00
m_py_obj(NULL)
{
2013-01-18 23:41:08 +00:00
Reset (py_obj);
}
2013-01-18 23:41:08 +00:00
PythonObject (const PythonObject &rhs) :
m_py_obj(NULL)
{
2013-01-18 23:41:08 +00:00
Reset (rhs.m_py_obj);
}
virtual
~PythonObject ()
{
Reset (NULL);
}
2013-01-18 23:41:08 +00:00
bool
Reset (const PythonObject &object)
{
return Reset(object.get());
2013-01-18 23:41:08 +00:00
}
virtual bool
Reset (PyObject* py_obj = NULL)
{
if (py_obj != m_py_obj)
{
2014-08-08 23:20:25 +00:00
if (Py_IsInitialized())
Py_XDECREF(m_py_obj);
2013-01-18 23:41:08 +00:00
m_py_obj = py_obj;
2014-08-08 23:20:25 +00:00
if (Py_IsInitialized())
Py_XINCREF(m_py_obj);
}
2013-01-18 23:41:08 +00:00
return true;
}
2013-01-18 23:41:08 +00:00
void
Dump () const
{
2013-01-18 23:41:08 +00:00
if (m_py_obj)
_PyObject_Dump (m_py_obj);
else
puts ("NULL");
}
void
Dump (Stream &strm) const;
2013-01-18 23:41:08 +00:00
PyObject*
get () const
2013-01-18 23:41:08 +00:00
{
return m_py_obj;
}
2015-03-17 20:04:04 +00:00
PyObjectType GetObjectType() const;
PythonString
Repr ();
PythonString
Str ();
explicit operator bool () const
{
2013-01-18 23:41:08 +00:00
return m_py_obj != NULL;
}
2014-02-19 01:45:22 +00:00
bool
IsNULLOrNone () const;
2015-03-17 20:04:04 +00:00
StructuredData::ObjectSP CreateStructuredObject() const;
2013-01-18 23:41:08 +00:00
protected:
PyObject* m_py_obj;
};
2013-01-18 23:41:08 +00:00
class PythonString: public PythonObject
{
public:
2013-01-18 23:41:08 +00:00
PythonString ();
PythonString (PyObject *o);
PythonString (const PythonObject &object);
2015-03-17 20:04:04 +00:00
PythonString (llvm::StringRef string);
PythonString (const char *string);
2013-01-18 23:41:08 +00:00
virtual ~PythonString ();
2015-03-17 20:04:04 +00:00
2013-01-18 23:41:08 +00:00
virtual bool
Reset (PyObject* py_obj = NULL);
2015-03-17 20:04:04 +00:00
llvm::StringRef
GetString() const;
size_t
GetSize() const;
2015-03-17 20:04:04 +00:00
void SetString(llvm::StringRef string);
StructuredData::StringSP CreateStructuredString() const;
};
2013-01-18 23:41:08 +00:00
class PythonInteger: public PythonObject
{
public:
2013-01-18 23:41:08 +00:00
PythonInteger ();
PythonInteger (PyObject* py_obj);
PythonInteger (const PythonObject &object);
PythonInteger (int64_t value);
virtual ~PythonInteger ();
virtual bool
Reset (PyObject* py_obj = NULL);
2015-03-17 20:04:04 +00:00
int64_t GetInteger() const;
void
SetInteger (int64_t value);
2015-03-17 20:04:04 +00:00
StructuredData::IntegerSP CreateStructuredInteger() const;
};
2013-01-18 23:41:08 +00:00
class PythonList: public PythonObject
{
public:
PythonList (bool create_empty);
2013-01-18 23:41:08 +00:00
PythonList (PyObject* py_obj);
PythonList (const PythonObject &object);
PythonList (uint32_t count);
virtual ~PythonList ();
virtual bool
Reset (PyObject* py_obj = NULL);
2015-03-17 20:04:04 +00:00
uint32_t GetSize() const;
PythonObject GetItemAtIndex(uint32_t index) const;
void
2013-01-18 23:41:08 +00:00
SetItemAtIndex (uint32_t index, const PythonObject &object);
void
2013-01-18 23:41:08 +00:00
AppendItem (const PythonObject &object);
2015-03-17 20:04:04 +00:00
StructuredData::ArraySP CreateStructuredArray() const;
};
2013-01-18 23:41:08 +00:00
class PythonDictionary: public PythonObject
{
public:
explicit PythonDictionary (bool create_empty);
2013-01-18 23:41:08 +00:00
PythonDictionary (PyObject* object);
PythonDictionary (const PythonObject &object);
virtual ~PythonDictionary ();
virtual bool
Reset (PyObject* object = NULL);
2015-03-17 20:04:04 +00:00
uint32_t GetSize() const;
2013-01-18 23:41:08 +00:00
PythonObject
GetItemForKey (const PythonString &key) const;
const char *
2013-01-18 23:41:08 +00:00
GetItemForKeyAsString (const PythonString &key, const char *fail_value = NULL) const;
int64_t
2013-01-18 23:41:08 +00:00
GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value = 0) const;
2013-01-18 23:41:08 +00:00
PythonObject
GetItemForKey (const char *key) const;
2013-01-18 23:41:08 +00:00
typedef bool (*DictionaryIteratorCallback)(PythonString* key, PythonDictionary* dict);
2013-01-18 23:41:08 +00:00
PythonList
GetKeys () const;
2013-01-18 23:41:08 +00:00
PythonString
GetKeyAtPosition (uint32_t pos) const;
2013-01-18 23:41:08 +00:00
PythonObject
GetValueAtPosition (uint32_t pos) const;
void
SetItemForKey (const PythonString &key, PyObject *value);
void
2013-01-18 23:41:08 +00:00
SetItemForKey (const PythonString &key, const PythonObject& value);
2015-03-17 20:04:04 +00:00
StructuredData::DictionarySP CreateStructuredDictionary() const;
};
} // namespace lldb_private
#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H