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