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

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

E-mail: