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

1.6       paf         1: /** @file
                      2:        Parser: stateless class decls.
                      3: 
1.90      moko        4:        Copyright (c) 2001-2017 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.91    ! moko       11: #define IDENT_PA_VSTATELESS_CLASS_H "$Id: pa_vstateless_class.h,v 1.90 2017/02/07 22:00:51 moko Exp $"
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: // forwards
                     21: 
1.43      paf        22: class VStateless_class;
1.59      misha      23: typedef Array<VStateless_class*> ArrayClass;
1.91    ! moko       24: typedef HASH_STRING<Property *> HashStringProperty;
        !            25: typedef HASH_STRING<Method *> HashStringMethod;
1.43      paf        26: 
1.2       paf        27: class Temp_method;
                     28: 
1.6       paf        29: /**
1.30      paf        30:        object' class. stores
                     31:        - base: VClass::base()
                     32:        - methods: VStateless_class::fmethods
1.6       paf        33: 
1.30      paf        34:        @see Method, VStateless_object, Temp_method
1.6       paf        35: */
1.39      paf        36: class VStateless_class: public Value {
1.19      paf        37:        friend class Temp_method;
1.43      paf        38: 
1.66      misha      39:        HashStringMethod fmethods;
1.43      paf        40: 
                     41:        bool flocked;
1.57      misha      42:        bool fall_vars_local;
1.61      misha      43:        bool fpartial;
1.70      misha      44:        Method::Call_type fcall_type;
1.43      paf        45: 
                     46: protected:
                     47: 
                     48:        VStateless_class* fbase;
1.68      misha      49:        /// all derived classes, recursively
1.66      misha      50:        ArrayClass fderived;
                     51: 
1.60      misha      52:        Method* fscalar;
1.58      misha      53:        Method* fdefault_getter;
1.71      moko       54:        Method* fdefault_setter;
1.43      paf        55: 
1.2       paf        56: public: // Value
                     57:        
1.5       paf        58:        /// VStateless_class: this
1.69      misha      59:        override VStateless_class* get_class() { return this; }
1.41      paf        60:        /// VStateless_class: fbase
1.69      misha      61:        override VStateless_class* base() { return fbase; }
1.66      misha      62: 
                     63:        override Value* get_element(const String& aname) { return get_element(*this, aname); }
                     64:        /// get_element with aself for VObject junctions
                     65:        virtual Value* get_element(Value& aself, const String& aname);
                     66: 
1.87      moko       67:        override const VJunction* put_element(const String& aname, Value* avalue) { return put_element(*this, aname, avalue); }
1.66      misha      68:        /// put_element with aself for VObject junctions
1.87      moko       69:        virtual const VJunction* put_element(Value& aself, const String& aname, Value* /*avalue*/) {
1.72      moko       70:                aself.bark("element can not be stored to %s", &aname); 
                     71:                return 0;
                     72:        }
1.66      misha      73: 
1.74      moko       74:        override Value& as_expr_result();
1.60      misha      75: 
1.66      misha      76:        Value* get_scalar(Value& aself);
                     77:        void set_scalar(Method* amethod);
1.2       paf        78: 
1.66      misha      79:        Value* get_default_getter(Value& aself, const String& aname);
                     80:        void set_default_getter(Method* amethod);
1.71      moko       81:        bool has_default_getter();
                     82: 
                     83:        VJunction* get_default_setter(Value& aself, const String& aname);
                     84:        void set_default_setter(Method* amethod);
                     85:        bool has_default_setter();
1.60      misha      86: 
1.66      misha      87:        void add_derived(VStateless_class &aclass);
1.61      misha      88: 
1.2       paf        89: public: // usage
                     90: 
1.80      moko       91:        VStateless_class(VStateless_class* amethoded_donor=0):
1.43      paf        92:                flocked(false),
1.89      moko       93:                fall_vars_local(false),
                     94:                fpartial(false),
                     95:                fcall_type(Method::CT_ANY),
1.66      misha      96:                fbase(0),
                     97:                fderived(0),
1.60      misha      98:                fscalar(0),
1.70      misha      99:                fdefault_getter(0),
1.89      moko      100:                fdefault_setter(0)
1.80      moko      101:        {
                    102:                if(amethoded_donor)
                    103:                        fmethods.merge_dont_replace(amethoded_donor->fmethods);
1.2       paf       104:        }
                    105: 
1.43      paf       106:        void lock() { flocked=true; }
                    107: 
1.87      moko      108:        Method* get_method(const String::Body &aname) const {
1.43      paf       109:                return fmethods.get(aname);
1.2       paf       110:        }
                    111: 
1.86      moko      112:        HashStringMethod& get_methods(){
1.65      misha     113:                return fmethods;
                    114:        }
                    115: 
1.61      misha     116:        bool is_vars_local(){
                    117:                return fall_vars_local;
                    118:        }
                    119: 
                    120:        void set_all_vars_local(){
1.57      misha     121:                fall_vars_local=true;
                    122:        }
                    123: 
1.61      misha     124:        bool is_partial(){
                    125:                return fpartial;
                    126:        }
                    127: 
                    128:        void set_partial(){
                    129:                fpartial=true;
1.57      misha     130:        }
                    131: 
1.70      misha     132:        Method::Call_type get_methods_call_type(){
                    133:                return fcall_type;
                    134:        }
                    135: 
                    136:        void set_methods_call_type(Method::Call_type call_type){
                    137:                if(fcall_type!=Method::CT_ANY)
1.87      moko      138:                        throw Exception(PARSER_RUNTIME, 0, "You can specify call type option in a class only once");
1.70      misha     139:                fcall_type=call_type;
                    140:        }
                    141: 
1.2       paf       142:        void add_native_method(
1.43      paf       143:                const char* cstr_name,
1.9       paf       144:                Method::Call_type call_type,
1.43      paf       145:                NativeCodePtr native_code,
1.87      moko      146:                int min_numbered_params_count,
                    147:                int max_numbered_params_count,
1.62      misha     148:                Method::Call_optimization call_optimization=Method::CO_WITHOUT_WCONTEXT);
1.66      misha     149: 
1.68      misha     150:        void set_method(const String& aname, Method* amethod);
                    151: 
1.66      misha     152:        /// overrided in VClass
1.68      misha     153:        virtual void real_set_method(const String& aname, Method* amethod);
1.66      misha     154:        virtual HashStringProperty* get_properties(){ return 0; };
                    155:        virtual void set_base(VStateless_class* abase);
1.2       paf       156:        
1.43      paf       157:        VStateless_class* base_class() { return fbase; }
1.2       paf       158: 
1.66      misha     159:        bool derived_from(VStateless_class& vclass){
1.77      moko      160:                return fbase==&vclass || ( fbase && fbase->derived_from(vclass) );
1.31      paf       161:        }
                    162: 
1.21      paf       163:        /// @returns new value for current class, used in classes/ & VClass
1.66      misha     164:        virtual Value* create_new_value(Pool&) { return 0; }
1.2       paf       165: };
                    166: 
1.6       paf       167: ///    Auto-object used for temporarily substituting/removing class method
1.2       paf       168: class Temp_method {
                    169:        VStateless_class& fclass;
                    170:        const String& fname;
1.43      paf       171:        Method* saved_method;
1.2       paf       172: public:
1.87      moko      173:        Temp_method(VStateless_class& aclass, const String& aname, Method* amethod) :
1.2       paf       174:                fclass(aclass),
                    175:                fname(aname),
                    176:                saved_method(aclass.get_method(aname)) {
1.67      misha     177:                fclass.set_method(aname, amethod);
1.2       paf       178:        }
                    179:        ~Temp_method() { 
1.67      misha     180:                fclass.set_method(fname, saved_method);
1.2       paf       181:        }
                    182: };
                    183: 
1.85      moko      184: 
                    185: class VBaseClassWrapper: public Value {
                    186: 
                    187:        VStateless_class& fclass;
                    188:        Value& fself;
                    189: 
                    190: public: // Value
                    191:        
                    192:        override const char* type() const { return fclass.type(); }
                    193:        override VStateless_class* get_class() { return &fclass; }
                    194:        override VStateless_class* base() { return fclass.base(); }
                    195: 
                    196:        override Value* get_element(const String& aname) { return fclass.get_element(fself, aname); }
                    197:        override const VJunction* put_element(const String& aname, Value* avalue) { return fclass.put_element(fself, aname, avalue); }
                    198:        override Value& as_expr_result(){ return fclass.as_expr_result(); }
                    199: 
                    200: public: // usage
                    201: 
                    202:        VBaseClassWrapper(VStateless_class& aclass, Value& aself): fclass(aclass), fself(aself) {};
                    203: };
                    204: 
1.2       paf       205: #endif

E-mail: