Annotation of parser3/src/include/pa_vclass.h, revision 1.18
1.1 paf 1: /*
1.18 ! paf 2: $Id: pa_vclass.h,v 1.17 2001/02/25 13:23:01 paf Exp $
1.1 paf 3: */
4:
5: #ifndef PA_VCLASS_H
6: #define PA_VCLASS_H
7:
8: #include "pa_value.h"
1.6 paf 9: #include "pa_vhash.h"
1.13 paf 10: #include "pa_vstring.h"
1.8 paf 11: #include "pa_vjunction.h"
1.1 paf 12:
1.17 paf 13: #define CLASS_NAME "CLASS"
14: #define BASE_NAME "BASE"
15:
1.1 paf 16: class VClass : public Value {
17: public: // Value
18:
19: // all: for error reporting after fail(), etc
1.4 paf 20: const char *type() const { return "Class"; }
1.1 paf 21:
22: // object_class: (field)=STATIC.value;(STATIC)=hash;(method)=method_ref with self=object_class
1.15 paf 23: Value *get_element(const String& aname) {
1.17 paf 24: // $NAME=my name
1.15 paf 25: if(aname==NAME_NAME)
26: return NEW VString(name());
1.17 paf 27: // $CLASS=my class=myself
28: if(aname==CLASS_NAME)
29: return this;
30: // $BASE=my parent
31: if(aname==BASE_NAME)
32: return base();
1.7 paf 33: // $method=junction(this+method)
1.15 paf 34: if(Method *method=static_cast<Method *>(methods().get(aname))) {
1.8 paf 35: Junction& j=*NEW Junction(pool(),
1.10 paf 36: *this,
37: method,0,0,0,0);
1.7 paf 38:
39: return NEW VJunction(j);
1.1 paf 40: }
1.17 paf 41: // $field=static field
42: return get_field(aname);
1.1 paf 43: }
44:
45: // object_class, operator_class: (field)=value - static values only
1.4 paf 46: void put_element(const String& name, Value *value) {
1.17 paf 47: set_field(name, value);
1.1 paf 48: }
49:
50: // object_class, object_instance: object_class
1.17 paf 51: VClass *get_class() { return this; }
1.1 paf 52:
53: public: // usage
54:
1.14 paf 55: VClass(Pool& apool) :
1.1 paf 56: Value(apool),
1.17 paf 57: fields(apool),
1.15 paf 58: fmethods(apool),
1.17 paf 59: fbase(0) {
1.1 paf 60: }
61:
62: void add_method(const String& name, Method& method) {
1.15 paf 63: fmethods.put(name, &method);
1.1 paf 64: }
1.15 paf 65: Hash& methods() { return fmethods; }
66:
1.17 paf 67: void set_base(VClass& abase) {
68: // remember the guy
69: fbase=&abase;
70:
71: // append base_method to my methods unless already there is one
72: // 'virtual' here
73: fmethods.merge_dont_replace(abase.methods());
1.15 paf 74: }
1.17 paf 75: VClass *base() { return fbase; }
1.18 ! paf 76:
! 77: bool is_or_derived_from(VClass& vclass) {
! 78: return
! 79: this==&vclass ||
! 80: fbase && fbase->is_or_derived_from(vclass);
! 81: }
1.11 paf 82:
1.17 paf 83: private:
1.11 paf 84:
1.17 paf 85: Value *get_field(const String& name) {
86: Value *result=static_cast<Value *>(fields.get(name));
87: if(!result && fbase)
88: result=fbase->get_field(name);
89: return result;
90: }
91:
92: void set_field(const String& name, Value *value) {
93: if(fbase && fbase->replace_field(name, value))
94: return;
95:
96: fields.put(name, value);
97: }
98: bool replace_field(const String& name, Value *value) {
99: return
100: (fbase && fbase->replace_field(name, value)) ||
101: fields.put_replace(name, value);
102: }
103:
1.1 paf 104: private:
105:
1.17 paf 106: VClass *fbase;
107: Hash fields;
1.15 paf 108: Hash fmethods;
1.1 paf 109: };
110:
111: #endif
E-mail: