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

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.70    ! misha      11: static const char * const IDENT_VSTATELESS_CLASS_H="$Date: 2009-08-14 10:39:48 $";
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.2       paf        19: 
1.38      paf        20: // defines
                     21: 
                     22: #define CLASS_NAME "CLASS"
1.54      misha      23: #define CLASS_NAMETEXT "CLASS_NAME"
1.63      misha      24: extern const String class_name, class_nametext;
1.38      paf        25: 
                     26: // forwards
                     27: 
1.43      paf        28: class VStateless_class;
1.59      misha      29: typedef Array<VStateless_class*> ArrayClass;
1.66      misha      30: typedef HashString<Property *> HashStringProperty;
                     31: typedef HashString<Method *> HashStringMethod;
1.43      paf        32: 
1.2       paf        33: class Temp_method;
                     34: 
1.6       paf        35: /**
1.30      paf        36:        object' class. stores
                     37:        - base: VClass::base()
                     38:        - methods: VStateless_class::fmethods
1.6       paf        39: 
1.30      paf        40:        @see Method, VStateless_object, Temp_method
1.6       paf        41: */
1.39      paf        42: class VStateless_class: public Value {
1.19      paf        43:        friend class Temp_method;
1.43      paf        44: 
                     45:        const String* fname;
                     46:        mutable const char* fname_cstr;
1.66      misha      47:        HashStringMethod fmethods;
1.43      paf        48: 
                     49:        bool flocked;
1.57      misha      50:        bool fall_vars_local;
1.61      misha      51:        bool fpartial;
1.70    ! misha      52:        Method::Call_type fcall_type;
1.43      paf        53: 
                     54: protected:
                     55: 
                     56:        VStateless_class* fbase;
1.68      misha      57:        /// all derived classes, recursively
1.66      misha      58:        ArrayClass fderived;
                     59: 
1.60      misha      60:        Method* fscalar;
1.58      misha      61:        Method* fdefault_getter;
1.43      paf        62: 
1.2       paf        63: public: // Value
                     64:        
1.43      paf        65:        const char* type() const { return "stateless_class"; }
1.2       paf        66: 
1.5       paf        67:        /// VStateless_class: this
1.69      misha      68:        override VStateless_class* get_class() { return this; }
1.41      paf        69:        /// VStateless_class: fbase
1.69      misha      70:        override VStateless_class* base() { return fbase; }
1.66      misha      71: 
                     72:        override Value* get_element(const String& aname) { return get_element(*this, aname); }
                     73:        /// get_element with aself for VObject junctions
                     74:        virtual Value* get_element(Value& aself, const String& aname);
                     75: 
                     76:        override const VJunction* put_element(const String& aname, Value* avalue, bool areplace) {      return put_element(*this, aname, avalue, areplace); }
                     77:        /// put_element with aself for VObject junctions
                     78:        virtual const VJunction* put_element(Value& /*aself*/, const String& aname, Value* avalue, bool areplace) {     return Value::put_element(aname, avalue, areplace); }
                     79: 
1.55      misha      80:        override Value& as_expr_result(bool /*return_string_as_is=false*/);
1.60      misha      81: 
1.66      misha      82:        Value* get_scalar(Value& aself);
                     83:        void set_scalar(Method* amethod);
1.2       paf        84: 
1.66      misha      85:        Value* get_default_getter(Value& aself, const String& aname);
                     86:        void set_default_getter(Method* amethod);
1.60      misha      87: 
1.66      misha      88:        void add_derived(VStateless_class &aclass);
1.61      misha      89: 
1.2       paf        90: public: // usage
                     91: 
1.43      paf        92:        VStateless_class(
                     93:                const String* aname=0, 
                     94:                VStateless_class* abase=0):
1.24      paf        95:                fname(aname),
1.43      paf        96:                flocked(false),
1.66      misha      97:                fbase(0),
                     98:                fderived(0),
1.58      misha      99:                fall_vars_local(false),
1.61      misha     100:                fpartial(false),
1.60      misha     101:                fscalar(0),
1.70    ! misha     102:                fdefault_getter(0),
        !           103:                fcall_type(Method::CT_ANY)
1.61      misha     104:                {
1.66      misha     105:                        set_base(abase);
1.2       paf       106:        }
                    107: 
1.43      paf       108:        void lock() { flocked=true; }
                    109: 
1.24      paf       110:        const String& name() const { 
                    111:                if(!fname) {
                    112:                        if(fbase)
                    113:                                return fbase->name();
                    114: 
1.56      misha     115:                        throw Exception(PARSER_RUNTIME,
1.24      paf       116:                                0,
                    117:                                "getting name of nameless class");
                    118:                }
                    119:                return *fname; 
                    120:        }
1.66      misha     121: 
1.43      paf       122:        const char* name_cstr() const{
1.66      misha     123:                if(!fname_cstr) // remembering last calculated, and can't reassign 'fname_cstr'!
                    124:                        fname_cstr=name().cstr();
                    125:                return fname_cstr;
1.24      paf       126:        }
1.61      misha     127: 
1.24      paf       128:        void set_name(const String& aname) {
                    129:                fname=&aname; 
1.66      misha     130:                fname_cstr=0;
1.24      paf       131:        }
                    132: 
1.43      paf       133:        Method* get_method(const String& aname) const { 
                    134:                return fmethods.get(aname);
1.2       paf       135:        }
                    136: 
1.66      misha     137:        HashStringMethod get_methods(){
1.65      misha     138:                return fmethods;
                    139:        }
                    140: 
1.61      misha     141:        bool is_vars_local(){
                    142:                return fall_vars_local;
                    143:        }
                    144: 
                    145:        void set_all_vars_local(){
1.57      misha     146:                fall_vars_local=true;
                    147:        }
                    148: 
1.61      misha     149:        bool is_partial(){
                    150:                return fpartial;
                    151:        }
                    152: 
                    153:        void set_partial(){
                    154:                fpartial=true;
1.57      misha     155:        }
                    156: 
1.70    ! misha     157:        Method::Call_type get_methods_call_type(){
        !           158:                return fcall_type;
        !           159:        }
        !           160: 
        !           161:        void set_methods_call_type(Method::Call_type call_type){
        !           162:                if(fcall_type!=Method::CT_ANY)
        !           163:                        throw Exception(PARSER_RUNTIME,
        !           164:                                                0,
        !           165:                                                "You can specify call type option in a class only once"
        !           166:                                        );
        !           167:                fcall_type=call_type;
        !           168:        }
        !           169: 
1.2       paf       170:        void add_native_method(
1.43      paf       171:                const char* cstr_name,
1.9       paf       172:                Method::Call_type call_type,
1.43      paf       173:                NativeCodePtr native_code,
1.62      misha     174:                int min_numbered_params_count, 
                    175:                int max_numbered_params_count, 
                    176:                Method::Call_optimization call_optimization=Method::CO_WITHOUT_WCONTEXT);
1.66      misha     177: 
1.68      misha     178:        void set_method(const String& aname, Method* amethod);
                    179: 
1.66      misha     180:        /// overrided in VClass
1.68      misha     181:        virtual void real_set_method(const String& aname, Method* amethod);
1.66      misha     182:        virtual HashStringProperty* get_properties(){ return 0; };
                    183:        virtual void set_base(VStateless_class* abase);
1.2       paf       184:        
1.43      paf       185:        VStateless_class* base_class() { return fbase; }
1.2       paf       186: 
1.66      misha     187:        bool derived_from(VStateless_class& vclass){
                    188:                return fbase==&vclass || fbase && fbase->derived_from(vclass);
1.31      paf       189:        }
                    190: 
1.21      paf       191:        /// @returns new value for current class, used in classes/ & VClass
1.66      misha     192:        virtual Value* create_new_value(Pool&) { return 0; }
1.2       paf       193: };
                    194: 
1.6       paf       195: ///    Auto-object used for temporarily substituting/removing class method
1.2       paf       196: class Temp_method {
                    197:        VStateless_class& fclass;
                    198:        const String& fname;
1.43      paf       199:        Method* saved_method;
1.2       paf       200: public:
1.43      paf       201:        Temp_method(VStateless_class& aclass, const String& aname, Method* amethod) : 
1.2       paf       202:                fclass(aclass),
                    203:                fname(aname),
                    204:                saved_method(aclass.get_method(aname)) {
1.67      misha     205:                fclass.set_method(aname, amethod);
1.2       paf       206:        }
                    207:        ~Temp_method() { 
1.67      misha     208:                fclass.set_method(fname, saved_method);
1.2       paf       209:        }
                    210: };
                    211: 
                    212: #endif

E-mail: