4
* Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved.
6
* Please see the accompanying license file, LICENSE.TXT, for information
7
* on using and copying this software.
11
vmiter.h - Iterator metaclass
17
04/22/00 MJRoberts - Creation
28
/* ------------------------------------------------------------------------ */
32
class CVmObjIter: public CVmObject
34
friend class CVmMetaclassIter;
37
/* metaclass registration object */
38
static class CVmMetaclass *metaclass_reg_;
39
class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
41
/* am I of the given metaclass? */
42
virtual int is_of_metaclass(class CVmMetaclass *meta) const
44
/* try my own metaclass and my base class */
45
return (meta == metaclass_reg_
46
|| CVmObject::is_of_metaclass(meta));
50
* call a static property - we don't have any of our own, so simply
51
* "inherit" the base class handling
53
static int call_stat_prop(VMG_ vm_val_t *result,
54
const uchar **pc_ptr, uint *argc,
56
{ return CVmObject::call_stat_prop(vmg_ result, pc_ptr, argc, prop); }
59
void set_prop(VMG_ class CVmUndo *,
60
vm_obj_id_t, vm_prop_id_t, const vm_val_t *)
62
/* cannot set iterator properties */
63
err_throw(VMERR_INVALID_SETPROP);
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);
71
/* property evaluator - undefined property */
72
int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
74
/* property evaluator - get next value */
75
virtual int getp_get_next(VMG_ vm_obj_id_t self, vm_val_t *retval,
78
/* property evaluator - is next value available? */
79
virtual int getp_is_next_avail(VMG_ vm_obj_id_t self, vm_val_t *retval,
82
/* property evaluator - reset to first item */
83
virtual int getp_reset_iter(VMG_ vm_obj_id_t self, vm_val_t *retval,
87
/* property evaluator - get current key */
88
virtual int getp_get_cur_key(VMG_ vm_obj_id_t self, vm_val_t *retval,
91
/* property evaluator - get current value */
92
virtual int getp_get_cur_val(VMG_ vm_obj_id_t self, vm_val_t *retval,
96
static int (CVmObjIter::*func_table_[])(VMG_ vm_obj_id_t self,
97
vm_val_t *retval, uint *argc);
101
/* ------------------------------------------------------------------------ */
103
* Indexed Iterator subclass. An indexed iterator works with arrays,
104
* lists, and other collections that can be accessed via an integer
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.
114
* DATAHOLDER collection_value
120
* The flag values are:
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.
126
/* total extension size */
127
#define VMOBJITERIDX_EXT_SIZE (VMB_DATAHOLDER + 16)
133
/* we've saved undo for the current savepoint */
134
#define VMOBJITERIDX_UNDO 0x0001
137
* indexed iterator class
139
class CVmObjIterIdx: public CVmObjIter
141
friend class CVmMetaclassIterIdx;
144
/* metaclass registration object */
145
static class CVmMetaclass *metaclass_reg_;
146
class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
148
/* am I of the given metaclass? */
149
virtual int is_of_metaclass(class CVmMetaclass *meta) const
151
/* try my own metaclass and my base class */
152
return (meta == metaclass_reg_
153
|| CVmObjIter::is_of_metaclass(meta));
157
* call a static property - we don't have any of our own, so simply
158
* "inherit" the base class handling
160
static int call_stat_prop(VMG_ vm_val_t *result,
161
const uchar **pc_ptr, uint *argc,
164
return CVmObjIter::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
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.
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);
176
/* notify of deletion */
177
void notify_delete(VMG_ int in_root_set);
180
* notify of a new savepoint - clear the 'undo' flag, since we
181
* cannot have created any undo information yet for the new
184
void notify_new_savept()
185
{ set_flags(get_flags() & ~VMOBJITERIDX_UNDO); }
188
void apply_undo(VMG_ struct CVmUndoRecord *rec);
190
/* mark references */
191
void mark_refs(VMG_ uint state);
193
/* there are no references in our undo stream */
194
void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
196
/* we keep only strong references */
197
void remove_stale_weak_refs(VMG0_) { }
198
void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
200
/* load from an image file */
201
void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz);
203
/* restore to image file state */
204
void reload_from_image(VMG_ vm_obj_id_t self,
205
const char *ptr, size_t siz);
208
* determine if the object has been changed since it was loaded -
209
* assume we have, since saving and reloading are very cheap
211
int is_changed_since_load() const { return TRUE; }
214
void save_to_file(VMG_ class CVmFile *fp);
216
/* restore from a file */
217
void restore_from_file(VMG_ vm_obj_id_t self,
218
class CVmFile *fp, class CVmObjFixup *fixups);
220
/* rebuild for image file */
221
virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
223
/* convert to constant data */
224
virtual void convert_to_const_data(VMG_ class CVmConstMapper *mapper,
229
CVmObjIterIdx() { ext_ = 0; }
232
CVmObjIterIdx(VMG_ const vm_val_t *coll, long first_valid_index,
233
long last_valid_index);
235
/* get the value from the collection for a given index */
236
void get_indexed_val(VMG_ long idx, vm_val_t *retval);
238
/* property evaluator - get next value */
239
virtual int getp_get_next(VMG_ vm_obj_id_t self, vm_val_t *retval,
242
/* property evaluator - is next value available? */
243
virtual int getp_is_next_avail(VMG_ vm_obj_id_t self, vm_val_t *retval,
246
/* property evaluator - reset to first item */
247
virtual int getp_reset_iter(VMG_ vm_obj_id_t self, vm_val_t *retval,
250
/* property evaluator - get current key */
251
virtual int getp_get_cur_key(VMG_ vm_obj_id_t self, vm_val_t *retval,
254
/* property evaluator - get current value */
255
virtual int getp_get_cur_val(VMG_ vm_obj_id_t self, vm_val_t *retval,
258
/* get my collection value */
259
void get_coll_val(vm_val_t *val) { vmb_get_dh(ext_, val); }
261
/* get/set the current index (without saving undo) */
262
long get_cur_index() const
263
{ return t3rp4u(ext_ + VMB_DATAHOLDER); }
264
void set_cur_index_no_undo(long idx)
265
{ oswp4(ext_ + VMB_DATAHOLDER, idx); }
267
/* set the index value, saving undo if necessary */
268
void set_cur_index(VMG_ vm_obj_id_t self, long idx);
270
/* get my first/last valid index values */
271
long get_first_valid() const
272
{ return t3rp4u(ext_ + VMB_DATAHOLDER + 4); }
273
long get_last_valid() const
274
{ return t3rp4u(ext_ + VMB_DATAHOLDER + 8); }
276
/* set my first/last valid index values - for construction only */
277
void set_first_valid(long idx) const
278
{ oswp4(ext_ + VMB_DATAHOLDER + 4, idx); }
279
void set_last_valid(long idx) const
280
{ oswp4(ext_ + VMB_DATAHOLDER + 8, idx); }
282
/* get/set the flags */
283
unsigned long get_flags() const
284
{ return t3rp4u(ext_ + VMB_DATAHOLDER + 12); }
285
void set_flags(unsigned long flags) const
286
{ oswp4(ext_ + VMB_DATAHOLDER + 12, flags); }
290
/* ------------------------------------------------------------------------ */
292
* Registration table object for the base iterator class
294
class CVmMetaclassIter: public CVmMetaclass
297
/* get the global name */
298
const char *get_meta_name() const { return "iterator/030001"; }
300
/* create from image file */
301
void create_for_image_load(VMG_ vm_obj_id_t id)
302
{ err_throw(VMERR_BAD_STATIC_NEW); }
304
/* create from restoring from saved state */
305
void create_for_restore(VMG_ vm_obj_id_t id)
306
{ err_throw(VMERR_BAD_STATIC_NEW); }
308
/* create dynamically using stack arguments */
309
vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
311
err_throw(VMERR_BAD_DYNAMIC_NEW);
312
AFTER_ERR_THROW(return VM_INVALID_OBJ;)
315
/* call a static property */
316
int call_stat_prop(VMG_ vm_val_t *result,
317
const uchar **pc_ptr, uint *argc,
320
return CVmObjIter::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
325
* Registration table object for indexed iterators
327
class CVmMetaclassIterIdx: public CVmMetaclass
330
/* get the global name */
331
const char *get_meta_name() const { return "indexed-iterator/030000"; }
333
/* create from image file */
334
void create_for_image_load(VMG_ vm_obj_id_t id)
336
new (vmg_ id) CVmObjIterIdx();
337
G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
340
/* create from restoring from saved state */
341
void create_for_restore(VMG_ vm_obj_id_t id)
343
new (vmg_ id) CVmObjIterIdx();
344
G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
347
/* create dynamically using stack arguments */
348
vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
350
err_throw(VMERR_BAD_DYNAMIC_NEW);
351
AFTER_ERR_THROW(return VM_INVALID_OBJ;)
354
/* call a static property */
355
int call_stat_prop(VMG_ vm_val_t *result,
356
const uchar **pc_ptr, uint *argc,
359
return CVmObjIterIdx::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
363
#endif /* VMITER_H */
368
VM_REGISTER_METACLASS(CVmObjIter)
369
VM_REGISTER_METACLASS(CVmObjIterIdx)