fix for squirrel on 64 bit
[supertux.git] / src / squirrel / squirrel / 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         SQClassMember(){}
9         SQClassMember(const SQClassMember &o) {
10                 val = o.val;
11                 attrs = o.attrs;
12         }
13         SQObjectPtr val;
14         SQObjectPtr attrs;
15 };
16
17 typedef sqvector<SQClassMember> SQClassMemberVec;
18
19 struct SQClass : public CHAINABLE_OBJ
20 {
21         SQClass(SQSharedState *ss,SQClass *base);
22 public:
23         static SQClass* Create(SQSharedState *ss,SQClass *base) {
24                 SQClass *newclass = (SQClass *)SQ_MALLOC(sizeof(SQClass));
25                 new (newclass) SQClass(ss, base);
26                 return newclass;
27         }
28         ~SQClass();
29         bool NewSlot(const SQObjectPtr &key,const SQObjectPtr &val);
30         bool Get(const SQObjectPtr &key,SQObjectPtr &val) {
31                 if(_members->Get(key,val)) {
32                         val = (type(val) == OT_INTEGER?_defaultvalues[_integer(val)].val:_methods[_integer(val)].val);
33                         return true;
34                 }
35                 return false;
36         }
37         bool SetAttributes(const SQObjectPtr &key,const SQObjectPtr &val);
38         bool GetAttributes(const SQObjectPtr &key,SQObjectPtr &outval);
39         void Lock() { _locked = true; if(_base) _base->Lock(); }
40         void Release() { sq_delete(this, SQClass);      }
41         void Finalize();
42         void Mark(SQCollectable ** );
43         int Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval);
44         SQInstance *CreateInstance();
45         SQTable *_members;
46         //SQTable *_properties;
47         SQClass *_base;
48         SQClassMemberVec _defaultvalues;
49         SQClassMemberVec _methods;
50         SQObjectPtrVec _metamethods;
51         SQObjectPtr _attributes;
52         unsigned int _typetag;
53         bool _locked;
54 };
55
56 struct SQInstance : public SQDelegable 
57 {
58         void Init(SQSharedState *ss);
59         SQInstance(SQSharedState *ss, SQClass *c);
60         SQInstance(SQSharedState *ss, SQInstance *c);
61 public:
62         static SQInstance* Create(SQSharedState *ss,SQClass *theclass) {
63                 SQInstance *newinst = (SQInstance *)SQ_MALLOC(sizeof(SQInstance));
64                 new (newinst) SQInstance(ss, theclass);
65                 return newinst;
66         }
67         SQInstance *Clone(SQSharedState *ss)
68         {
69                 SQInstance *newinst = (SQInstance *)SQ_MALLOC(sizeof(SQInstance));
70                 new (newinst) SQInstance(ss, this);
71                 return newinst;
72         }
73         ~SQInstance();
74         bool Get(const SQObjectPtr &key,SQObjectPtr &val)  {
75                 if(_class->_members->Get(key,val)) {
76                         val = (type(val) == OT_INTEGER?_values[_integer(val)]:_class->_methods[_integer(val)].val);
77                         return true;
78                 }
79                 return false;
80         }
81         bool Set(const SQObjectPtr &key,const SQObjectPtr &val) {
82                 SQObjectPtr idx;
83                 if(_class->_members->Get(key,idx) && type(idx) == OT_INTEGER) {
84             _values[_integer(idx)] = val;
85                         return true;
86                 }
87                 return false;
88         }
89         void Release() { 
90                 if (_hook) { _hook(_userpointer,0);}
91                 sq_delete(this, SQInstance);
92         }
93         void Finalize();
94         void Mark(SQCollectable ** );
95         bool InstanceOf(SQClass *trg);
96         bool GetMetaMethod(SQMetaMethod mm,SQObjectPtr &res);
97
98         SQClass *_class;
99         SQUserPointer _userpointer;
100         SQRELEASEHOOK _hook;
101         SQObjectPtrVec _values;
102 };
103
104 #endif //_SQCLASS_H_