-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathObject.h
More file actions
348 lines (279 loc) · 9.94 KB
/
Copy pathObject.h
File metadata and controls
348 lines (279 loc) · 9.94 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#ifndef HX_OBJECT_H
#define HX_OBJECT_H
#ifndef HXCPP_H
#error "Please include hxcpp.h, not hx/Object.h"
#endif
#ifdef HXCPP_TELEMETRY
extern void __hxt_gc_new(void* obj, int inSize);
#endif
// --- Constants -------------------------------------------------------
enum ObjectType
{
vtUnknown = -1,
vtInt = 0xff,
vtNull = 0,
vtFloat = 1,
vtBool = 2,
vtString = 3,
vtObject = 4,
vtArray = 5,
vtFunction = 6,
vtEnum,
vtClass,
vtAbstractBase = 0x100,
};
namespace hx
{
class FieldRef;
class IndexRef;
typedef Array<Dynamic> DynamicArray;
HXCPP_EXTERN_CLASS_ATTRIBUTES null BadCast();
#ifdef HXCPP_SCRIPTABLE
typedef void (*StackExecute)(struct CppiaCtx *ctx);
struct ScriptFunction
{
ScriptFunction(StackExecute inExe=0,const char *inSig=0)
: execute(inExe), signature(inSig) { }
StackExecute execute;
const char *signature;
};
struct ScriptCallable;
#endif
enum NewObjectType
{
NewObjAlloc,
NewObjContainer,
NewObjConst,
};
// --- hx::Object ------------------------------------------------------------
//
// Base for all hxcpp objects.
// This contains the virtual functions required by the core to provide
// a generic interface to the specific classes.
//
// Hxcpp classes inherit from this.
//
class HXCPP_EXTERN_CLASS_ATTRIBUTES Object
{
public:
// These allocate the function using the garbage-colleced malloc
inline void *operator new( size_t inSize, bool inContainer=true, const char *inName=0 )
{
#ifdef HX_USE_INLINE_IMMIX_OPERATOR_NEW
ImmixAllocator *alloc = hx::gMultiThreadMode ? tlsImmixAllocator : gMainThreadAlloc;
#ifdef HXCPP_DEBUG
if (!alloc)
BadImmixAlloc();
#endif
#ifndef HXCPP_ALIGN_ALLOC
// Inline the fast-path if we can
// We know the object can hold a pointer (vtable) and that the size is int-aligned
int start = alloc->spaceStart;
int end = start + sizeof(int) + inSize;
if ( end <= (alloc->spaceEnd WITH_PAUSE_FOR_COLLECT_FLAG ) )
{
alloc->spaceStart = end;
int startRow = start>>IMMIX_LINE_BITS;
alloc->allocStartFlags[ startRow ] |= gImmixStartFlag[start&127];
//alloc->allocBase[ startRow ] |= (1<<( (start>>2) & 31) );
unsigned int *buffer = (unsigned int *)(alloc->allocBase + start);
if (inContainer)
*buffer++ = (( (end+(IMMIX_LINE_LEN-1))>>IMMIX_LINE_BITS) -startRow) |
(inSize<<IMMIX_ALLOC_SIZE_SHIFT) |
gMarkIDWithContainer;
else
*buffer++ = (( (end+(IMMIX_LINE_LEN-1))>>IMMIX_LINE_BITS) -startRow) |
(inSize<<IMMIX_ALLOC_SIZE_SHIFT) |
gMarkID;
#ifdef HXCPP_TELEMETRY
__hxt_gc_new(buffer, inSize);
#endif
return buffer;
}
#endif // HXCPP_ALIGN_ALLOC
// Fall back to external method
void *result = alloc->CallAlloc(inSize, inContainer ? IMMIX_ALLOC_IS_CONTAINER : 0);
#else // Not HX_USE_INLINE_IMMIX_OPERATOR_NEW ...
void *result = hx::InternalNew(inSize,inContainer);
#endif
#ifdef HXCPP_TELEMETRY
__hxt_gc_new(result, inSize);
#endif
return result;
}
inline void *operator new( size_t inSize, hx::NewObjectType inType, const char *inName=0 )
{
if (inType==NewObjConst)
return InternalCreateConstBuffer(0,(int)inSize);
return operator new(inSize, inType==NewObjContainer, inName);
}
void operator delete( void *, bool) { }
void operator delete( void *, bool, const char * ) { }
void operator delete( void *, hx::NewObjectType) { }
void operator delete( void *, hx::NewObjectType, const char * ) { }
//virtual void *__root();
virtual void __Mark(hx::MarkContext *__inCtx) { }
#ifdef HXCPP_VISIT_ALLOCS
virtual void __Visit(hx::VisitContext *__inCtx) { }
#endif
virtual bool __Is(hx::Object *inClass) const { return true; }
virtual hx::Object *__ToInterface(const hx::type_info &inInterface) { return 0; }
virtual hx::Object *__GetRealObject() { return this; }
// helpers...
bool __Is(Dynamic inClass ) const;
inline bool __IsArray() const { return __GetType()==vtArray; }
virtual int __GetType() const { return vtClass; }
virtual void *__GetHandle() const { return 0; }
virtual hx::FieldRef __FieldRef(const String &inString);
virtual String __ToString() const;
virtual int __ToInt() const { return 0; }
virtual double __ToDouble() const { return 0.0; }
virtual const char * __CStr() const;
virtual String toString();
virtual bool __HasField(const String &inString);
virtual Dynamic __Field(const String &inString, hx::PropertyAccess inCallProp);
virtual Dynamic __IField(int inFieldID);
virtual double __INumField(int inFieldID);
virtual Dynamic __SetField(const String &inField,const Dynamic &inValue, hx::PropertyAccess inCallProp);
virtual void __SetThis(Dynamic inThis);
virtual Dynamic __Run(const Array<Dynamic> &inArgs);
virtual Dynamic *__GetFieldMap();
virtual void __GetFields(Array<String> &outFields);
virtual hx::Class __GetClass() const;
virtual int __Compare(const hx::Object *inRHS) const;
virtual DynamicArray __EnumParams();
virtual String __Tag() const;
virtual int __Index() const;
virtual int __length() const { return 0; }
virtual Dynamic __GetItem(int inIndex) const;
virtual Dynamic __SetItem(int inIndex,Dynamic inValue);
virtual void __SetSize(int inLen) { }
typedef const Dynamic &D;
virtual Dynamic __run();
virtual Dynamic __run(D a);
virtual Dynamic __run(D a,D b);
virtual Dynamic __run(D a,D b,D c);
virtual Dynamic __run(D a,D b,D c,D d);
virtual Dynamic __run(D a,D b,D c,D d,D e);
virtual int __ArgCount() const { return -1; }
#ifdef HXCPP_SCRIPTABLE
virtual void **__GetScriptVTable() { return 0; }
virtual hx::ScriptCallable *__GetScriptCallable() { return 0; }
static hx::ScriptFunction __script_construct;
#endif
inline bool __compare( hx::Object *inRHS )
{ return __GetRealObject()!=inRHS->__GetRealObject(); }
static hx::Class &__SGetClass();
static void __boot();
};
// --- hx::ObjectPtr ---------------------------------------------------------------
//
// This class simply provides syntax so that pointers can be written as objects,
// and overloaded operators can be used
template<typename OBJ_>
class ObjectPtr
{
inline bool SetPtr(OBJ_ *inPtr)
{
mPtr = inPtr;
return true;
}
inline bool SetPtr(...) { return false; }
inline void CastPtr(hx::Object *inPtr,bool inThrowOnInvalid)
{
if (inPtr)
{
mPtr = dynamic_cast<OBJ_ *>(inPtr->__GetRealObject());
if (!mPtr)
mPtr = (Ptr)inPtr->__ToInterface(typeid(Obj));
if (inThrowOnInvalid && !mPtr)
::hx::BadCast();
}
else
mPtr = 0;
}
public:
typedef OBJ_ Obj;
typedef OBJ_ *Ptr;
inline ObjectPtr() : mPtr(0) { }
inline ObjectPtr(OBJ_ *inObj) : mPtr(inObj) { }
inline ObjectPtr(const null &inNull) : mPtr(0) { }
inline ObjectPtr(const ObjectPtr<OBJ_> &inOther) : mPtr( inOther.mPtr ) { }
template<typename SOURCE_>
inline ObjectPtr(const ObjectPtr<SOURCE_> &inObjectPtr)
{
if (!SetPtr(inObjectPtr.mPtr))
#ifdef HXCPP_STRICT_CASTS
CastPtr(inObjectPtr.mPtr,true);
#else
CastPtr(inObjectPtr.mPtr,false);
#endif
}
template<typename SOURCE_>
inline ObjectPtr(const SOURCE_ *inPtr,bool inCheckCast=true)
{
if (!SetPtr(const_cast<SOURCE_ *>(inPtr)))
CastPtr(const_cast<SOURCE_ *>(inPtr),inCheckCast);
}
inline ObjectPtr &operator=(const null &inNull) { mPtr = 0; return *this; }
inline ObjectPtr &operator=(Ptr inRHS) { mPtr = inRHS; return *this; }
inline ObjectPtr &operator=(const ObjectPtr &inRHS) { mPtr = inRHS.mPtr; return *this; }
template<typename InterfaceImpl>
inline ObjectPtr &operator=(InterfaceImpl *inRHS)
{
mPtr = inRHS->operator Ptr();
return *this;
}
inline OBJ_ *GetPtr() const { return mPtr; }
inline OBJ_ *operator->()
{
#ifdef HXCPP_CHECK_POINTER
if (!mPtr) NullReference("Object", true);
// The handler might have fixed up the null value
if (!mPtr) NullReference("Object", false);
#ifdef HXCPP_GC_CHECK_POINTER
GCCheckPointer(mPtr);
#endif
#endif
return mPtr;
}
inline const OBJ_ *operator->() const
{
#ifdef HXCPP_CHECK_POINTER
if (!mPtr) NullReference("Object", true);
// The handler might have fixed up the null value
if (!mPtr) NullReference("Object", false);
#ifdef HXCPP_GC_CHECK_POINTER
GCCheckPointer(mPtr);
#endif
#endif
return mPtr;
}
template<typename T>
inline bool operator==(const T &inTRHS) const
{
ObjectPtr inRHS(inTRHS.mPtr,false);
if (mPtr==inRHS.mPtr) return true;
if (!mPtr || !inRHS.mPtr) return false;
return !mPtr->__compare(inRHS.mPtr);
}
template<typename T>
inline bool operator!=(const T &inTRHS) const
{
ObjectPtr inRHS(inTRHS.mPtr,false);
if (mPtr==inRHS.mPtr) return false;
if (!mPtr || !inRHS.mPtr) return true;
return mPtr->__compare(inRHS.mPtr);
}
inline bool operator==(const null &inRHS) const { return mPtr==0; }
inline bool operator!=(const null &inRHS) const { return mPtr!=0; }
//inline bool operator==(const Dynamic &inRHS) const { return inRHS==*this; }
//inline bool operator!=(const Dynamic &inRHS) const { return inRHS!=*this; }
// This is defined in the "FieldRef" class...
inline class hx::FieldRef FieldRef(const String &inString);
inline class hx::IndexRef IndexRef(int inString);
inline static hx::Class &__SGetClass() { return OBJ_::__SGetClass(); }
OBJ_ *mPtr;
};
} // end namespace hx
#endif