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

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.65    ! misha      11: static const char * const IDENT_VSTATELESS_CLASS_H="$Date: 2009-06-14 00:33:54 $";
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.65    ! misha     126:        HashString<Method*> get_methods(){
        !           127:                return fmethods;
        !           128:        }
        !           129: 
1.61      misha     130:        bool is_vars_local(){
                    131:                return fall_vars_local;
                    132:        }
                    133: 
                    134:        void set_all_vars_local(){
1.57      misha     135:                fall_vars_local=true;
                    136:        }
                    137: 
1.61      misha     138:        bool is_partial(){
                    139:                return fpartial;
                    140:        }
                    141: 
                    142:        void set_partial(){
                    143:                fpartial=true;
1.57      misha     144:        }
                    145: 
1.51      paf       146:        /// virtual for VClass to override to pre-cache property accessors into fields
                    147:        virtual void add_method(const String& name, Method& method);
1.40      paf       148:        
1.2       paf       149:        void add_native_method(
1.43      paf       150:                const char* cstr_name,
1.9       paf       151:                Method::Call_type call_type,
1.43      paf       152:                NativeCodePtr native_code,
1.62      misha     153:                int min_numbered_params_count, 
                    154:                int max_numbered_params_count, 
                    155:                Method::Call_optimization call_optimization=Method::CO_WITHOUT_WCONTEXT);
1.2       paf       156:        
1.43      paf       157:        void set_base(VStateless_class* abase) {
1.2       paf       158:                // remember the guy
1.32      paf       159:                fbase=abase;
1.2       paf       160:        }
1.43      paf       161:        VStateless_class* base_class() { return fbase; }
1.2       paf       162: 
1.30      paf       163:        bool derived_from(VStateless_class& vclass) {
1.2       paf       164:                return 
1.30      paf       165:                        fbase==&vclass || 
                    166:                        fbase && fbase->derived_from(vclass);
1.31      paf       167:        }
                    168: 
1.11      paf       169:        //@{
                    170:        /// @name just stubs, real onces defined below the hierarchy
1.43      paf       171:        virtual Value* get_field(const String&) { return 0; }
                    172:        virtual bool replace_field(const String&, Value*) { return false; }
1.21      paf       173:        //@}
                    174: 
                    175:        /// @returns new value for current class, used in classes/ & VClass
1.64      misha     176:        virtual Value* create_new_value(Pool&, HashStringValue* /*afields*/) { return 0; }
1.50      paf       177: 
                    178: protected:
                    179: 
                    180:        void fill_properties(HashStringValue& acache);
1.2       paf       181: 
1.49      paf       182: private:
1.2       paf       183: 
1.49      paf       184:        void put_method(const String& aname, Method* amethod);  
1.2       paf       185: };
                    186: 
1.6       paf       187: ///    Auto-object used for temporarily substituting/removing class method
1.2       paf       188: class Temp_method {
                    189:        VStateless_class& fclass;
                    190:        const String& fname;
1.43      paf       191:        Method* saved_method;
1.2       paf       192: public:
1.43      paf       193:        Temp_method(VStateless_class& aclass, const String& aname, Method* amethod) : 
1.2       paf       194:                fclass(aclass),
                    195:                fname(aname),
                    196:                saved_method(aclass.get_method(aname)) {
                    197:                fclass.put_method(aname, amethod);
                    198:        }
                    199:        ~Temp_method() { 
                    200:                fclass.put_method(fname, saved_method);
                    201:        }
                    202: };
                    203: 
                    204: #endif

E-mail: