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

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.5     ! paf         6:        $Id: pa_vclass.h,v 1.4 2001/03/12 09:31:26 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), 
                     40:                fbase(0),
                     41:                ffields(apool),
                     42:                fmethods(apool) {
                     43:        }
                     44: 
1.5     ! paf        45:        Method *get_method(const String& name) { 
        !            46:                return static_cast<Method *>(fmethods.get(name)); 
        !            47:        }
        !            48: 
1.1       paf        49:        void add_method(const String& name, Method& method) {
                     50:                fmethods.put(name, &method);
                     51:        }
1.3       paf        52:        void add_native_method(
                     53:                const char *cstr_name,
                     54:                Native_code_ptr native_code,
                     55:                int min_numbered_params_count, int max_numbered_params_count);
1.1       paf        56: //     Hash& methods() { return fmethods; }
                     57:        
                     58:        void set_base(VClass& abase) {
                     59:                // remember the guy
                     60:                fbase=&abase;
                     61:        }
                     62:        VClass *base() { return fbase; }
                     63: 
                     64:        bool is_or_derived_from(VClass& vclass) {
                     65:                return 
                     66:                        this==&vclass || 
                     67:                        fbase && fbase->is_or_derived_from(vclass);
                     68:        }
                     69: 
                     70:        Junction *get_junction(VAliased& self, const String& name) {
                     71:                if(Method *method=static_cast<Method *>(fmethods.get(name)))
                     72:                        return NEW Junction(pool(), self, this, method, 0,0,0,0);
                     73:                if(fbase)
                     74:                        return fbase->get_junction(self, name);
                     75:                return 0; 
                     76:        }
                     77: 
                     78:        void set_field(const String& name, Value *value) {
1.4       paf        79:                if(value) // used in ^process to temporarily remove @main
                     80:                        value->set_name(name);
1.1       paf        81:                if(fbase && fbase->replace_field(name, value))
                     82:                        return;
                     83: 
                     84:                ffields.put(name, value);
                     85:        }
                     86: 
                     87: private:
                     88: 
                     89:        Value *get_field(const String& name) {
                     90:                Value *result=static_cast<Value *>(ffields.get(name));
                     91:                if(!result && fbase)
                     92:                        result=fbase->get_field(name);
                     93:                return result;
                     94:        }
                     95:                
                     96:        bool replace_field(const String& name, Value *value) {
                     97:                return 
                     98:                        (fbase && fbase->replace_field(name, value)) ||
                     99:                        ffields.put_replace(name, value);
                    100:        }
1.4       paf       101: 
                    102: private: // Temp_method
                    103: 
                    104:        void put_method(const String& name, Method *method) { fmethods.put(name, method); }
1.1       paf       105:        
                    106: private:
                    107: 
                    108:        VClass *fbase;
                    109:        Hash ffields;
                    110:        Hash fmethods;
1.4       paf       111: };
                    112: 
                    113: class Temp_method {
                    114:        VClass& fclass;
                    115:        const String& fname;
                    116:        Method *saved_method;
                    117: public:
                    118:        Temp_method(VClass& aclass, const String& aname, Method *amethod) : 
                    119:                fclass(aclass),
                    120:                fname(aname),
                    121:                saved_method(aclass.get_method(aname)) {
                    122:                fclass.put_method(aname, amethod);
                    123:        }
                    124:        ~Temp_method() { 
                    125:                fclass.put_method(fname, saved_method);
                    126:        }
1.1       paf       127: };
                    128: 
                    129: #endif

E-mail: