Annotation of parser3/src/types/pa_vclass.h, revision 1.6

1.1       paf         1: /*
                      2:        Parser
                      3:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.2       paf         4:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.1       paf         5: 
1.6     ! paf         6:        $Id: pa_vclass.h,v 1.5 2001/03/12 09:41:00 paf Exp $
1.1       paf         7: */
                      8: 
                      9: #ifndef PA_VCLASS_H
                     10: #define PA_VCLASS_H
                     11: 
                     12: #include "pa_valiased.h"
                     13: #include "pa_vhash.h"
                     14: #include "pa_vjunction.h"
                     15: 
                     16: #define CLASS_NAME "CLASS"
                     17: #define BASE_NAME "BASE"
                     18: 
1.4       paf        19: class Temp_method;
                     20: 
1.1       paf        21: class VClass : public VAliased {
1.4       paf        22:        friend Temp_method;
1.1       paf        23: public: // Value
                     24:        
                     25:        // all: for error reporting after fail(), etc
                     26:        const char *type() const { return "class"; }
                     27: 
                     28:        // object_class: (field)=STATIC.value;(STATIC)=hash;(method)=method_ref with self=object_class
                     29:        Value *get_element(const String& aname);
                     30: 
                     31:        // object_class, operator_class: (field)=value - static values only
                     32:        void put_element(const String& name, Value *value);
                     33: 
                     34:        // object_class, object_instance: object_class
                     35:        VClass *get_class() { return this; }
                     36: 
                     37: public: // usage
                     38: 
                     39:        VClass(Pool& apool) : VAliased(apool, *this), 
1.6     ! paf        40:                read_only(false),
1.1       paf        41:                fbase(0),
                     42:                ffields(apool),
                     43:                fmethods(apool) {
                     44:        }
                     45: 
1.5       paf        46:        Method *get_method(const String& name) { 
                     47:                return static_cast<Method *>(fmethods.get(name)); 
                     48:        }
                     49: 
1.6     ! paf        50:        // make class read-only
        !            51:        //      this blocks 
        !            52:        //              put_method  // which could be done with ^process
        !            53:        //              put_element  // - - - - - CLASS:static_field
        !            54:        void freeze() { read_only=true; }
        !            55: 
1.1       paf        56:        void add_method(const String& name, Method& method) {
1.6     ! paf        57:                put_method(name, &method);
1.1       paf        58:        }
1.3       paf        59:        void add_native_method(
                     60:                const char *cstr_name,
                     61:                Native_code_ptr native_code,
                     62:                int min_numbered_params_count, int max_numbered_params_count);
1.1       paf        63:        
                     64:        void set_base(VClass& abase) {
                     65:                // remember the guy
                     66:                fbase=&abase;
                     67:        }
                     68:        VClass *base() { return fbase; }
                     69: 
                     70:        bool is_or_derived_from(VClass& vclass) {
                     71:                return 
                     72:                        this==&vclass || 
                     73:                        fbase && fbase->is_or_derived_from(vclass);
                     74:        }
                     75: 
                     76:        Junction *get_junction(VAliased& self, const String& name) {
                     77:                if(Method *method=static_cast<Method *>(fmethods.get(name)))
                     78:                        return NEW Junction(pool(), self, this, method, 0,0,0,0);
                     79:                if(fbase)
                     80:                        return fbase->get_junction(self, name);
                     81:                return 0; 
                     82:        }
                     83: 
                     84:        void set_field(const String& name, Value *value) {
1.4       paf        85:                if(value) // used in ^process to temporarily remove @main
                     86:                        value->set_name(name);
1.1       paf        87:                if(fbase && fbase->replace_field(name, value))
                     88:                        return;
                     89: 
                     90:                ffields.put(name, value);
                     91:        }
                     92: 
                     93: private:
                     94: 
                     95:        Value *get_field(const String& name) {
                     96:                Value *result=static_cast<Value *>(ffields.get(name));
                     97:                if(!result && fbase)
                     98:                        result=fbase->get_field(name);
                     99:                return result;
                    100:        }
                    101:                
                    102:        bool replace_field(const String& name, Value *value) {
                    103:                return 
                    104:                        (fbase && fbase->replace_field(name, value)) ||
                    105:                        ffields.put_replace(name, value);
                    106:        }
1.4       paf       107: 
                    108: private: // Temp_method
                    109: 
1.6     ! paf       110:        void put_method(const String& aname, Method *amethod);
1.1       paf       111:        
                    112: private:
                    113: 
1.6     ! paf       114:        bool read_only;
1.1       paf       115:        VClass *fbase;
                    116:        Hash ffields;
                    117:        Hash fmethods;
1.4       paf       118: };
                    119: 
                    120: class Temp_method {
                    121:        VClass& fclass;
                    122:        const String& fname;
                    123:        Method *saved_method;
                    124: public:
                    125:        Temp_method(VClass& aclass, const String& aname, Method *amethod) : 
                    126:                fclass(aclass),
                    127:                fname(aname),
                    128:                saved_method(aclass.get_method(aname)) {
                    129:                fclass.put_method(aname, amethod);
                    130:        }
                    131:        ~Temp_method() { 
                    132:                fclass.put_method(fname, saved_method);
                    133:        }
1.1       paf       134: };
                    135: 
                    136: #endif

E-mail: