Annotation of parser3/src/include/pa_vclass.h, revision 1.24

1.1       paf         1: /*
1.24    ! paf         2:   $Id: pa_vclass.h,v 1.23 2001/03/08 16:54:25 paf Exp $
1.1       paf         3: */
                      4: 
                      5: #ifndef PA_VCLASS_H
                      6: #define PA_VCLASS_H
                      7: 
1.19      paf         8: #include "pa_valiased.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.19      paf        16: class VClass : public VAliased {
1.1       paf        17: public: // Value
                     18:        
                     19:        // all: for error reporting after fail(), etc
1.20      paf        20:        const char *type() const { return "class"; }
1.23      paf        21: 
1.1       paf        22:        // object_class: (field)=STATIC.value;(STATIC)=hash;(method)=method_ref with self=object_class
1.22      paf        23:        Value *get_element(const String& aname) {
1.17      paf        24:                // $NAME=my name
1.15      paf        25:                if(aname==NAME_NAME)
1.23      paf        26:                        return NEW VString(fclass_alias->name());
1.17      paf        27:                // $CLASS=my class=myself
                     28:                if(aname==CLASS_NAME)
1.23      paf        29:                        return fclass_alias;
1.17      paf        30:                // $BASE=my parent
                     31:                if(aname==BASE_NAME)
1.23      paf        32:                        return fclass_alias->base();
1.19      paf        33:                // $method=junction(self+class+method)
                     34:                if(Junction *junction=get_junction(*this, aname))
                     35:                        return NEW VJunction(*junction);
1.17      paf        36:                // $field=static field
                     37:                return get_field(aname);
1.1       paf        38:        }
                     39: 
                     40:        // object_class, operator_class: (field)=value - static values only
1.4       paf        41:        void put_element(const String& name, Value *value) {
1.17      paf        42:                set_field(name, value);
1.1       paf        43:        }
                     44: 
                     45:        // object_class, object_instance: object_class
1.17      paf        46:        VClass *get_class() { return this; }
1.1       paf        47: 
                     48: public: // usage
                     49: 
1.19      paf        50:        VClass(Pool& apool) : VAliased(apool, *this), 
1.23      paf        51:                fbase(0),
                     52:                ffields(*new(apool) Hash(apool)),
                     53:                fmethods(*new(apool) Hash(apool)) {
                     54:        }
                     55: 
                     56:        VClass(Pool& apool, 
                     57:                VClass *abase, Hash& afields, Hash& amethods) : VAliased(apool, *this), 
                     58: 
                     59:                fbase(abase),
                     60:                ffields(afields),
                     61:                fmethods(amethods) {
1.1       paf        62:        }
                     63: 
                     64:        void add_method(const String& name, Method& method) {
1.15      paf        65:                fmethods.put(name, &method);
1.1       paf        66:        }
1.19      paf        67: //     Hash& methods() { return fmethods; }
1.15      paf        68:        
1.17      paf        69:        void set_base(VClass& abase) {
                     70:                // remember the guy
                     71:                fbase=&abase;
1.15      paf        72:        }
1.17      paf        73:        VClass *base() { return fbase; }
1.18      paf        74: 
                     75:        bool is_or_derived_from(VClass& vclass) {
                     76:                return 
                     77:                        this==&vclass || 
                     78:                        fbase && fbase->is_or_derived_from(vclass);
1.19      paf        79:        }
                     80: 
                     81:        Junction *get_junction(VAliased& self, const String& name) {
                     82:                if(Method *method=static_cast<Method *>(fmethods.get(name)))
                     83:                        return NEW Junction(pool(), self, this, method, 0,0,0,0);
                     84:                if(fbase)
                     85:                        return fbase->get_junction(self, name);
                     86:                return 0; 
1.18      paf        87:        }
1.11      paf        88: 
1.17      paf        89: private:
1.11      paf        90: 
1.17      paf        91:        Value *get_field(const String& name) {
1.23      paf        92:                Value *result=static_cast<Value *>(ffields.get(name));
1.17      paf        93:                if(!result && fbase)
                     94:                        result=fbase->get_field(name);
                     95:                return result;
                     96:        }
                     97:                
                     98:        void set_field(const String& name, Value *value) {
                     99:                if(fbase && fbase->replace_field(name, value))
                    100:                        return;
                    101: 
1.23      paf       102:                ffields.put(name, value);
1.17      paf       103:        }
                    104:        bool replace_field(const String& name, Value *value) {
                    105:                return 
                    106:                        (fbase && fbase->replace_field(name, value)) ||
1.23      paf       107:                        ffields.put_replace(name, value);
1.17      paf       108:        }
                    109:        
1.1       paf       110: private:
                    111: 
1.17      paf       112:        VClass *fbase;
1.23      paf       113:        Hash& ffields;
                    114:        Hash& fmethods;
1.1       paf       115: };
                    116: 
                    117: #endif

E-mail: