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

1.6       paf         1: /** @file
                      2:        Parser: stateless class decls.
                      3: 
1.73    ! moko        4:        Copyright (c) 2001-2012 Art. Lebedev Studio (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.73    ! moko       11: #define IDENT_PA_VSTATELESS_CLASS_H "$Id: 2010-09-06 21:17:49 $"
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.71      moko       62:        Method* fdefault_setter;
1.43      paf        63: 
1.2       paf        64: public: // Value
                     65:        
1.43      paf        66:        const char* type() const { return "stateless_class"; }
1.2       paf        67: 
1.5       paf        68:        /// VStateless_class: this
1.69      misha      69:        override VStateless_class* get_class() { return this; }
1.41      paf        70:        /// VStateless_class: fbase
1.69      misha      71:        override VStateless_class* base() { return fbase; }
1.66      misha      72: 
                     73:        override Value* get_element(const String& aname) { return get_element(*this, aname); }
                     74:        /// get_element with aself for VObject junctions
                     75:        virtual Value* get_element(Value& aself, const String& aname);
                     76: 
                     77:        override const VJunction* put_element(const String& aname, Value* avalue, bool areplace) {      return put_element(*this, aname, avalue, areplace); }
                     78:        /// put_element with aself for VObject junctions
1.72      moko       79:        virtual const VJunction* put_element(Value& aself, const String& aname, Value* /*avalue*/, bool /*areplace*/) { 
                     80:                aself.bark("element can not be stored to %s", &aname); 
                     81:                return 0;
                     82:        }
1.66      misha      83: 
1.55      misha      84:        override Value& as_expr_result(bool /*return_string_as_is=false*/);
1.60      misha      85: 
1.66      misha      86:        Value* get_scalar(Value& aself);
                     87:        void set_scalar(Method* amethod);
1.2       paf        88: 
1.66      misha      89:        Value* get_default_getter(Value& aself, const String& aname);
                     90:        void set_default_getter(Method* amethod);
1.71      moko       91:        bool has_default_getter();
                     92: 
                     93:        VJunction* get_default_setter(Value& aself, const String& aname);
                     94:        void set_default_setter(Method* amethod);
                     95:        bool has_default_setter();
1.60      misha      96: 
1.66      misha      97:        void add_derived(VStateless_class &aclass);
1.61      misha      98: 
1.2       paf        99: public: // usage
                    100: 
1.43      paf       101:        VStateless_class(
                    102:                const String* aname=0, 
                    103:                VStateless_class* abase=0):
1.24      paf       104:                fname(aname),
1.43      paf       105:                flocked(false),
1.66      misha     106:                fbase(0),
                    107:                fderived(0),
1.58      misha     108:                fall_vars_local(false),
1.61      misha     109:                fpartial(false),
1.60      misha     110:                fscalar(0),
1.70      misha     111:                fdefault_getter(0),
1.71      moko      112:                fdefault_setter(0),
1.70      misha     113:                fcall_type(Method::CT_ANY)
1.61      misha     114:                {
1.66      misha     115:                        set_base(abase);
1.2       paf       116:        }
                    117: 
1.43      paf       118:        void lock() { flocked=true; }
                    119: 
1.24      paf       120:        const String& name() const { 
                    121:                if(!fname) {
                    122:                        if(fbase)
                    123:                                return fbase->name();
                    124: 
1.56      misha     125:                        throw Exception(PARSER_RUNTIME,
1.24      paf       126:                                0,
                    127:                                "getting name of nameless class");
                    128:                }
                    129:                return *fname; 
                    130:        }
1.66      misha     131: 
1.43      paf       132:        const char* name_cstr() const{
1.66      misha     133:                if(!fname_cstr) // remembering last calculated, and can't reassign 'fname_cstr'!
                    134:                        fname_cstr=name().cstr();
                    135:                return fname_cstr;
1.24      paf       136:        }
1.61      misha     137: 
1.24      paf       138:        void set_name(const String& aname) {
                    139:                fname=&aname; 
1.66      misha     140:                fname_cstr=0;
1.24      paf       141:        }
                    142: 
1.43      paf       143:        Method* get_method(const String& aname) const { 
                    144:                return fmethods.get(aname);
1.2       paf       145:        }
                    146: 
1.66      misha     147:        HashStringMethod get_methods(){
1.65      misha     148:                return fmethods;
                    149:        }
                    150: 
1.61      misha     151:        bool is_vars_local(){
                    152:                return fall_vars_local;
                    153:        }
                    154: 
                    155:        void set_all_vars_local(){
1.57      misha     156:                fall_vars_local=true;
                    157:        }
                    158: 
1.61      misha     159:        bool is_partial(){
                    160:                return fpartial;
                    161:        }
                    162: 
                    163:        void set_partial(){
                    164:                fpartial=true;
1.57      misha     165:        }
                    166: 
1.70      misha     167:        Method::Call_type get_methods_call_type(){
                    168:                return fcall_type;
                    169:        }
                    170: 
                    171:        void set_methods_call_type(Method::Call_type call_type){
                    172:                if(fcall_type!=Method::CT_ANY)
                    173:                        throw Exception(PARSER_RUNTIME,
                    174:                                                0,
                    175:                                                "You can specify call type option in a class only once"
                    176:                                        );
                    177:                fcall_type=call_type;
                    178:        }
                    179: 
1.2       paf       180:        void add_native_method(
1.43      paf       181:                const char* cstr_name,
1.9       paf       182:                Method::Call_type call_type,
1.43      paf       183:                NativeCodePtr native_code,
1.62      misha     184:                int min_numbered_params_count, 
                    185:                int max_numbered_params_count, 
                    186:                Method::Call_optimization call_optimization=Method::CO_WITHOUT_WCONTEXT);
1.66      misha     187: 
1.68      misha     188:        void set_method(const String& aname, Method* amethod);
                    189: 
1.66      misha     190:        /// overrided in VClass
1.68      misha     191:        virtual void real_set_method(const String& aname, Method* amethod);
1.66      misha     192:        virtual HashStringProperty* get_properties(){ return 0; };
                    193:        virtual void set_base(VStateless_class* abase);
1.2       paf       194:        
1.43      paf       195:        VStateless_class* base_class() { return fbase; }
1.2       paf       196: 
1.66      misha     197:        bool derived_from(VStateless_class& vclass){
                    198:                return fbase==&vclass || fbase && fbase->derived_from(vclass);
1.31      paf       199:        }
                    200: 
1.21      paf       201:        /// @returns new value for current class, used in classes/ & VClass
1.66      misha     202:        virtual Value* create_new_value(Pool&) { return 0; }
1.2       paf       203: };
                    204: 
1.6       paf       205: ///    Auto-object used for temporarily substituting/removing class method
1.2       paf       206: class Temp_method {
                    207:        VStateless_class& fclass;
                    208:        const String& fname;
1.43      paf       209:        Method* saved_method;
1.2       paf       210: public:
1.43      paf       211:        Temp_method(VStateless_class& aclass, const String& aname, Method* amethod) : 
1.2       paf       212:                fclass(aclass),
                    213:                fname(aname),
                    214:                saved_method(aclass.get_method(aname)) {
1.67      misha     215:                fclass.set_method(aname, amethod);
1.2       paf       216:        }
                    217:        ~Temp_method() { 
1.67      misha     218:                fclass.set_method(fname, saved_method);
1.2       paf       219:        }
                    220: };
                    221: 
                    222: #endif

E-mail: