ScummVM API documentation
sqclass.h
1 /* see copyright notice in squirrel.h */
2 #ifndef _SQCLASS_H_
3 #define _SQCLASS_H_
4 
5 struct SQInstance;
6 
7 struct SQClassMember {
8  SQObjectPtr val;
9  SQObjectPtr attrs;
10  void Null() {
11  val.Null();
12  attrs.Null();
13  }
14 };
15 
17 
18 #define MEMBER_TYPE_METHOD 0x01000000
19 #define MEMBER_TYPE_FIELD 0x02000000
20 
21 #define _ismethod(o) (_integer(o)&MEMBER_TYPE_METHOD)
22 #define _isfield(o) (_integer(o)&MEMBER_TYPE_FIELD)
23 #define _make_method_idx(i) ((SQInteger)(MEMBER_TYPE_METHOD|i))
24 #define _make_field_idx(i) ((SQInteger)(MEMBER_TYPE_FIELD|i))
25 #define _member_type(o) (_integer(o)&0xFF000000)
26 #define _member_idx(o) (_integer(o)&0x00FFFFFF)
27 
28 struct SQClass : public CHAINABLE_OBJ
29 {
30  SQClass(SQSharedState *ss,SQClass *base);
31 public:
32  static SQClass* Create(SQSharedState *ss,SQClass *base) {
33  SQClass *newclass = (SQClass *)SQ_MALLOC(sizeof(SQClass));
34  new (newclass) SQClass(ss, base);
35  return newclass;
36  }
37  ~SQClass();
38  bool NewSlot(SQSharedState *ss, const SQObjectPtr &key,const SQObjectPtr &val,bool bstatic);
39  bool Get(const SQObjectPtr &key,SQObjectPtr &val) {
40  if(_members->Get(key,val)) {
41  if(_isfield(val)) {
42  SQObjectPtr &o = _defaultvalues[_member_idx(val)].val;
43  val = _realval(o);
44  }
45  else {
46  val = _methods[_member_idx(val)].val;
47  }
48  return true;
49  }
50  return false;
51  }
52  bool GetConstructor(SQObjectPtr &ctor)
53  {
54  if(_constructoridx != -1) {
55  ctor = _methods[_constructoridx].val;
56  return true;
57  }
58  return false;
59  }
60  bool SetAttributes(const SQObjectPtr &key,const SQObjectPtr &val);
61  bool GetAttributes(const SQObjectPtr &key,SQObjectPtr &outval);
62  void Lock() { _locked = true; if(_base) _base->Lock(); }
63  void Release() {
64  if (_hook) { _hook(_typetag,0);}
65  sq_delete(this, SQClass);
66  }
67  void Finalize();
68  void FinalizeCore();
69 #ifndef NO_GARBAGE_COLLECTOR
70  void Mark(SQCollectable ** );
71  SQObjectType GetType() {return OT_CLASS;}
72 #endif
73  SQInteger Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval);
74  SQInstance *CreateInstance();
75  SQTable *_members;
76  SQClass *_base;
77  SQClassMemberVec _defaultvalues;
78  SQClassMemberVec _methods;
79  SQObjectPtr _metamethods[MT_LAST];
80  SQObjectPtr _attributes;
81  SQUserPointer _typetag;
82  SQRELEASEHOOK _hook;
83  bool _locked;
84  SQInteger _constructoridx;
85  SQInteger _udsize;
86 };
87 
88 #define calcinstancesize(_theclass_) \
89  (_theclass_->_udsize + sq_aligning(sizeof(SQInstance) + (sizeof(SQObjectPtr)*(_theclass_->_defaultvalues.size()>0?_theclass_->_defaultvalues.size()-1:0))))
90 
91 struct SQInstance : public SQDelegable
92 {
93  void Init(SQSharedState *ss);
94  SQInstance(SQSharedState *ss, SQClass *c, SQInteger memsize);
95  SQInstance(SQSharedState *ss, SQInstance *c, SQInteger memsize);
96 public:
97  static SQInstance* Create(SQSharedState *ss,SQClass *theclass) {
98 
99  SQInteger size = calcinstancesize(theclass);
100  SQInstance *newinst = (SQInstance *)SQ_MALLOC(size);
101  new (newinst) SQInstance(ss, theclass,size);
102  if(theclass->_udsize) {
103  newinst->_userpointer = ((unsigned char *)newinst) + (size - theclass->_udsize);
104  }
105  return newinst;
106  }
107  SQInstance *Clone(SQSharedState *ss)
108  {
109  SQInteger size = calcinstancesize(_class);
110  SQInstance *newinst = (SQInstance *)SQ_MALLOC(size);
111  new (newinst) SQInstance(ss, this,size);
112  if(_class->_udsize) {
113  newinst->_userpointer = ((unsigned char *)newinst) + (size - _class->_udsize);
114  }
115  return newinst;
116  }
117  ~SQInstance();
118  bool Get(const SQObjectPtr &key,SQObjectPtr &val) {
119  if(_class->_members->Get(key,val)) {
120  if(_isfield(val)) {
121  SQObjectPtr &o = _values[_member_idx(val)];
122  val = _realval(o);
123  }
124  else {
125  val = _class->_methods[_member_idx(val)].val;
126  }
127  return true;
128  }
129  return false;
130  }
131  bool Set(const SQObjectPtr &key,const SQObjectPtr &val) {
132  SQObjectPtr idx;
133  if(_class->_members->Get(key,idx) && _isfield(idx)) {
134  _values[_member_idx(idx)] = val;
135  return true;
136  }
137  return false;
138  }
139  void Release() {
140  _uiRef++;
141  if (_hook) { _hook(_userpointer,0);}
142  _uiRef--;
143  if(_uiRef > 0) return;
144  SQInteger size = _memsize;
145  this->~SQInstance();
146  SQ_FREE(this, size);
147  }
148  void Finalize();
149 #ifndef NO_GARBAGE_COLLECTOR
150  void Mark(SQCollectable ** );
151  SQObjectType GetType() {return OT_INSTANCE;}
152 #endif
153  bool InstanceOf(SQClass *trg);
154  bool GetMetaMethod(SQVM *v,SQMetaMethod mm,SQObjectPtr &res);
155 
156  SQClass *_class;
157  SQUserPointer _userpointer;
158  SQRELEASEHOOK _hook;
159  SQInteger _memsize;
160  SQObjectPtr _values[1];
161 };
162 
163 #endif //_SQCLASS_H_
Definition: sqvm.h:33
Definition: sqclass.h:28
Definition: sqtable.h:25
Definition: sqobject.h:339
Definition: sqstate.h:59
Definition: sqobject.h:205
Definition: sqclass.h:7
Definition: sqclass.h:91
Definition: sqobject.h:313