2 see copyright notice in squirrel.h
\r
4 #include "sqpcheader.h"
\r
8 #include "sqclosure.h"
\r
10 SQClass::SQClass(SQSharedState *ss,SQClass *base)
\r
15 _metamethods.resize(MT_LAST); //size it to max size
\r
17 _defaultvalues.copy(base->_defaultvalues);
\r
18 _methods.copy(base->_methods);
\r
19 _metamethods.copy(base->_metamethods);
\r
22 _members = base?base->_members->Clone() : SQTable::Create(ss,0);
\r
23 __ObjAddRef(_members);
\r
26 ADD_TO_CHAIN(&_sharedstate->_gc_chain, this);
\r
29 void SQClass::Finalize() {
\r
30 _attributes = _null_;
\r
31 _defaultvalues.resize(0);
\r
33 _metamethods.resize(0);
\r
34 __ObjRelease(_members);
\r
36 __ObjRelease(_base);
\r
42 REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain, this);
\r
46 bool SQClass::NewSlot(const SQObjectPtr &key,const SQObjectPtr &val)
\r
50 return false; //the slot already exists
\r
51 if(_members->Get(key,temp) && type(temp) == OT_INTEGER) //overrides the default value
\r
53 _defaultvalues[_integer(temp)].val = val;
\r
56 if(type(val) == OT_CLOSURE || type(val) == OT_NATIVECLOSURE) {
\r
58 if((mmidx = _sharedstate->GetMetaMethodIdxByName(key)) != -1) {
\r
59 _metamethods[mmidx] = val;
\r
62 if(type(temp) == OT_NULL) {
\r
65 _members->NewSlot(key,SQObjectPtr((SQUserPointer)_methods.size()));
\r
66 _methods.push_back(m);
\r
69 _methods[(int)_userpointer(temp)].val = val;
\r
76 _members->NewSlot(key,SQObjectPtr((SQInteger)_defaultvalues.size()));
\r
77 _defaultvalues.push_back(m);
\r
81 SQInstance *SQClass::CreateInstance()
\r
83 if(!_locked) Lock();
\r
84 return SQInstance::Create(_opt_ss(this),this);
\r
87 int SQClass::Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval)
\r
90 int idx = _members->Next(refpos,outkey,oval);
\r
92 if(type(oval) != OT_INTEGER) {
\r
93 outval = _methods[(int)_userpointer(oval)].val;
\r
96 outval = _defaultvalues[_integer(oval)].val;
\r
102 bool SQClass::SetAttributes(const SQObjectPtr &key,const SQObjectPtr &val)
\r
105 if(_members->Get(key,idx)) {
\r
106 if(type(idx) == OT_INTEGER)
\r
107 _defaultvalues[_integer(idx)].attrs = val;
\r
109 _methods[(int)_userpointer(idx)].attrs = val;
\r
115 bool SQClass::GetAttributes(const SQObjectPtr &key,SQObjectPtr &outval)
\r
118 if(_members->Get(key,idx)) {
\r
119 outval = (type(idx) == OT_INTEGER?_defaultvalues[_integer(idx)].attrs:_methods[(int)_userpointer(idx)].attrs);
\r
125 ///////////////////////////////////////////////////////////////////////
\r
126 void SQInstance::Init(SQSharedState *ss)
\r
129 _userpointer = NULL;
\r
131 __ObjAddRef(_class);
\r
132 _delegate = _class->_members;
\r
134 ADD_TO_CHAIN(&_sharedstate->_gc_chain, this);
\r
137 SQInstance::SQInstance(SQSharedState *ss, SQClass *c)
\r
140 _values.resize(_class->_defaultvalues.size());
\r
141 for(unsigned int i = 0; i < _class->_defaultvalues.size(); i++) {
\r
142 _values[i] = _class->_defaultvalues[i].val;
\r
147 SQInstance::SQInstance(SQSharedState *ss, SQInstance *i)
\r
149 _class = i->_class;
\r
150 _values.copy(i->_values);
\r
154 void SQInstance::Finalize()
\r
156 __ObjRelease(_class);
\r
160 SQInstance::~SQInstance()
\r
162 REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain, this);
\r
166 bool SQInstance::GetMetaMethod(SQMetaMethod mm,SQObjectPtr &res)
\r
168 if(type(_class->_metamethods[mm]) != OT_NULL) {
\r
169 res = _class->_metamethods[mm];
\r
175 bool SQInstance::InstanceOf(SQClass *trg)
\r
177 SQClass *parent = _class;
\r
178 while(parent != NULL) {
\r
181 parent = parent->_base;
\r