Annotation of parser3/src/types/pa_vstateless_class.h, revision 1.31

1.6       paf         1: /** @file
                      2:        Parser: stateless class decls.
                      3: 
1.22      paf         4:        Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.23      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.2       paf         6: */
                      7: 
                      8: #ifndef PA_VSTATELESS_CLASS_H
                      9: #define PA_VSTATELESS_CLASS_H
1.27      paf        10: 
1.31    ! paf        11: static const char* IDENT_VSTATELESS_CLASS_H="$Date: 2002/08/12 14:21:52 $";
1.2       paf        12: 
1.13      paf        13: #include "pa_hash.h"
1.2       paf        14: #include "pa_vjunction.h"
                     15: 
                     16: class Temp_method;
                     17: 
1.6       paf        18: /**
1.30      paf        19:        object' class. stores
                     20:        - base: VClass::base()
                     21:        - methods: VStateless_class::fmethods
1.6       paf        22: 
1.30      paf        23:        @see Method, VStateless_object, Temp_method
1.6       paf        24: */
1.29      paf        25: class VStateless_class : public Value {
1.19      paf        26:        friend class Temp_method;
1.2       paf        27: public: // Value
                     28:        
                     29:        const char *type() const { return "stateless_class"; }
                     30: 
1.5       paf        31:        /// VStateless_class: this
                     32:        VStateless_class *get_class() { return this; }
1.7       paf        33:        
                     34:        /// VStateless_class: +$method
                     35:        Value *get_element(const String& aname) {
                     36:                // $method=junction(self+class+method)
1.31    ! paf        37:                if(Junction *junction=get_junction(aname, *this))
1.15      parser     38:                        return new(junction->pool()) VJunction(*junction);
1.7       paf        39:                
                     40:                return 0;
                     41:        }
1.2       paf        42: 
                     43: public: // usage
                     44: 
1.24      paf        45:        VStateless_class(Pool& apool, 
                     46:                const String *aname=0, 
1.29      paf        47:                VStateless_class *abase=0) : Value(apool), 
1.24      paf        48:                fname(aname),
1.3       paf        49:                fbase(abase),
1.2       paf        50:                fmethods(apool) {
                     51:        }
                     52: 
1.24      paf        53:        const String& name() const { 
                     54:                if(!fname) {
                     55:                        if(fbase)
                     56:                                return fbase->name();
                     57: 
                     58:                        throw Exception("parser.runtime",
                     59:                                0,
                     60:                                "getting name of nameless class");
                     61:                }
                     62: 
                     63:                return *fname; 
                     64:        }
                     65:        const char *name_cstr() const {
                     66:                return this?name().cstr():"<unknown>";
                     67:        }
                     68:        void set_name(const String& aname) {
                     69:                fname=&aname; 
                     70:        }
                     71: 
1.26      paf        72:        Method *get_method(const String& name) const { 
1.2       paf        73:                return static_cast<Method *>(fmethods.get(name)); 
                     74:        }
                     75: 
                     76:        void add_method(const String& name, Method& method) {
                     77:                put_method(name, &method);
                     78:        }
                     79:        void add_native_method(
                     80:                const char *cstr_name,
1.9       paf        81:                Method::Call_type call_type,
1.2       paf        82:                Native_code_ptr native_code,
                     83:                int min_numbered_params_count, int max_numbered_params_count);
                     84:        
                     85:        void set_base(VStateless_class& abase) {
                     86:                // remember the guy
                     87:                fbase=&abase;
                     88:        }
1.30      paf        89:        VStateless_class *base_class() { return fbase; }
1.2       paf        90: 
1.30      paf        91:        bool derived_from(VStateless_class& vclass) {
1.2       paf        92:                return 
1.30      paf        93:                        fbase==&vclass || 
                     94:                        fbase && fbase->derived_from(vclass);
1.31    ! paf        95:        }
        !            96: 
        !            97:        Junction *get_junction(const String& name, Value& self) {
        !            98:                if(Method *method=static_cast<Method *>(fmethods.get(name)))
        !            99:                        return new(name.pool()) Junction(name.pool(), self, this, method, 0,0,0,0);
        !           100:                if(fbase)
        !           101:                        return fbase->get_junction(name, self);
        !           102:                return 0; 
1.2       paf       103:        }
                    104: 
1.11      paf       105:        //@{
                    106:        /// @name just stubs, real onces defined below the hierarchy
1.18      parser    107:        virtual Value *get_field(const String& ) { return 0; }
                    108:        virtual bool replace_field(const String& , Value *) { return false; }
1.21      paf       109:        //@}
                    110: 
                    111:        /// @returns new value for current class, used in classes/ & VClass
1.18      parser    112:        virtual Value *create_new_value(Pool& ) { return 0; }
1.2       paf       113: 
                    114: private: // Temp_method
                    115: 
1.8       paf       116:        void put_method(const String& aname, Method *amethod) {
                    117:                fmethods.put(aname, amethod); 
                    118:        }
1.2       paf       119:        
                    120: private:
                    121: 
1.24      paf       122:        const String *fname;
1.2       paf       123:        Hash fmethods;
                    124: 
                    125: protected:
                    126: 
                    127:        VStateless_class *fbase;
                    128: 
                    129: };
                    130: 
1.6       paf       131: ///    Auto-object used for temporarily substituting/removing class method
1.2       paf       132: class Temp_method {
                    133:        VStateless_class& fclass;
                    134:        const String& fname;
                    135:        Method *saved_method;
                    136: public:
                    137:        Temp_method(VStateless_class& aclass, const String& aname, Method *amethod) : 
                    138:                fclass(aclass),
                    139:                fname(aname),
                    140:                saved_method(aclass.get_method(aname)) {
                    141:                fclass.put_method(aname, amethod);
                    142:        }
                    143:        ~Temp_method() { 
                    144:                fclass.put_method(fname, saved_method);
                    145:        }
                    146: };
                    147: 
                    148: #endif

E-mail: