ScummVM API documentation
sqobject.h
1 /* see copyright notice in squirrel.h */
2 #ifndef _SQOBJECT_H_
3 #define _SQOBJECT_H_
4 
5 #include "squtils.h"
6 
7 #ifdef _SQ64
8 #define UINT_MINUS_ONE (0xFFFFFFFFFFFFFFFF)
9 #else
10 #define UINT_MINUS_ONE (0xFFFFFFFF)
11 #endif
12 
13 #define SQ_CLOSURESTREAM_HEAD (('S'<<24)|('Q'<<16)|('I'<<8)|('R'))
14 #define SQ_CLOSURESTREAM_PART (('P'<<24)|('A'<<16)|('R'<<8)|('T'))
15 #define SQ_CLOSURESTREAM_TAIL (('T'<<24)|('A'<<16)|('I'<<8)|('L'))
16 
17 struct SQSharedState;
18 
19 enum SQMetaMethod{
20  MT_ADD=0,
21  MT_SUB=1,
22  MT_MUL=2,
23  MT_DIV=3,
24  MT_UNM=4,
25  MT_MODULO=5,
26  MT_SET=6,
27  MT_GET=7,
28  MT_TYPEOF=8,
29  MT_NEXTI=9,
30  MT_CMP=10,
31  MT_CALL=11,
32  MT_CLONED=12,
33  MT_NEWSLOT=13,
34  MT_DELSLOT=14,
35  MT_TOSTRING=15,
36  MT_NEWMEMBER=16,
37  MT_INHERITED=17,
38  MT_LAST = 18
39 };
40 
41 #define MM_ADD _SC("_add")
42 #define MM_SUB _SC("_sub")
43 #define MM_MUL _SC("_mul")
44 #define MM_DIV _SC("_div")
45 #define MM_UNM _SC("_unm")
46 #define MM_MODULO _SC("_modulo")
47 #define MM_SET _SC("_set")
48 #define MM_GET _SC("_get")
49 #define MM_TYPEOF _SC("_typeof")
50 #define MM_NEXTI _SC("_nexti")
51 #define MM_CMP _SC("_cmp")
52 #define MM_CALL _SC("_call")
53 #define MM_CLONED _SC("_cloned")
54 #define MM_NEWSLOT _SC("_newslot")
55 #define MM_DELSLOT _SC("_delslot")
56 #define MM_TOSTRING _SC("_tostring")
57 #define MM_NEWMEMBER _SC("_newmember")
58 #define MM_INHERITED _SC("_inherited")
59 
60 
61 #define _CONSTRUCT_VECTOR(type,size,ptr) { \
62  for(SQInteger n = 0; n < ((SQInteger)size); n++) { \
63  new (&ptr[n]) type(); \
64  } \
65 }
66 
67 #define _DESTRUCT_VECTOR(type,size,ptr) { \
68  for(SQInteger nl = 0; nl < ((SQInteger)size); nl++) { \
69  ptr[nl].~type(); \
70  } \
71 }
72 
73 #define _COPY_VECTOR(dest,src,size) { \
74  for(SQInteger _n_ = 0; _n_ < ((SQInteger)size); _n_++) { \
75  dest[_n_] = src[_n_]; \
76  } \
77 }
78 
79 #define _NULL_SQOBJECT_VECTOR(vec,size) { \
80  for(SQInteger _n_ = 0; _n_ < ((SQInteger)size); _n_++) { \
81  vec[_n_].Null(); \
82  } \
83 }
84 
85 #define MINPOWER2 4
86 
88 {
89  SQUnsignedInteger _uiRef;
90  struct SQWeakRef *_weakref;
91  SQRefCounted() { _uiRef = 0; _weakref = NULL; }
92  virtual ~SQRefCounted();
93  SQWeakRef *GetWeakRef(SQObjectType type);
94  virtual void Release()=0;
95 
96 };
97 
99 {
100  void Release();
101  SQObject _obj;
102 };
103 
104 #define _realval(o) (sq_type((o)) != OT_WEAKREF?(SQObject)o:_weakref(o)->_obj)
105 
106 struct SQObjectPtr;
107 
108 #define __AddRef(type,unval) if(ISREFCOUNTED(type)) \
109  { \
110  unval.pRefCounted->_uiRef++; \
111  }
112 
113 #define __Release(type,unval) if(ISREFCOUNTED(type) && ((--unval.pRefCounted->_uiRef)==0)) \
114  { \
115  unval.pRefCounted->Release(); \
116  }
117 
118 #define __ObjRelease(obj) { \
119  if((obj)) { \
120  (obj)->_uiRef--; \
121  if((obj)->_uiRef == 0) \
122  (obj)->Release(); \
123  (obj) = NULL; \
124  } \
125 }
126 
127 #define __ObjAddRef(obj) { \
128  (obj)->_uiRef++; \
129 }
130 
131 #define is_delegable(t) (sq_type(t)&SQOBJECT_DELEGABLE)
132 #define raw_type(obj) _RAW_TYPE((obj)._type)
133 
134 #define _integer(obj) ((obj)._unVal.nInteger)
135 #define _float(obj) ((obj)._unVal.fFloat)
136 #define _string(obj) ((obj)._unVal.pString)
137 #define _table(obj) ((obj)._unVal.pTable)
138 #define _array(obj) ((obj)._unVal.pArray)
139 #define _closure(obj) ((obj)._unVal.pClosure)
140 #define _generator(obj) ((obj)._unVal.pGenerator)
141 #define _nativeclosure(obj) ((obj)._unVal.pNativeClosure)
142 #define _userdata(obj) ((obj)._unVal.pUserData)
143 #define _userpointer(obj) ((obj)._unVal.pUserPointer)
144 #define _thread(obj) ((obj)._unVal.pThread)
145 #define _funcproto(obj) ((obj)._unVal.pFunctionProto)
146 #define _class(obj) ((obj)._unVal.pClass)
147 #define _instance(obj) ((obj)._unVal.pInstance)
148 #define _delegable(obj) ((SQDelegable *)(obj)._unVal.pDelegable)
149 #define _weakref(obj) ((obj)._unVal.pWeakRef)
150 #define _outer(obj) ((obj)._unVal.pOuter)
151 #define _refcounted(obj) ((obj)._unVal.pRefCounted)
152 #define _rawval(obj) ((obj)._unVal.raw)
153 
154 #define _stringval(obj) (obj)._unVal.pString->_val
155 #define _userdataval(obj) ((SQUserPointer)sq_aligning((obj)._unVal.pUserData + 1))
156 
157 #define tofloat(num) ((sq_type(num)==OT_FLOAT)?_float(num):(SQFloat)_integer(num))
158 #define tointeger(num) ((sq_type(num)==OT_FLOAT)?(SQInteger)_float(num):_integer(num))
159 #if defined(SQUSEDOUBLE) && !defined(_SQ64) || !defined(SQUSEDOUBLE) && defined(_SQ64)
162 #define SQ_REFOBJECT_INIT() SQ_OBJECT_RAWINIT()
163 #else
164 #define SQ_REFOBJECT_INIT()
165 #endif
166 
167 #define _REF_TYPE_DECL(type,_class,sym) \
168  SQObjectPtr(_class * x) \
169  { \
170  SQ_OBJECT_RAWINIT() \
171  _type=type; \
172  _unVal.sym = x; \
173  assert(_unVal.pTable); \
174  _unVal.pRefCounted->_uiRef++; \
175  } \
176  inline SQObjectPtr& operator=(_class *x) \
177  { \
178  SQObjectType tOldType; \
179  SQObjectValue unOldVal; \
180  tOldType=_type; \
181  unOldVal=_unVal; \
182  _type = type; \
183  SQ_REFOBJECT_INIT() \
184  _unVal.sym = x; \
185  _unVal.pRefCounted->_uiRef++; \
186  __Release(tOldType,unOldVal); \
187  return *this; \
188  }
189 
190 #define _SCALAR_TYPE_DECL(type,_class,sym) \
191  SQObjectPtr(_class x) \
192  { \
193  SQ_OBJECT_RAWINIT() \
194  _type=type; \
195  _unVal.sym = x; \
196  } \
197  inline SQObjectPtr& operator=(_class x) \
198  { \
199  __Release(_type,_unVal); \
200  _type = type; \
201  SQ_OBJECT_RAWINIT() \
202  _unVal.sym = x; \
203  return *this; \
204  }
205 struct SQObjectPtr : public SQObject
206 {
207  SQObjectPtr()
208  {
209  SQ_OBJECT_RAWINIT()
210  _type=OT_NULL;
211  _unVal.pUserPointer=NULL;
212  }
213  SQObjectPtr(const SQObjectPtr &o)
214  {
215  _type = o._type;
216  _unVal = o._unVal;
217  __AddRef(_type,_unVal);
218  }
219  SQObjectPtr(const SQObject &o)
220  {
221  _type = o._type;
222  _unVal = o._unVal;
223  __AddRef(_type,_unVal);
224  }
225  _REF_TYPE_DECL(OT_TABLE,SQTable,pTable)
226  _REF_TYPE_DECL(OT_CLASS,SQClass,pClass)
227  _REF_TYPE_DECL(OT_INSTANCE,SQInstance,pInstance)
228  _REF_TYPE_DECL(OT_ARRAY,SQArray,pArray)
229  _REF_TYPE_DECL(OT_CLOSURE,SQClosure,pClosure)
230  _REF_TYPE_DECL(OT_NATIVECLOSURE,SQNativeClosure,pNativeClosure)
231  _REF_TYPE_DECL(OT_OUTER,SQOuter,pOuter)
232  _REF_TYPE_DECL(OT_GENERATOR,SQGenerator,pGenerator)
233  _REF_TYPE_DECL(OT_STRING,SQString,pString)
234  _REF_TYPE_DECL(OT_USERDATA,SQUserData,pUserData)
235  _REF_TYPE_DECL(OT_WEAKREF,SQWeakRef,pWeakRef)
236  _REF_TYPE_DECL(OT_THREAD,SQVM,pThread)
237  _REF_TYPE_DECL(OT_FUNCPROTO,SQFunctionProto,pFunctionProto)
238 
239  _SCALAR_TYPE_DECL(OT_INTEGER,SQInteger,nInteger)
240  _SCALAR_TYPE_DECL(OT_FLOAT,SQFloat,fFloat)
241  _SCALAR_TYPE_DECL(OT_USERPOINTER,SQUserPointer,pUserPointer)
242 
243  SQObjectPtr(bool bBool)
244  {
245  SQ_OBJECT_RAWINIT()
246  _type = OT_BOOL;
247  _unVal.nInteger = bBool?1:0;
248  }
249  inline SQObjectPtr& operator=(bool b)
250  {
251  __Release(_type,_unVal);
252  SQ_OBJECT_RAWINIT()
253  _type = OT_BOOL;
254  _unVal.nInteger = b?1:0;
255  return *this;
256  }
257 
258  ~SQObjectPtr()
259  {
260  __Release(_type,_unVal);
261  }
262 
263  inline SQObjectPtr& operator=(const SQObjectPtr& obj)
264  {
265  SQObjectType tOldType;
266  SQObjectValue unOldVal;
267  tOldType=_type;
268  unOldVal=_unVal;
269  _unVal = obj._unVal;
270  _type = obj._type;
271  __AddRef(_type,_unVal);
272  __Release(tOldType,unOldVal);
273  return *this;
274  }
275  inline SQObjectPtr& operator=(const SQObject& obj)
276  {
277  SQObjectType tOldType;
278  SQObjectValue unOldVal;
279  tOldType=_type;
280  unOldVal=_unVal;
281  _unVal = obj._unVal;
282  _type = obj._type;
283  __AddRef(_type,_unVal);
284  __Release(tOldType,unOldVal);
285  return *this;
286  }
287  inline void Null()
288  {
289  SQObjectType tOldType = _type;
290  SQObjectValue unOldVal = _unVal;
291  _type = OT_NULL;
292  _unVal.raw = (SQRawObjectVal)NULL;
293  __Release(tOldType ,unOldVal);
294  }
295  private:
296  SQObjectPtr(const SQChar *){} //safety
297 };
298 
299 
300 inline void _Swap(SQObject &a,SQObject &b)
301 {
302  SQObjectType tOldType = a._type;
303  SQObjectValue unOldVal = a._unVal;
304  a._type = b._type;
305  a._unVal = b._unVal;
306  b._type = tOldType;
307  b._unVal = unOldVal;
308 }
309 
311 #ifndef NO_GARBAGE_COLLECTOR
312 #define MARK_FLAG 0x80000000
313 struct SQCollectable : public SQRefCounted {
314  SQCollectable *_next;
315  SQCollectable *_prev;
316  SQSharedState *_sharedstate;
317  virtual SQObjectType GetType()=0;
318  virtual void Release()=0;
319  virtual void Mark(SQCollectable **chain)=0;
320  void UnMark();
321  virtual void Finalize()=0;
322  static void AddToChain(SQCollectable **chain,SQCollectable *c);
323  static void RemoveFromChain(SQCollectable **chain,SQCollectable *c);
324 };
325 
326 
327 #define ADD_TO_CHAIN(chain,obj) AddToChain(chain,obj)
328 #define REMOVE_FROM_CHAIN(chain,obj) {if(!(_uiRef&MARK_FLAG))RemoveFromChain(chain,obj);}
329 #define CHAINABLE_OBJ SQCollectable
330 #define INIT_CHAIN() {_next=NULL;_prev=NULL;_sharedstate=ss;}
331 #else
332 
333 #define ADD_TO_CHAIN(chain,obj) ((void)0)
334 #define REMOVE_FROM_CHAIN(chain,obj) ((void)0)
335 #define CHAINABLE_OBJ SQRefCounted
336 #define INIT_CHAIN() ((void)0)
337 #endif
338 
339 struct SQDelegable : public CHAINABLE_OBJ {
340  bool SetDelegate(SQTable *m);
341  virtual bool GetMetaMethod(SQVM *v,SQMetaMethod mm,SQObjectPtr &res);
342  SQTable *_delegate;
343 };
344 
345 SQUnsignedInteger TranslateIndex(const SQObjectPtr &idx);
348 const SQChar *GetTypeName(const SQObjectPtr &obj1);
349 const SQChar *IdType2Name(SQObjectType type);
350 
351 
352 
353 #endif //_SQOBJECT_H_
Definition: sqclosure.h:10
Definition: sqvm.h:33
Definition: sqclass.h:28
Definition: sqtable.h:25
Definition: sqclosure.h:74
Definition: sqobject.h:339
Definition: sqclosure.h:108
Definition: sqobject.h:98
Definition: sqstate.h:59
Definition: sqobject.h:87
Definition: squirrel.h:153
Definition: squserdata.h:5
Definition: sqobject.h:205
Definition: sqclass.h:91
Definition: squirrel.h:129
Definition: sqstring.h:14
Definition: sqclosure.h:147
Definition: sqarray.h:5
Definition: sqfuncproto.h:49
Definition: sqobject.h:313