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

1.1       paf         1: /*
1.19    ! paf         2:   $Id: pa_vclass.h,v 1.18.2.1 2001/02/25 16:08: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.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)
1.19    ! paf        26:                        return NEW VString(class_alias->name());
1.17      paf        27:                // $CLASS=my class=myself
                     28:                if(aname==CLASS_NAME)
1.19    ! paf        29:                        return class_alias;
1.17      paf        30:                // $BASE=my parent
                     31:                if(aname==BASE_NAME)
1.19    ! paf        32:                        return class_alias->base();
        !            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.17      paf        51:                fields(apool),
1.15      paf        52:                fmethods(apool),
1.17      paf        53:                fbase(0) {
1.1       paf        54:        }
                     55: 
                     56:        void add_method(const String& name, Method& method) {
1.15      paf        57:                fmethods.put(name, &method);
1.1       paf        58:        }
1.19    ! paf        59: //     Hash& methods() { return fmethods; }
1.15      paf        60:        
1.17      paf        61:        void set_base(VClass& abase) {
                     62:                // remember the guy
                     63:                fbase=&abase;
1.15      paf        64:        }
1.17      paf        65:        VClass *base() { return fbase; }
1.18      paf        66: 
                     67:        bool is_or_derived_from(VClass& vclass) {
                     68:                return 
                     69:                        this==&vclass || 
                     70:                        fbase && fbase->is_or_derived_from(vclass);
1.19    ! paf        71:        }
        !            72: 
        !            73:        Junction *get_junction(VAliased& self, const String& name) {
        !            74:                if(Method *method=static_cast<Method *>(fmethods.get(name)))
        !            75:                        return NEW Junction(pool(), self, this, method, 0,0,0,0);
        !            76:                if(fbase)
        !            77:                        return fbase->get_junction(self, name);
        !            78:                return 0; 
1.18      paf        79:        }
1.11      paf        80: 
1.17      paf        81: private:
1.11      paf        82: 
1.17      paf        83:        Value *get_field(const String& name) {
                     84:                Value *result=static_cast<Value *>(fields.get(name));
                     85:                if(!result && fbase)
                     86:                        result=fbase->get_field(name);
                     87:                return result;
                     88:        }
                     89:                
                     90:        void set_field(const String& name, Value *value) {
                     91:                if(fbase && fbase->replace_field(name, value))
                     92:                        return;
                     93: 
                     94:                fields.put(name, value);
                     95:        }
                     96:        bool replace_field(const String& name, Value *value) {
                     97:                return 
                     98:                        (fbase && fbase->replace_field(name, value)) ||
                     99:                        fields.put_replace(name, value);
                    100:        }
                    101:        
1.1       paf       102: private:
                    103: 
1.17      paf       104:        VClass *fbase;
                    105:        Hash fields;
1.15      paf       106:        Hash fmethods;
1.1       paf       107: };
                    108: 
                    109: #endif

E-mail: