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

1.6       paf         1: /** @file
                      2:        Parser: stateless class decls.
                      3: 
1.62      misha       4:        Copyright (c) 2001-2009 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.63    ! misha      11: static const char * const IDENT_VSTATELESS_CLASS_H="$Date: 2009-05-04 09:25:32 $";
1.43      paf        12: 
                     13: // include
1.2       paf        14: 
1.45      paf        15: #include "pa_pool.h"
1.13      paf        16: #include "pa_hash.h"
1.2       paf        17: #include "pa_vjunction.h"
1.43      paf        18: #include "pa_method.h"
1.49      paf        19: #include "pa_vproperty.h"
1.2       paf        20: 
1.38      paf        21: // defines
                     22: 
                     23: #define CLASS_NAME "CLASS"
1.54      misha      24: #define CLASS_NAMETEXT "CLASS_NAME"
1.63    ! misha      25: extern const String class_name, class_nametext;
1.38      paf        26: 
                     27: // forwards
                     28: 
1.43      paf        29: class VStateless_class;
1.59      misha      30: typedef Array<VStateless_class*> ArrayClass;
1.43      paf        31: 
1.2       paf        32: class Temp_method;
                     33: 
1.6       paf        34: /**
1.30      paf        35:        object' class. stores
                     36:        - base: VClass::base()
                     37:        - methods: VStateless_class::fmethods
1.6       paf        38: 
1.30      paf        39:        @see Method, VStateless_object, Temp_method
1.6       paf        40: */
1.39      paf        41: class VStateless_class: public Value {
1.19      paf        42:        friend class Temp_method;
1.43      paf        43: 
                     44:        const String* fname;
                     45:        mutable const char* fname_cstr;
1.63    ! misha      46:        HashString<Method*> fmethods;
1.43      paf        47: 
                     48:        bool flocked;
1.57      misha      49:        bool fall_vars_local;
1.61      misha      50:        bool fpartial;
1.43      paf        51: 
                     52: protected:
                     53: 
                     54:        VStateless_class* fbase;
1.60      misha      55:        Method* fscalar;
1.58      misha      56:        Method* fdefault_getter;
1.61      misha      57:        Method* fdefault_setter;
1.43      paf        58: 
1.2       paf        59: public: // Value
                     60:        
1.43      paf        61:        const char* type() const { return "stateless_class"; }
1.2       paf        62: 
1.5       paf        63:        /// VStateless_class: this
1.43      paf        64:        override VStateless_class *get_class() { return this; }
1.41      paf        65:        /// VStateless_class: fbase
1.43      paf        66:        override Value* base() { return fbase; }
1.58      misha      67:        override Value* get_element(const String& aname, Value& aself, bool alooking_up);
1.55      misha      68:        override Value& as_expr_result(bool /*return_string_as_is=false*/);
1.60      misha      69: 
1.58      misha      70:        override Value* get_default_getter(Value& aself, const String& aname);
                     71:        override void set_default_getter(Method* amethod);
1.2       paf        72: 
1.60      misha      73:        override Value* get_scalar(Value& aself);
                     74:        override void set_scalar(Method* amethod);
                     75: 
1.61      misha      76:        override VJunction* get_default_setter(Value& aself, const String& aname);
                     77:        override void set_default_setter(Method* amethod);
                     78: 
1.2       paf        79: public: // usage
                     80: 
1.43      paf        81:        VStateless_class(
                     82:                const String* aname=0, 
                     83:                VStateless_class* abase=0):
1.24      paf        84:                fname(aname),
1.43      paf        85:                flocked(false),
1.57      misha      86:                fbase(abase),
1.58      misha      87:                fall_vars_local(false),
1.61      misha      88:                fpartial(false),
1.60      misha      89:                fscalar(0),
1.61      misha      90:                fdefault_getter(0),
                     91:                fdefault_setter(0)
                     92:                {
1.2       paf        93:        }
                     94: 
1.43      paf        95:        void lock() { flocked=true; }
                     96: 
1.24      paf        97:        const String& name() const { 
                     98:                if(!fname) {
                     99:                        if(fbase)
                    100:                                return fbase->name();
                    101: 
1.56      misha     102:                        throw Exception(PARSER_RUNTIME,
1.24      paf       103:                                0,
                    104:                                "getting name of nameless class");
                    105:                }
                    106: 
                    107:                return *fname; 
                    108:        }
1.43      paf       109:        const char* name_cstr() const{
                    110:                if(this) {
                    111:                        if(!fname_cstr) // remembering last calculated, and can't reassign 'fname_cstr'!
                    112:                                fname_cstr=name().cstr();
                    113:                        return fname_cstr;
                    114:                } else
                    115:                        return "<unknown>";
1.24      paf       116:        }
1.61      misha     117: 
1.24      paf       118:        void set_name(const String& aname) {
                    119:                fname=&aname; 
                    120:        }
                    121: 
1.43      paf       122:        Method* get_method(const String& aname) const { 
                    123:                return fmethods.get(aname);
1.2       paf       124:        }
                    125: 
1.61      misha     126:        bool is_vars_local(){
                    127:                return fall_vars_local;
                    128:        }
                    129: 
                    130:        void set_all_vars_local(){
1.57      misha     131:                fall_vars_local=true;
                    132:        }
                    133: 
1.61      misha     134:        bool is_partial(){
                    135:                return fpartial;
                    136:        }
                    137: 
                    138:        void set_partial(){
                    139:                fpartial=true;
1.57      misha     140:        }
                    141: 
1.51      paf       142:        /// virtual for VClass to override to pre-cache property accessors into fields
                    143:        virtual void add_method(const String& name, Method& method);
1.40      paf       144:        
1.2       paf       145:        void add_native_method(
1.43      paf       146:                const char* cstr_name,
1.9       paf       147:                Method::Call_type call_type,
1.43      paf       148:                NativeCodePtr native_code,
1.62      misha     149:                int min_numbered_params_count, 
                    150:                int max_numbered_params_count, 
                    151:                Method::Call_optimization call_optimization=Method::CO_WITHOUT_WCONTEXT);
1.2       paf       152:        
1.43      paf       153:        void set_base(VStateless_class* abase) {
1.2       paf       154:                // remember the guy
1.32      paf       155:                fbase=abase;
1.2       paf       156:        }
1.43      paf       157:        VStateless_class* base_class() { return fbase; }
1.2       paf       158: 
1.30      paf       159:        bool derived_from(VStateless_class& vclass) {
1.2       paf       160:                return 
1.30      paf       161:                        fbase==&vclass || 
                    162:                        fbase && fbase->derived_from(vclass);
1.31      paf       163:        }
                    164: 
1.11      paf       165:        //@{
                    166:        /// @name just stubs, real onces defined below the hierarchy
1.43      paf       167:        virtual Value* get_field(const String&) { return 0; }
                    168:        virtual bool replace_field(const String&, Value*) { return false; }
1.21      paf       169:        //@}
                    170: 
                    171:        /// @returns new value for current class, used in classes/ & VClass
1.52      paf       172:        virtual Value* create_new_value(Pool&, HashStringValue& /*afields*/) { return 0; }
1.50      paf       173: 
                    174: protected:
                    175: 
                    176:        void fill_properties(HashStringValue& acache);
1.2       paf       177: 
1.49      paf       178: private:
1.2       paf       179: 
1.49      paf       180:        void put_method(const String& aname, Method* amethod);  
1.2       paf       181: };
                    182: 
1.6       paf       183: ///    Auto-object used for temporarily substituting/removing class method
1.2       paf       184: class Temp_method {
                    185:        VStateless_class& fclass;
                    186:        const String& fname;
1.43      paf       187:        Method* saved_method;
1.2       paf       188: public:
1.43      paf       189:        Temp_method(VStateless_class& aclass, const String& aname, Method* amethod) : 
1.2       paf       190:                fclass(aclass),
                    191:                fname(aname),
                    192:                saved_method(aclass.get_method(aname)) {
                    193:                fclass.put_method(aname, amethod);
                    194:        }
                    195:        ~Temp_method() { 
                    196:                fclass.put_method(fname, saved_method);
                    197:        }
                    198: };
                    199: 
                    200: #endif

E-mail: