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