forked from zrax/pycdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyc_object.h
143 lines (118 loc) · 3.4 KB
/
pyc_object.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifndef _PYC_OBJECT_H
#define _PYC_OBJECT_H
template <class _Obj>
class PycRef {
public:
PycRef() : m_obj(0) { }
PycRef(_Obj* obj) : m_obj(obj)
{
if (m_obj)
m_obj->addRef();
}
PycRef(const PycRef<_Obj>& obj) : m_obj(obj.m_obj)
{
if (m_obj)
m_obj->addRef();
}
~PycRef<_Obj>()
{
if (m_obj)
m_obj->delRef();
}
PycRef<_Obj>& operator=(_Obj* obj)
{
if (obj)
obj->addRef();
if (m_obj)
m_obj->delRef();
m_obj = obj;
return *this;
}
PycRef<_Obj>& operator=(const PycRef<_Obj>& obj)
{
if (obj.m_obj)
obj.m_obj->addRef();
if (m_obj)
m_obj->delRef();
m_obj = obj.m_obj;
return *this;
}
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; }
inline int type() const;
/* This is just for coding convenience -- no type checking is done! */
template <class _Cast>
PycRef<_Cast> cast() const { return static_cast<_Cast*>(m_obj); }
bool isIdent(const _Obj* obj) { return m_obj == obj; }
private:
_Obj* m_obj;
};
class PycData;
class PycModule;
/* 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',
TYPE_OBREF = 'r',
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 = '>',
TYPE_ASCII = 'a',
TYPE_ASCII_INTERNED = 'A',
TYPE_SMALL_TUPLE = ')',
TYPE_SHORT_ASCII = 'z',
TYPE_SHORT_ASCII_INTERNED = 'Z',
};
PycObject(int type = TYPE_UNKNOWN) : m_refs(0), m_type(type) { }
virtual ~PycObject() { }
int type() const { return m_type; }
virtual bool isEqual(PycRef<PycObject> obj) const
{ return obj.isIdent(this); }
virtual void load(PycData*, PycModule*) { }
private:
int m_refs;
int m_type;
public:
void addRef() { ++m_refs; }
void delRef() { if (--m_refs == 0) delete this; }
};
template <class _Obj>
int PycRef<_Obj>::type() const
{ return m_obj ? m_obj->type() : PycObject::TYPE_NULL; }
PycRef<PycObject> CreateObject(int type);
PycRef<PycObject> LoadObject(PycData* stream, PycModule* mod);
/* Static Singleton objects */
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;
#endif