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

1.6       paf         1: /** @file
                      2:        Parser: stateless class decls.
                      3: 
1.100     moko        4:        Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.95      moko        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.102   ! moko       11: #define IDENT_PA_VSTATELESS_CLASS_H "$Id: pa_vstateless_class.h,v 1.101 2026/10/12 21:59:21 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.101     moko       29: #ifdef FEATURE_GET_ELEMENT4CALL
                     30: 
                     31: #ifndef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
                     32: #error FEATURE_GET_ELEMENT4CALL requires OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL: to eliminate the base get_element call, CLASS and CLASS_NAME must be compiled into the bytecode
                     33: #endif
                     34: 
                     35: #define PA_MAYBE(call)
                     36: 
                     37: #else
                     38: 
                     39: // the call returns method, and CLASS and CLASS_NAME if they are not compiled into the bytecode
                     40: #define PA_MAYBE(call) if(Value* result=call) return result
                     41: 
                     42: #endif
                     43: 
                     44: /// for use in get_element: no call, $method is returned by get_element4call ONLY, CLASS and CLASS_NAME are compiled into the bytecode
                     45: #define MAYBE_GET_SPECIAL_OR_METHOD(call) PA_MAYBE(call)
                     46: 
                     47: /// for use in get_element of the classes without methods: no call, CLASS and CLASS_NAME are compiled into the bytecode
                     48: /// when a method is added to such class, MAYBE_GET_SPECIAL must be replaced with 'if'
                     49: #define MAYBE_GET_SPECIAL(call) PA_MAYBE(call)
                     50: 
1.6       paf        51: /**
1.30      paf        52:        object' class. stores
                     53:        - base: VClass::base()
                     54:        - methods: VStateless_class::fmethods
1.6       paf        55: 
1.30      paf        56:        @see Method, VStateless_object, Temp_method
1.6       paf        57: */
1.39      paf        58: class VStateless_class: public Value {
1.19      paf        59:        friend class Temp_method;
1.43      paf        60: 
1.66      misha      61:        HashStringMethod fmethods;
1.43      paf        62: 
                     63:        bool flocked;
1.57      misha      64:        bool fall_vars_local;
1.61      misha      65:        bool fpartial;
1.70      misha      66:        Method::Call_type fcall_type;
1.43      paf        67: 
                     68: protected:
                     69: 
                     70:        VStateless_class* fbase;
1.68      misha      71:        /// all derived classes, recursively
1.66      misha      72:        ArrayClass fderived;
                     73: 
1.60      misha      74:        Method* fscalar;
1.58      misha      75:        Method* fdefault_getter;
1.71      moko       76:        Method* fdefault_setter;
1.43      paf        77: 
1.2       paf        78: public: // Value
                     79:        
1.5       paf        80:        /// VStateless_class: this
1.69      misha      81:        override VStateless_class* get_class() { return this; }
1.41      paf        82:        /// VStateless_class: fbase
1.69      misha      83:        override VStateless_class* base() { return fbase; }
1.66      misha      84: 
                     85:        override Value* get_element(const String& aname) { return get_element(*this, aname); }
                     86:        /// get_element with aself for VObject junctions
                     87:        virtual Value* get_element(Value& aself, const String& aname);
                     88: 
1.96      moko       89: #ifdef FEATURE_GET_ELEMENT4CALL
                     90:        /// same as get_element, but with proper error reporting
                     91:        override Value* get_element4call(const String& aname) {
                     92:                if(Value* result=get_element(*this, aname))
                     93:                        return result;
                     94:                return bark("%s method not found", &aname);
                     95:        }
                     96: #endif
                     97: 
1.87      moko       98:        override const VJunction* put_element(const String& aname, Value* avalue) { return put_element(*this, aname, avalue); }
1.66      misha      99:        /// put_element with aself for VObject junctions
1.87      moko      100:        virtual const VJunction* put_element(Value& aself, const String& aname, Value* /*avalue*/) {
1.98      moko      101:                aself.bark("element cannot be stored into %s", &aname); 
1.72      moko      102:                return 0;
                    103:        }
1.66      misha     104: 
1.74      moko      105:        override Value& as_expr_result();
1.60      misha     106: 
1.66      misha     107:        Value* get_scalar(Value& aself);
                    108:        void set_scalar(Method* amethod);
1.2       paf       109: 
1.66      misha     110:        Value* get_default_getter(Value& aself, const String& aname);
                    111:        void set_default_getter(Method* amethod);
1.71      moko      112:        bool has_default_getter();
                    113: 
                    114:        VJunction* get_default_setter(Value& aself, const String& aname);
                    115:        void set_default_setter(Method* amethod);
                    116:        bool has_default_setter();
1.60      misha     117: 
1.66      misha     118:        void add_derived(VStateless_class &aclass);
1.61      misha     119: 
1.2       paf       120: public: // usage
                    121: 
1.102   ! moko      122:        static bool gall_vars_local; // @conf[] $MAIN:LOCALS, not thread local: never changes
1.94      moko      123: 
1.80      moko      124:        VStateless_class(VStateless_class* amethoded_donor=0):
1.43      paf       125:                flocked(false),
1.94      moko      126:                fall_vars_local(gall_vars_local),
1.89      moko      127:                fpartial(false),
                    128:                fcall_type(Method::CT_ANY),
1.66      misha     129:                fbase(0),
                    130:                fderived(0),
1.60      misha     131:                fscalar(0),
1.70      misha     132:                fdefault_getter(0),
1.89      moko      133:                fdefault_setter(0)
1.80      moko      134:        {
                    135:                if(amethoded_donor)
                    136:                        fmethods.merge_dont_replace(amethoded_donor->fmethods);
1.2       paf       137:        }
                    138: 
1.43      paf       139:        void lock() { flocked=true; }
                    140: 
1.87      moko      141:        Method* get_method(const String::Body &aname) const {
1.43      paf       142:                return fmethods.get(aname);
1.2       paf       143:        }
                    144: 
1.86      moko      145:        HashStringMethod& get_methods(){
1.65      misha     146:                return fmethods;
                    147:        }
                    148: 
1.61      misha     149:        bool is_vars_local(){
                    150:                return fall_vars_local;
                    151:        }
                    152: 
                    153:        void set_all_vars_local(){
1.57      misha     154:                fall_vars_local=true;
                    155:        }
                    156: 
1.61      misha     157:        bool is_partial(){
                    158:                return fpartial;
                    159:        }
                    160: 
                    161:        void set_partial(){
                    162:                fpartial=true;
1.57      misha     163:        }
                    164: 
1.70      misha     165:        Method::Call_type get_methods_call_type(){
                    166:                return fcall_type;
                    167:        }
                    168: 
                    169:        void set_methods_call_type(Method::Call_type call_type){
                    170:                if(fcall_type!=Method::CT_ANY)
1.87      moko      171:                        throw Exception(PARSER_RUNTIME, 0, "You can specify call type option in a class only once");
1.70      misha     172:                fcall_type=call_type;
                    173:        }
                    174: 
1.2       paf       175:        void add_native_method(
1.43      paf       176:                const char* cstr_name,
1.9       paf       177:                Method::Call_type call_type,
1.43      paf       178:                NativeCodePtr native_code,
1.87      moko      179:                int min_numbered_params_count,
                    180:                int max_numbered_params_count,
1.62      misha     181:                Method::Call_optimization call_optimization=Method::CO_WITHOUT_WCONTEXT);
1.66      misha     182: 
1.68      misha     183:        void set_method(const String& aname, Method* amethod);
                    184: 
1.66      misha     185:        /// overrided in VClass
1.68      misha     186:        virtual void real_set_method(const String& aname, Method* amethod);
1.66      misha     187:        virtual HashStringProperty* get_properties(){ return 0; };
                    188:        virtual void set_base(VStateless_class* abase);
1.2       paf       189:        
1.43      paf       190:        VStateless_class* base_class() { return fbase; }
1.2       paf       191: 
1.66      misha     192:        bool derived_from(VStateless_class& vclass){
1.77      moko      193:                return fbase==&vclass || ( fbase && fbase->derived_from(vclass) );
1.31      paf       194:        }
                    195: 
1.21      paf       196:        /// @returns new value for current class, used in classes/ & VClass
1.66      misha     197:        virtual Value* create_new_value(Pool&) { return 0; }
1.92      moko      198: 
                    199:        const Method* get_element_method(const String &aname) {  // not get_method() to allow $aname[$method]
                    200:                if(Value* value=get_element(aname))
                    201:                        if(Junction* junction=value->get_junction())
                    202:                                return junction->method;
                    203:                return 0;
                    204:        }
                    205: 
1.2       paf       206: };
                    207: 
1.6       paf       208: ///    Auto-object used for temporarily substituting/removing class method
1.2       paf       209: class Temp_method {
                    210:        VStateless_class& fclass;
                    211:        const String& fname;
1.43      paf       212:        Method* saved_method;
1.2       paf       213: public:
1.87      moko      214:        Temp_method(VStateless_class& aclass, const String& aname, Method* amethod) :
1.2       paf       215:                fclass(aclass),
                    216:                fname(aname),
                    217:                saved_method(aclass.get_method(aname)) {
1.67      misha     218:                fclass.set_method(aname, amethod);
1.2       paf       219:        }
                    220:        ~Temp_method() { 
1.67      misha     221:                fclass.set_method(fname, saved_method);
1.2       paf       222:        }
                    223: };
                    224: 
1.85      moko      225: 
                    226: class VBaseClassWrapper: public Value {
                    227: 
                    228:        VStateless_class& fclass;
                    229:        Value& fself;
                    230: 
                    231: public: // Value
                    232:        
                    233:        override const char* type() const { return fclass.type(); }
                    234:        override VStateless_class* get_class() { return &fclass; }
                    235:        override VStateless_class* base() { return fclass.base(); }
                    236: 
                    237:        override Value* get_element(const String& aname) { return fclass.get_element(fself, aname); }
                    238:        override const VJunction* put_element(const String& aname, Value* avalue) { return fclass.put_element(fself, aname, avalue); }
                    239:        override Value& as_expr_result(){ return fclass.as_expr_result(); }
                    240: 
                    241: public: // usage
                    242: 
                    243:        VBaseClassWrapper(VStateless_class& aclass, Value& aself): fclass(aclass), fself(aself) {};
                    244: };
                    245: 
1.2       paf       246: #endif

E-mail: