Debian Codegraph @

terps/tads/tads3/vmiter.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmiter.h - Iterator metaclass
12 Function
13
14 Notes
15
16 Modified
17 04/22/00 MJRoberts - Creation
18 */
19
20 #ifndef VMITER_H
21 #define VMITER_H
22
23 #include <stdlib.h>
24 #include "vmtype.h"
25 #include "vmobj.h"
26 #include "vmglob.h"
27
28 /* ------------------------------------------------------------------------ */
29 /*
30 * Base Iterator class
31 */
32 class CVmObjIter: public CVmObject
33 {
34 friend class CVmMetaclassIter;
35
36 public:
37 /* metaclass registration object */
38 static class CVmMetaclass *metaclass_reg_;
39 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
40
41 /* am I of the given metaclass? */
42 virtual int is_of_metaclass(class CVmMetaclass *meta) const
43 {
44 /* try my own metaclass and my base class */
45 return (meta == metaclass_reg_
46 || CVmObject::is_of_metaclass(meta));
47 }
48
49 /*
50 * call a static property - we don't have any of our own, so simply
51 * "inherit" the base class handling
52 */
53 static int call_stat_prop(VMG_ vm_val_t *result,
54 const uchar **pc_ptr, uint *argc,
55 vm_prop_id_t prop)
56 { return CVmObject::call_stat_prop(vmg_ result, pc_ptr, argc, prop); }
57
58 /* set a property */
59 void set_prop(VMG_ class CVmUndo *,
60 vm_obj_id_t, vm_prop_id_t, const vm_val_t *)
61 {
62 /* cannot set iterator properties */
63 err_throw(VMERR_INVALID_SETPROP);
64 }
65
66 /* get a property */
67 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
68 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
69
70 protected:
71 /* property evaluator - undefined property */
72 int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
73
74 /* property evaluator - get next value */
75 virtual int getp_get_next(VMG_ vm_obj_id_t self, vm_val_t *retval,
76 uint *argc) = 0;
77
78 /* property evaluator - is next value available? */
79 virtual int getp_is_next_avail(VMG_ vm_obj_id_t self, vm_val_t *retval,
80 uint *argc) = 0;
81
82 /* property evaluator - reset to first item */
83 virtual int getp_reset_iter(VMG_ vm_obj_id_t self, vm_val_t *retval,
84 uint *argc) = 0;
85
86
87 /* property evaluator - get current key */
88 virtual int getp_get_cur_key(VMG_ vm_obj_id_t self, vm_val_t *retval,
89 uint *argc) = 0;
90
91 /* property evaluator - get current value */
92 virtual int getp_get_cur_val(VMG_ vm_obj_id_t self, vm_val_t *retval,
93 uint *argc) = 0;
94
95 /* function table */
96 static int (CVmObjIter::*func_table_[])(VMG_ vm_obj_id_t self,
97 vm_val_t *retval, uint *argc);
98 };
99
100
101 /* ------------------------------------------------------------------------ */
102 /*
103 * Indexed Iterator subclass. An indexed iterator works with arrays,
104 * lists, and other collections that can be accessed via an integer
105 * index.
106 */
107
108 /*
109 * The extension data for an indexed iterator consists of a reference to
110 * the associated indexed collection, the index value of the next item
111 * to be retrieved, and the first and last valid index values: Note that
112 * the collection value can be an object ID or a constant VM_LIST value.
113 *
114 * DATAHOLDER collection_value
115 *. UINT4 cur_index
116 *. UINT4 first_valid
117 *. UINT4 last_valid
118 *. UINT4 flags
119 *
120 * The flag values are:
121 *
122 * VMOBJITERIDX_UNDO - we've saved undo for this savepoint. If this is
123 * set, we won't save additional undo for the same savepoint.
124 */
125
126 /* total extension size */
127 #define VMOBJITERIDX_EXT_SIZE (VMB_DATAHOLDER + 16)
128
129 /*
130 * flag bits
131 */
132
133 /* we've saved undo for the current savepoint */
134 #define VMOBJITERIDX_UNDO 0x0001
135
136 /*
137 * indexed iterator class
138 */
139 class CVmObjIterIdx: public CVmObjIter
140 {
141 friend class CVmMetaclassIterIdx;
142
143 public:
144 /* metaclass registration object */
145 static class CVmMetaclass *metaclass_reg_;
146 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
147
148 /* am I of the given metaclass? */
149 virtual int is_of_metaclass(class CVmMetaclass *meta) const
150 {
151 /* try my own metaclass and my base class */
152 return (meta == metaclass_reg_
153 || CVmObjIter::is_of_metaclass(meta));
154 }
155
156 /*
157 * call a static property - we don't have any of our own, so simply
158 * "inherit" the base class handling
159 */
160 static int call_stat_prop(VMG_ vm_val_t *result,
161 const uchar **pc_ptr, uint *argc,
162 vm_prop_id_t prop)
163 {
164 return CVmObjIter::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
165 }
166
167 /*
168 * Create an indexed iterator. This method is to be called by a
169 * list, array, or other indexed collection object to create an
170 * iterator for its value.
171 */
172 static vm_obj_id_t create_for_coll(VMG_ const vm_val_t *coll,
173 long first_valid_index,
174 long last_valid_index);
175
176 /* notify of deletion */
177 void notify_delete(VMG_ int in_root_set);
178
179 /*
180 * notify of a new savepoint - clear the 'undo' flag, since we
181 * cannot have created any undo information yet for the new
182 * savepoint
183 */
184 void notify_new_savept()
185 { set_flags(get_flags() & ~VMOBJITERIDX_UNDO); }
186
187 /* apply undo */
188 void apply_undo(VMG_ struct CVmUndoRecord *rec);
189
190 /* mark references */
191 void mark_refs(VMG_ uint state);
192
193 /* there are no references in our undo stream */
194 void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
195
196 /* we keep only strong references */
197 void remove_stale_weak_refs(VMG0_) { }
198 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
199
200 /* load from an image file */
201 void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz);
202
203 /* restore to image file state */
204 void reload_from_image(VMG_ vm_obj_id_t self,
205 const char *ptr, size_t siz);
206
207 /*
208 * determine if the object has been changed since it was loaded -
209 * assume we have, since saving and reloading are very cheap
210 */
211 int is_changed_since_load() const { return TRUE; }
212
213 /* save to a file */
214 void save_to_file(VMG_ class CVmFile *fp);
215
216 /* restore from a file */
217 void restore_from_file(VMG_ vm_obj_id_t self,
218 class CVmFile *fp, class CVmObjFixup *fixups);
219
220 /* rebuild for image file */
221 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
222
223 /* convert to constant data */
224 virtual void convert_to_const_data(VMG_ class CVmConstMapper *mapper,
225 vm_obj_id_t self);
226
227 /* get the next iteration item */
228 virtual int iter_next(VMG_ vm_obj_id_t self, vm_val_t *val);
229
230 protected:
231 /* create */
232 CVmObjIterIdx() { ext_ = 0; }
233
234 /* create */
235 CVmObjIterIdx(VMG_ const vm_val_t *coll, long first_valid_index,
236 long last_valid_index);
237
238 /* get the value from the collection for a given index */
239 void get_indexed_val(VMG_ long idx, vm_val_t *retval);
240
241 /* property evaluator - get next value */
242 virtual int getp_get_next(VMG_ vm_obj_id_t self, vm_val_t *retval,
243 uint *argc);
244
245 /* property evaluator - is next value available? */
246 virtual int getp_is_next_avail(VMG_ vm_obj_id_t self, vm_val_t *retval,
247 uint *argc);
248
249 /* property evaluator - reset to first item */
250 virtual int getp_reset_iter(VMG_ vm_obj_id_t self, vm_val_t *retval,
251 uint *argc);
252
253 /* property evaluator - get current key */
254 virtual int getp_get_cur_key(VMG_ vm_obj_id_t self, vm_val_t *retval,
255 uint *argc);
256
257 /* property evaluator - get current value */
258 virtual int getp_get_cur_val(VMG_ vm_obj_id_t self, vm_val_t *retval,
259 uint *argc);
260
261 /* get my collection value */
262 void get_coll_val(vm_val_t *val) { vmb_get_dh(ext_, val); }
263
264 /* get/set the current index (without saving undo) */
265 long get_cur_index() const
266 { return t3rp4u(ext_ + VMB_DATAHOLDER); }
267 void set_cur_index_no_undo(long idx)
268 { oswp4(ext_ + VMB_DATAHOLDER, idx); }
269
270 /* set the index value, saving undo if necessary */
271 void set_cur_index(VMG_ vm_obj_id_t self, long idx);
272
273 /* get my first/last valid index values */
274 long get_first_valid() const
275 { return t3rp4u(ext_ + VMB_DATAHOLDER + 4); }
276 long get_last_valid() const
277 { return t3rp4u(ext_ + VMB_DATAHOLDER + 8); }
278
279 /* set my first/last valid index values - for construction only */
280 void set_first_valid(long idx) const
281 { oswp4(ext_ + VMB_DATAHOLDER + 4, idx); }
282 void set_last_valid(long idx) const
283 { oswp4(ext_ + VMB_DATAHOLDER + 8, idx); }
284
285 /* get/set the flags */
286 unsigned long get_flags() const
287 { return t3rp4u(ext_ + VMB_DATAHOLDER + 12); }
288 void set_flags(unsigned long flags) const
289 { oswp4(ext_ + VMB_DATAHOLDER + 12, flags); }
290 };
291
292
293 /* ------------------------------------------------------------------------ */
294 /*
295 * Registration table object for the base iterator class
296 */
297 class CVmMetaclassIter: public CVmMetaclass
298 {
299 public:
300 /* get the global name */
301 const char *get_meta_name() const { return "iterator/030001"; }
302
303 /* create from image file */
304 void create_for_image_load(VMG_ vm_obj_id_t id)
305 { err_throw(VMERR_BAD_STATIC_NEW); }
306
307 /* create from restoring from saved state */
308 void create_for_restore(VMG_ vm_obj_id_t id)
309 { err_throw(VMERR_BAD_STATIC_NEW); }
310
311 /* create dynamically using stack arguments */
312 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
313 {
314 err_throw(VMERR_BAD_DYNAMIC_NEW);
315 AFTER_ERR_THROW(return VM_INVALID_OBJ;)
316 }
317
318 /* call a static property */
319 int call_stat_prop(VMG_ vm_val_t *result,
320 const uchar **pc_ptr, uint *argc,
321 vm_prop_id_t prop)
322 {
323 return CVmObjIter::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
324 }
325 };
326
327 /*
328 * Registration table object for indexed iterators
329 */
330 class CVmMetaclassIterIdx: public CVmMetaclass
331 {
332 public:
333 /* get the global name */
334 const char *get_meta_name() const { return "indexed-iterator/030000"; }
335
336 /* create from image file */
337 void create_for_image_load(VMG_ vm_obj_id_t id)
338 {
339 new (vmg_ id) CVmObjIterIdx();
340 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
341 }
342
343 /* create from restoring from saved state */
344 void create_for_restore(VMG_ vm_obj_id_t id)
345 {
346 new (vmg_ id) CVmObjIterIdx();
347 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
348 }
349
350 /* create dynamically using stack arguments */
351 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
352 {
353 err_throw(VMERR_BAD_DYNAMIC_NEW);
354 AFTER_ERR_THROW(return VM_INVALID_OBJ;)
355 }
356
357 /* call a static property */
358 int call_stat_prop(VMG_ vm_val_t *result,
359 const uchar **pc_ptr, uint *argc,
360 vm_prop_id_t prop)
361 {
362 return CVmObjIterIdx::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
363 }
364 };
365
366 #endif /* VMITER_H */
367
368 /*
369 * Register the class
370 */
371 VM_REGISTER_METACLASS(CVmObjIter)
372 VM_REGISTER_METACLASS(CVmObjIterIdx)
373