2009-07-24 08:35:21 +00:00
|
|
|
#ifndef _PYC_OBJECT_H
|
|
|
|
#define _PYC_OBJECT_H
|
|
|
|
|
2018-01-28 14:33:26 -08:00
|
|
|
#include <typeinfo>
|
|
|
|
|
2009-07-24 21:15:51 +00:00
|
|
|
template <class _Obj>
|
|
|
|
class PycRef {
|
|
|
|
public:
|
2019-10-08 08:42:33 -07:00
|
|
|
PycRef() noexcept : m_obj() { }
|
2011-10-23 00:21:17 -07:00
|
|
|
|
2019-10-08 08:42:33 -07:00
|
|
|
PycRef(_Obj* obj) noexcept : m_obj(obj)
|
2011-10-23 00:21:17 -07:00
|
|
|
{
|
2014-01-21 00:07:34 -08:00
|
|
|
if (m_obj)
|
2011-10-23 00:21:17 -07:00
|
|
|
m_obj->addRef();
|
|
|
|
}
|
|
|
|
|
2019-10-08 08:42:33 -07:00
|
|
|
PycRef(const PycRef<_Obj>& obj) noexcept : m_obj(obj.m_obj)
|
2011-10-23 00:21:17 -07:00
|
|
|
{
|
2014-01-21 00:07:34 -08:00
|
|
|
if (m_obj)
|
2011-10-23 00:21:17 -07:00
|
|
|
m_obj->addRef();
|
|
|
|
}
|
|
|
|
|
2019-10-08 08:42:33 -07:00
|
|
|
PycRef(PycRef<_Obj>&& obj) noexcept : m_obj(obj.m_obj)
|
|
|
|
{
|
|
|
|
obj.m_obj = nullptr;
|
|
|
|
}
|
|
|
|
|
2011-10-23 00:21:17 -07:00
|
|
|
~PycRef<_Obj>()
|
|
|
|
{
|
2014-01-21 00:07:34 -08:00
|
|
|
if (m_obj)
|
2011-10-23 00:21:17 -07:00
|
|
|
m_obj->delRef();
|
2010-12-24 20:25:55 -08:00
|
|
|
}
|
2009-07-24 21:15:51 +00:00
|
|
|
|
|
|
|
PycRef<_Obj>& operator=(_Obj* obj)
|
|
|
|
{
|
2011-10-23 00:21:17 -07:00
|
|
|
if (obj)
|
|
|
|
obj->addRef();
|
|
|
|
if (m_obj)
|
|
|
|
m_obj->delRef();
|
2009-07-24 21:15:51 +00:00
|
|
|
m_obj = obj;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PycRef<_Obj>& operator=(const PycRef<_Obj>& obj)
|
|
|
|
{
|
2011-10-23 00:21:17 -07:00
|
|
|
if (obj.m_obj)
|
|
|
|
obj.m_obj->addRef();
|
|
|
|
if (m_obj)
|
|
|
|
m_obj->delRef();
|
2009-07-24 21:15:51 +00:00
|
|
|
m_obj = obj.m_obj;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-10-08 08:42:33 -07:00
|
|
|
PycRef<_Obj>& operator=(PycRef<_Obj>&& obj) noexcept
|
|
|
|
{
|
|
|
|
m_obj = obj.m_obj;
|
|
|
|
obj.m_obj = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-07-24 21:15:51 +00:00
|
|
|
bool operator==(_Obj* obj) const { return m_obj == obj; }
|
|
|
|
bool operator==(const PycRef<_Obj>& obj) const { return m_obj == obj.m_obj; }
|
|
|
|
bool operator!=(_Obj* obj) const { return m_obj != obj; }
|
|
|
|
bool operator!=(const PycRef<_Obj>& obj) const { return m_obj != obj.m_obj; }
|
|
|
|
|
|
|
|
_Obj& operator*() const { return *m_obj; }
|
|
|
|
_Obj* operator->() const { return m_obj; }
|
|
|
|
operator _Obj*() const { return m_obj; }
|
|
|
|
|
2017-07-05 16:36:04 -07:00
|
|
|
inline int type() const;
|
|
|
|
|
2009-07-24 21:15:51 +00:00
|
|
|
template <class _Cast>
|
2022-12-01 16:13:31 -08:00
|
|
|
PycRef<_Cast> try_cast() const { return dynamic_cast<_Cast*>(m_obj); }
|
2018-01-28 14:33:26 -08:00
|
|
|
|
|
|
|
template <class _Cast>
|
2022-12-01 16:13:31 -08:00
|
|
|
PycRef<_Cast> cast() const
|
2018-01-28 14:33:26 -08:00
|
|
|
{
|
|
|
|
_Cast* result = dynamic_cast<_Cast*>(m_obj);
|
|
|
|
if (!result)
|
|
|
|
throw std::bad_cast();
|
|
|
|
return result;
|
|
|
|
}
|
2009-07-24 21:15:51 +00:00
|
|
|
|
2018-01-28 14:33:26 -08:00
|
|
|
bool isIdent(const _Obj* obj) const { return m_obj == obj; }
|
2017-07-05 16:36:04 -07:00
|
|
|
|
2009-07-24 21:15:51 +00:00
|
|
|
private:
|
|
|
|
_Obj* m_obj;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-01-21 00:07:34 -08:00
|
|
|
class PycData;
|
|
|
|
class PycModule;
|
|
|
|
|
2009-07-24 08:35:21 +00:00
|
|
|
/* Please only hold PycObjects inside PycRefs! */
|
|
|
|
class PycObject {
|
|
|
|
public:
|
|
|
|
enum Type {
|
|
|
|
// From the Python Marshallers
|
|
|
|
TYPE_NULL = '0',
|
|
|
|
TYPE_NONE = 'N',
|
|
|
|
TYPE_FALSE = 'F',
|
|
|
|
TYPE_TRUE = 'T',
|
|
|
|
TYPE_STOPITER = 'S',
|
|
|
|
TYPE_ELLIPSIS = '.',
|
|
|
|
TYPE_INT = 'i',
|
|
|
|
TYPE_INT64 = 'I',
|
|
|
|
TYPE_FLOAT = 'f',
|
|
|
|
TYPE_BINARY_FLOAT = 'g',
|
|
|
|
TYPE_COMPLEX = 'x',
|
|
|
|
TYPE_BINARY_COMPLEX = 'y',
|
|
|
|
TYPE_LONG = 'l',
|
|
|
|
TYPE_STRING = 's',
|
|
|
|
TYPE_INTERNED = 't',
|
|
|
|
TYPE_STRINGREF = 'R',
|
2014-01-21 00:07:34 -08:00
|
|
|
TYPE_OBREF = 'r',
|
2009-07-24 08:35:21 +00:00
|
|
|
TYPE_TUPLE = '(',
|
|
|
|
TYPE_LIST = '[',
|
|
|
|
TYPE_DICT = '{',
|
|
|
|
TYPE_CODE = 'c',
|
|
|
|
TYPE_CODE2 = 'C', // Used in Python 1.0 - 1.2
|
|
|
|
TYPE_UNICODE = 'u',
|
|
|
|
TYPE_UNKNOWN = '?',
|
|
|
|
TYPE_SET = '<',
|
|
|
|
TYPE_FROZENSET = '>',
|
2014-01-21 00:07:34 -08:00
|
|
|
TYPE_ASCII = 'a',
|
|
|
|
TYPE_ASCII_INTERNED = 'A',
|
|
|
|
TYPE_SMALL_TUPLE = ')',
|
|
|
|
TYPE_SHORT_ASCII = 'z',
|
|
|
|
TYPE_SHORT_ASCII_INTERNED = 'Z',
|
2009-07-24 08:35:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
PycObject(int type = TYPE_UNKNOWN) : m_refs(0), m_type(type) { }
|
|
|
|
virtual ~PycObject() { }
|
|
|
|
|
2017-07-05 16:36:04 -07:00
|
|
|
int type() const { return m_type; }
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2009-07-24 21:15:51 +00:00
|
|
|
virtual bool isEqual(PycRef<PycObject> obj) const
|
2019-10-02 16:01:54 -07:00
|
|
|
{
|
|
|
|
return obj.isIdent(this);
|
|
|
|
}
|
2009-07-24 21:15:51 +00:00
|
|
|
|
2014-01-21 00:07:34 -08:00
|
|
|
virtual void load(PycData*, PycModule*) { }
|
2009-07-24 08:35:21 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int m_refs;
|
2022-12-01 11:42:31 -08:00
|
|
|
|
|
|
|
protected:
|
2009-07-24 08:35:21 +00:00
|
|
|
int m_type;
|
|
|
|
|
|
|
|
public:
|
2014-01-21 00:07:34 -08:00
|
|
|
void addRef() { ++m_refs; }
|
|
|
|
void delRef() { if (--m_refs == 0) delete this; }
|
2009-07-24 08:35:21 +00:00
|
|
|
};
|
|
|
|
|
2017-07-05 16:36:04 -07:00
|
|
|
template <class _Obj>
|
|
|
|
int PycRef<_Obj>::type() const
|
2019-10-08 08:42:33 -07:00
|
|
|
{
|
|
|
|
return m_obj ? m_obj->type() : PycObject::TYPE_NULL;
|
|
|
|
}
|
2017-07-05 16:36:04 -07:00
|
|
|
|
2009-07-24 21:15:51 +00:00
|
|
|
PycRef<PycObject> CreateObject(int type);
|
2014-01-21 00:07:34 -08:00
|
|
|
PycRef<PycObject> LoadObject(PycData* stream, PycModule* mod);
|
2009-07-24 08:35:21 +00:00
|
|
|
|
|
|
|
/* Static Singleton objects */
|
2009-07-24 21:15:51 +00:00
|
|
|
extern PycRef<PycObject> Pyc_None;
|
|
|
|
extern PycRef<PycObject> Pyc_Ellipsis;
|
|
|
|
extern PycRef<PycObject> Pyc_StopIteration;
|
|
|
|
extern PycRef<PycObject> Pyc_False;
|
|
|
|
extern PycRef<PycObject> Pyc_True;
|
2009-07-24 08:35:21 +00:00
|
|
|
|
|
|
|
#endif
|