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

1.6       paf         1: /** @file
                      2:        Parser: stateless class decls.
                      3: 
1.95      moko        4:        Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
                      5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <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.96    ! moko       11: #define IDENT_PA_VSTATELESS_CLASS_H "$Id: pa_vstateless_class.h,v 1.95 2023/09/26 20:49:13 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.96    ! moko       67: #ifdef FEATURE_GET_ELEMENT4CALL
        !            68:        /// same as get_element, but with proper error reporting
        !            69:        override Value* get_element4call(const String& aname) {
        !            70:                if(Value* result=get_element(*this, aname))
        !            71:                        return result;
        !            72:                return bark("%s method not found", &aname);
        !            73:        }
        !            74: #endif
        !            75: 
1.87      moko       76:        override const VJunction* put_element(const String& aname, Value* avalue) { return put_element(*this, aname, avalue); }
1.66      misha      77:        /// put_element with aself for VObject junctions
1.87      moko       78:        virtual const VJunction* put_element(Value& aself, const String& aname, Value* /*avalue*/) {
1.72      moko       79:                aself.bark("element can not be stored to %s", &aname); 
                     80:                return 0;
                     81:        }
1.66      misha      82: 
1.74      moko       83:        override Value& as_expr_result();
1.60      misha      84: 
1.66      misha      85:        Value* get_scalar(Value& aself);
                     86:        void set_scalar(Method* amethod);
1.2       paf        87: 
1.66      misha      88:        Value* get_default_getter(Value& aself, const String& aname);
                     89:        void set_default_getter(Method* amethod);
1.71      moko       90:        bool has_default_getter();
                     91: 
                     92:        VJunction* get_default_setter(Value& aself, const String& aname);
                     93:        void set_default_setter(Method* amethod);
                     94:        bool has_default_setter();
1.60      misha      95: 
1.66      misha      96:        void add_derived(VStateless_class &aclass);
1.61      misha      97: 
1.2       paf        98: public: // usage
                     99: 
1.94      moko      100:        static bool gall_vars_local; // @conf[] $MAIN:LOCALS
                    101: 
1.80      moko      102:        VStateless_class(VStateless_class* amethoded_donor=0):
1.43      paf       103:                flocked(false),
1.94      moko      104:                fall_vars_local(gall_vars_local),
1.89      moko      105:                fpartial(false),
                    106:                fcall_type(Method::CT_ANY),
1.66      misha     107:                fbase(0),
                    108:                fderived(0),
1.60      misha     109:                fscalar(0),
1.70      misha     110:                fdefault_getter(0),
1.89      moko      111:                fdefault_setter(0)
1.80      moko      112:        {
                    113:                if(amethoded_donor)
                    114:                        fmethods.merge_dont_replace(amethoded_donor->fmethods);
1.2       paf       115:        }
                    116: 
1.43      paf       117:        void lock() { flocked=true; }
                    118: 
1.87      moko      119:        Method* get_method(const String::Body &aname) const {
1.43      paf       120:                return fmethods.get(aname);
1.2       paf       121:        }
                    122: 
1.86      moko      123:        HashStringMethod& get_methods(){
1.65      misha     124:                return fmethods;
                    125:        }
                    126: 
1.61      misha     127:        bool is_vars_local(){
                    128:                return fall_vars_local;
                    129:        }
                    130: 
                    131:        void set_all_vars_local(){
1.57      misha     132:                fall_vars_local=true;
                    133:        }
                    134: 
1.61      misha     135:        bool is_partial(){
                    136:                return fpartial;
                    137:        }
                    138: 
                    139:        void set_partial(){
                    140:                fpartial=true;
1.57      misha     141:        }
                    142: 
1.70      misha     143:        Method::Call_type get_methods_call_type(){
                    144:                return fcall_type;
                    145:        }
                    146: 
                    147:        void set_methods_call_type(Method::Call_type call_type){
                    148:                if(fcall_type!=Method::CT_ANY)
1.87      moko      149:                        throw Exception(PARSER_RUNTIME, 0, "You can specify call type option in a class only once");
1.70      misha     150:                fcall_type=call_type;
                    151:        }
                    152: 
1.2       paf       153:        void add_native_method(
1.43      paf       154:                const char* cstr_name,
1.9       paf       155:                Method::Call_type call_type,
1.43      paf       156:                NativeCodePtr native_code,
1.87      moko      157:                int min_numbered_params_count,
                    158:                int max_numbered_params_count,
1.62      misha     159:                Method::Call_optimization call_optimization=Method::CO_WITHOUT_WCONTEXT);
1.66      misha     160: 
1.68      misha     161:        void set_method(const String& aname, Method* amethod);
                    162: 
1.66      misha     163:        /// overrided in VClass
1.68      misha     164:        virtual void real_set_method(const String& aname, Method* amethod);
1.66      misha     165:        virtual HashStringProperty* get_properties(){ return 0; };
                    166:        virtual void set_base(VStateless_class* abase);
1.2       paf       167:        
1.43      paf       168:        VStateless_class* base_class() { return fbase; }
1.2       paf       169: 
1.66      misha     170:        bool derived_from(VStateless_class& vclass){
1.77      moko      171:                return fbase==&vclass || ( fbase && fbase->derived_from(vclass) );
1.31      paf       172:        }
                    173: 
1.21      paf       174:        /// @returns new value for current class, used in classes/ & VClass
1.66      misha     175:        virtual Value* create_new_value(Pool&) { return 0; }
1.92      moko      176: 
                    177:        const Method* get_element_method(const String &aname) {  // not get_method() to allow $aname[$method]
                    178:                if(Value* value=get_element(aname))
                    179:                        if(Junction* junction=value->get_junction())
                    180:                                return junction->method;
                    181:                return 0;
                    182:        }
                    183: 
1.2       paf       184: };
                    185: 
1.6       paf       186: ///    Auto-object used for temporarily substituting/removing class method
1.2       paf       187: class Temp_method {
                    188:        VStateless_class& fclass;
                    189:        const String& fname;
1.43      paf       190:        Method* saved_method;
1.2       paf       191: public:
1.87      moko      192:        Temp_method(VStateless_class& aclass, const String& aname, Method* amethod) :
1.2       paf       193:                fclass(aclass),
                    194:                fname(aname),
                    195:                saved_method(aclass.get_method(aname)) {
1.67      misha     196:                fclass.set_method(aname, amethod);
1.2       paf       197:        }
                    198:        ~Temp_method() { 
1.67      misha     199:                fclass.set_method(fname, saved_method);
1.2       paf       200:        }
                    201: };
                    202: 
1.85      moko      203: 
                    204: class VBaseClassWrapper: public Value {
                    205: 
                    206:        VStateless_class& fclass;
                    207:        Value& fself;
                    208: 
                    209: public: // Value
                    210:        
                    211:        override const char* type() const { return fclass.type(); }
                    212:        override VStateless_class* get_class() { return &fclass; }
                    213:        override VStateless_class* base() { return fclass.base(); }
                    214: 
                    215:        override Value* get_element(const String& aname) { return fclass.get_element(fself, aname); }
                    216:        override const VJunction* put_element(const String& aname, Value* avalue) { return fclass.put_element(fself, aname, avalue); }
                    217:        override Value& as_expr_result(){ return fclass.as_expr_result(); }
                    218: 
                    219: public: // usage
                    220: 
                    221:        VBaseClassWrapper(VStateless_class& aclass, Value& aself): fclass(aclass), fself(aself) {};
                    222: };
                    223: 
1.2       paf       224: #endif

E-mail: