initial import
[panda.git] / goo / st-object.h
blobd29c7afc8a6b7957b8c106e11e5e87bf41ef15e9
1 /*
2 * st-object.h
4 * Copyright (C) 2008 Vincent Geddes
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #ifndef __ST_OBJECT_H__
26 #define __ST_OBJECT_H__
28 #include <st-types.h>
29 #include <st-small-integer.h>
30 #include <st-utils.h>
31 #include <st-mini.h>
32 #include <st-class.h>
33 #include <st-universe.h>
34 #include <glib.h>
36 st_oop_t st_object_new (st_oop_t klass);
37 st_oop_t st_object_new_arrayed (st_oop_t klass, st_smi_t size);
39 void st_object_initialize_header (st_oop_t object, st_oop_t klass);
40 void st_object_initialize_body (st_oop_t object, st_smi_t instance_size);
42 /* tag tests */
43 INLINE int st_object_tag (st_oop_t object);
44 INLINE bool st_object_is_mark (st_oop_t object);
45 INLINE bool st_object_is_heap (st_oop_t object);
46 INLINE bool st_object_is_smi (st_oop_t object);
49 INLINE st_oop_t st_object_class (st_oop_t object);
50 INLINE guint st_object_hash (st_oop_t object);
51 INLINE st_vtable_t *st_object_virtual (st_oop_t object);
52 INLINE st_vtable_t *st_object_super_virtual (st_oop_t object);
53 INLINE bool st_object_equal (st_oop_t object, st_oop_t another);
54 INLINE bool st_object_verify (st_oop_t object);
56 /* type tests */
57 INLINE bool st_object_is_class (st_oop_t object);
58 INLINE bool st_object_is_metaclass (st_oop_t object);
59 INLINE bool st_object_is_association (st_oop_t object);
60 INLINE bool st_object_is_symbol (st_oop_t object);
61 INLINE bool st_object_is_compiled_method (st_oop_t object);
62 INLINE bool st_object_is_compiled_block (st_oop_t object);
63 INLINE bool st_object_is_block_closure (st_oop_t object);
64 INLINE bool st_object_is_method_context (st_oop_t object);
65 INLINE bool st_object_is_block_context (st_oop_t object);
66 INLINE bool st_object_is_arrayed (st_oop_t object);
67 INLINE bool st_object_is_array (st_oop_t object);
68 INLINE bool st_object_is_byte_array (st_oop_t object);
70 st_vtable_t *st_object_vtable (void);
73 /* inline definitions */
75 INLINE int
76 st_object_tag (st_oop_t object)
78 return object & st_tag_mask;
81 INLINE bool
82 st_object_is_mark (st_oop_t object)
84 return st_object_tag (object) == ST_MARK_TAG;
87 INLINE bool
88 st_object_is_heap (st_oop_t object)
90 return st_object_tag (object) == ST_POINTER_TAG;
93 INLINE bool
94 st_object_is_smi (st_oop_t object)
96 return st_object_tag (object) == ST_SMI_TAG;
101 INLINE st_oop_t
102 st_object_class (st_oop_t object)
104 if (G_UNLIKELY (st_object_is_smi (object)))
105 return st_smi_class;
107 return st_heap_object_class (object);
110 INLINE st_vtable_t *
111 st_object_virtual (st_oop_t object)
113 if (G_UNLIKELY (st_object_is_smi (object)))
114 return st_smi_vtable ();
116 return (ST_CLASS_VTABLE (st_object_class (object)));
119 INLINE st_vtable_t *
120 st_object_super_virtual (st_oop_t object)
122 if (G_UNLIKELY (st_object_is_smi (object)))
123 return st_smi_vtable ()->parent_table;
125 return ST_CLASS_VTABLE (st_object_class (object))->parent_table;
129 INLINE bool
130 st_object_equal (st_oop_t object, st_oop_t another)
132 return st_object_virtual (object)->equal (object, another);
135 INLINE guint
136 st_object_hash (st_oop_t object)
138 return st_object_virtual (object)->hash (object);
142 INLINE bool
143 st_object_verify (st_oop_t object)
145 return st_object_virtual (object)->verify (object);
148 INLINE char *
149 st_object_describe (st_oop_t object)
151 return st_object_virtual (object)->describe (object);
154 /* type tests
156 * We delegate the test to the vtable methods stored in object's class object
160 INLINE bool
161 st_object_is_class (st_oop_t object)
163 return st_object_is_heap (object) &&
164 st_object_virtual (object)->is_class ();
167 INLINE bool
168 st_object_is_metaclass (st_oop_t object)
170 return st_object_is_heap (object) &&
171 st_object_virtual (object)->is_metaclass ();
174 INLINE bool
175 st_object_is_association (st_oop_t object)
177 return st_object_is_heap (object) &&
178 st_object_virtual (object)->is_association ();
181 INLINE bool
182 st_object_is_symbol (st_oop_t object)
184 return st_object_is_heap (object) &&
185 st_object_virtual (object)->is_symbol ();
188 INLINE bool
189 st_object_is_float (st_oop_t object)
191 return st_object_is_heap (object) &&
192 st_object_virtual (object)->is_float ();
195 INLINE bool
196 st_object_is_dictionary (st_oop_t object)
198 return st_object_is_heap (object) &&
199 st_object_virtual (object)->is_dictionary ();
202 INLINE bool
203 st_object_is_set (st_oop_t object)
205 return st_object_is_heap (object) &&
206 st_object_virtual (object)->is_set ();
209 INLINE bool
210 st_object_is_compiled_method (st_oop_t object)
212 return st_object_is_heap (object) &&
213 st_object_virtual (object)->is_compiled_method ();
216 INLINE bool
217 st_object_is_compiled_block (st_oop_t object)
219 return st_object_is_heap (object) &&
220 st_object_virtual (object)->is_compiled_block ();
223 INLINE bool
224 st_object_is_block_closure (st_oop_t object)
226 return st_object_is_heap (object) &&
227 st_object_virtual (object)->is_block_closure ();
230 INLINE bool
231 st_object_is_method_context (st_oop_t object)
233 return st_object_is_heap (object) &&
234 st_object_virtual (object)->is_method_context ();
237 INLINE bool
238 st_object_is_block_context (st_oop_t object)
240 return st_object_is_heap (object) &&
241 st_object_virtual (object)->is_block_context ();
244 INLINE bool
245 st_object_is_arrayed (st_oop_t object)
247 return st_object_is_heap (object) &&
248 st_object_virtual (object)->is_arrayed ();
251 INLINE bool
252 st_object_is_array (st_oop_t object)
254 return st_object_is_heap (object) &&
255 st_object_virtual (object)->is_array ();
258 INLINE bool
259 st_object_is_byte_array (st_oop_t object)
261 return st_object_is_heap (object) &&
262 st_object_virtual (object)->is_byte_array ();
265 #endif /* __ST_OBJECT_H__ */