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   vmcoll.h - T3 Collection base class
12 Function
13   A Collection is the base class for List, Array, and other objects
14   providing a collection of objects that can be iterated via an
15   Iterator.
16 
17   Collection is an abstract base class: it cannot be instantiated, and
18   thus has no image-file or state-file representation.
19 Notes
20 
21 Modified
22   04/22/00 MJRoberts  - Creation
23 */
24 
25 #ifndef VMCOLL_H
26 #define VMCOLL_H
27 
28 #include <stdlib.h>
29 #include "vmtype.h"
30 #include "vmobj.h"
31 #include "vmglob.h"
32 #include "vmerr.h"
33 #include "vmerrnum.h"
34 
35 
36 class CVmObjCollection: public CVmObject
37 {
38     friend class CVmMetaclassCollection;
39 
40 public:
41     /* metaclass registration object */
42     static class CVmMetaclass *metaclass_reg_;
get_metaclass_reg()43     class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
44 
45     /* am I of the given metaclass? */
is_of_metaclass(class CVmMetaclass * meta)46     virtual int is_of_metaclass(class CVmMetaclass *meta) const
47     {
48         /* try my own metaclass and my base class */
49         return (meta == metaclass_reg_
50                 || CVmObject::is_of_metaclass(meta));
51     }
52 
53     /*
54      *   call a static property - we don't have any of our own, so simply
55      *   "inherit" the base class handling
56      */
call_stat_prop(VMG_ vm_val_t * result,const uchar ** pc_ptr,uint * argc,vm_prop_id_t prop)57     static int call_stat_prop(VMG_ vm_val_t *result,
58                               const uchar **pc_ptr, uint *argc,
59                               vm_prop_id_t prop)
60         { return CVmObject::call_stat_prop(vmg_ result, pc_ptr, argc, prop); }
61 
62     /* get a property */
63     int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval,
64                  vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
65 
66     /*
67      *   constant value property evaluator - this allows us to evaluate a
68      *   property for an object value or for a constant value using the
69      *   same code
70      */
71     int const_get_coll_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval,
72                             const vm_val_t *self_val, vm_obj_id_t *src_obj,
73                             uint *argc);
74 
75 protected:
76     /*
77      *   Create an iterator for this collection.  Fills in *retval with a
78      *   reference to the new iterator object.  This iterator must refer
79      *   to an immutable snapshot of the collection.
80      */
81     virtual void new_iterator(VMG_ vm_val_t *retval,
82                               const vm_val_t *self_val) = 0;
83 
84     /*
85      *   Create a "live" iterator for this collection.  Fills in *retval
86      *   with a reference to the new iterator object.  This iterator must
87      *   refer to the original "live" collection.
88      */
89     virtual void new_live_iterator(VMG_ vm_val_t *retval,
90                                    const vm_val_t *self_val) = 0;
91 
92     /* property evaluator - undefined property */
getp_undef(VMG_ vm_val_t *,const vm_val_t *,uint *)93     int getp_undef(VMG_ vm_val_t *, const vm_val_t *, uint *)
94         { return FALSE; }
95 
96     /* property evaluator - create iterator */
97     int getp_create_iter(VMG_ vm_val_t *retval, const vm_val_t *self_val,
98                          uint *argc);
99 
100     /* property evaluator - create live iterator */
101     int getp_create_live_iter(VMG_ vm_val_t *retval,
102                               const vm_val_t *self_val, uint *argc);
103 
104     /* property evaluation function table */
105     static int (CVmObjCollection::*func_table_[])(VMG_ vm_val_t *retval,
106         const vm_val_t *self_val, uint *argc);
107 };
108 
109 
110 /* ------------------------------------------------------------------------ */
111 /*
112  *   Registration table object
113  */
114 class CVmMetaclassCollection: public CVmMetaclass
115 {
116 public:
117     /* get the global name */
get_meta_name()118     const char *get_meta_name() const { return "collection/030000"; }
119 
120     /* create from image file */
create_for_image_load(VMG_ vm_obj_id_t id)121     void create_for_image_load(VMG_ vm_obj_id_t id)
122         { err_throw(VMERR_BAD_STATIC_NEW); }
123 
124     /* create from restoring from saved state */
create_for_restore(VMG_ vm_obj_id_t id)125     void create_for_restore(VMG_ vm_obj_id_t id)
126         { err_throw(VMERR_BAD_STATIC_NEW); }
127 
128     /* create dynamically using stack arguments */
create_from_stack(VMG_ const uchar ** pc_ptr,uint argc)129     vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
130     {
131         err_throw(VMERR_BAD_DYNAMIC_NEW);
132         return VM_INVALID_OBJ;
133     }
134 
135     /* call a static property */
call_stat_prop(VMG_ vm_val_t * result,const uchar ** pc_ptr,uint * argc,vm_prop_id_t prop)136     int call_stat_prop(VMG_ vm_val_t *result,
137                        const uchar **pc_ptr, uint *argc,
138                        vm_prop_id_t prop)
139     {
140         return CVmObjCollection::
141             call_stat_prop(vmg_ result, pc_ptr, argc, prop);
142     }
143 };
144 
145 #endif /* VMCOLL_H */
146 
147 /*
148  *   Register the class
149  */
150 VM_REGISTER_METACLASS(CVmObjCollection)
151 
152