Annotation of parser3/src/types/pa_vmethod_frame.h, revision 1.107

1.3       paf         1: /** @file
1.5       paf         2:        Parser: @b method_frame write context
1.3       paf         3: 
1.98      moko        4:        Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
1.22      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: */
                      7: 
                      8: #ifndef PA_VMETHOD_FRAME_H
                      9: #define PA_VMETHOD_FRAME_H
1.28      paf        10: 
1.107   ! moko       11: #define IDENT_PA_VMETHOD_FRAME_H "$Id: pa_vmethod_frame.h,v 1.106 2016/10/04 21:20:41 moko Exp $"
1.1       paf        12: 
1.101     moko       13: #include "pa_symbols.h"
1.1       paf        14: #include "pa_wcontext.h"
1.12      parser     15: #include "pa_vvoid.h"
1.1       paf        16: #include "pa_vjunction.h"
                     17: 
1.46      paf        18: // forwards
                     19: 
                     20: class Request;
                     21: 
                     22: /**
                     23:        @b method parameters passed in this array.
                     24:        contains handy typecast ad junction/not junction ensurers
                     25: 
                     26: */
1.78      misha      27: class MethodParams {
1.46      paf        28: public:
1.78      misha      29:        MethodParams() : felements(0), fused(0){}
                     30: 
1.82      misha      31: #ifdef USE_DESTRUCTORS
                     32:        ~MethodParams(){
                     33:                Value **flast=felements+count();
1.95      moko       34:                for(Value **current=felements;current<flast;current++){
                     35:                        Junction* junction=(*current)->get_junction();
                     36:                        if (junction && junction->code)
1.82      misha      37:                                delete *current;
1.95      moko       38:                }
1.82      misha      39:        }
                     40: #endif
                     41: 
1.77      misha      42:        void store_params(Value **params, size_t count){ 
1.78      misha      43:                felements=params;
1.77      misha      44:                fused=count;
                     45:        }
                     46: 
1.78      misha      47:        inline size_t count() const { return fused; }
                     48: 
                     49:        inline Value *get(size_t index) const {
                     50:                assert(index<count());
                     51:                return felements[index];
                     52:        }
                     53: 
1.77      misha      54:        inline Value& operator[] (size_t index) { return *get(index); }
1.46      paf        55: 
                     56:        Value& last() { return *get(count()-1); }
                     57: 
                     58:        /// handy is-value-a-junction ensurer
                     59:        Value& as_junction(int index, const char* msg) { 
1.107   ! moko       60:                Value& value=*get(index);
        !            61:                if(!value.get_junction())
        !            62:                        throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d)", msg, 1+index);
        !            63:                return value;
1.46      paf        64:        }
1.107   ! moko       65: 
1.46      paf        66:        /// handy value-is-not-a-junction ensurer
                     67:        Value& as_no_junction(int index, const char* msg) { 
1.107   ! moko       68:                Value& value=*get(index);
        !            69:                if(value.get_junction())
        !            70:                        throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d)", msg, 1+index);
        !            71:                return value;
1.46      paf        72:        }
1.107   ! moko       73: 
1.62      misha      74:        /// handy is-value-a-junction ensurer or can be auto-processed
                     75:        Value& as_expression(int index, const char* msg) { 
1.107   ! moko       76:                Value& value=*get(index);
        !            77:                if(value.is_evaluated_expr()){
        !            78:                        return value;
1.62      misha      79:                } else {
1.107   ! moko       80:                        if(!value.get_junction())
        !            81:                                throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d)", msg, 1+index);
        !            82:                        return value;
1.62      misha      83:                }
                     84:        }
1.107   ! moko       85: 
1.46      paf        86:        /// handy expression auto-processing to double
1.107   ! moko       87:        double as_double(int index, const char* msg, Request& r) {
        !            88:                Value& value=*get(index);
        !            89:                if(value.is_evaluated_expr())
        !            90:                        return value.as_double();
        !            91:                return get_processed(value, msg, index, r).as_double();
1.46      paf        92:        }
1.107   ! moko       93: 
1.46      paf        94:        /// handy expression auto-processing to int
1.107   ! moko       95:        int as_int(int index, const char* msg, Request& r) {
        !            96:                Value& value=*get(index);
        !            97:                if(value.is_evaluated_expr())
        !            98:                        return value.as_int();
        !            99:                return get_processed(value, msg, index, r).as_int();
1.46      paf       100:        }
1.107   ! moko      101: 
1.46      paf       102:        /// handy expression auto-processing to bool
1.107   ! moko      103:        bool as_bool(int index, const char* msg, Request& r) {
        !           104:                Value& value=*get(index);
        !           105:                if(value.is_evaluated_expr())
        !           106:                        return value.as_bool();
        !           107:                return get_processed(value, msg, index, r).as_bool();
1.46      paf       108:        }
1.107   ! moko      109: 
1.46      paf       110:        /// handy string ensurer
                    111:        const String& as_string(int index, const char* msg) { 
                    112:                return as_no_junction(index, msg).as_string();
                    113:        }
1.107   ! moko      114: 
1.93      misha     115:        /// handy hash ensurers
                    116:        HashStringValue* as_hash(int index, const char* name=0);
1.107   ! moko      117: 
1.93      misha     118:        /// handy table ensurer
                    119:        Table* as_table(int index, const char* name=0);
                    120: 
1.46      paf       121: private:
                    122: 
1.78      misha     123:        Value **felements;
                    124:        size_t fused;
                    125: 
1.107   ! moko      126:        Value& get_processed(Value& value, const char* msg, int index, Request& r);
1.46      paf       127: 
                    128: };
                    129: 
1.4       paf       130: /**    Method frame write context
                    131:        accepts values written by method code
                    132:        also handles method parameters and local variables
1.3       paf       133: */
1.46      paf       134: class VMethodFrame: public WContext {
1.63      misha     135: protected:
1.46      paf       136:        VMethodFrame *fcaller;
                    137: 
1.83      misha     138:        HashString<Value*>* my; /*OR*/ MethodParams fnumbered_params;
1.87      moko      139:        Value& fself;
1.46      paf       140: 
1.64      misha     141:        typedef const VJunction* (VMethodFrame::*put_element_t)(const String& aname, Value* avalue);
                    142:        put_element_t put_element_impl;
1.46      paf       143: 
1.1       paf       144: public: // Value
                    145: 
1.46      paf       146:        override const char* type() const { return "method_frame"; }
1.20      paf       147: 
                    148:        /// VMethodFrame: $result | parent get_string(=accumulated fstring)
1.46      paf       149:        override const String* get_string() { 
1.20      paf       150:                // check the $result value
1.46      paf       151:                Value* result=get_result_variable();
1.20      paf       152:                // if we have one, return it's string value, else return as usual: accumulated fstring or fvalue
                    153:                return result ? result->get_string() : WContext::get_string();
                    154:        }
                    155:        
1.40      paf       156:        /// VMethodFrame: my or self_transparent or $caller
1.84      misha     157:        override Value* get_element(const String& aname) { 
1.103     moko      158:                if(SYMBOLS_EQ(aname,CALLER_SYMBOL))
1.40      paf       159:                        return caller();
1.42      paf       160: 
1.103     moko      161:                if(SYMBOLS_EQ(aname,SELF_SYMBOL))
1.67      misha     162:                        return &self();
                    163: 
1.66      misha     164:                Value* result;
                    165:                if(my && (result=my->get(aname)))
                    166:                        return result;
                    167: 
1.84      misha     168:                if(result=self().get_element(aname))
1.66      misha     169:                        return result;
1.40      paf       170: 
                    171:                return 0;
1.1       paf       172:        }
                    173: 
1.4       paf       174:        /// VMethodFrame: self_transparent
1.46      paf       175:        override VStateless_class* get_class() { return self().get_class(); }
1.44      paf       176: 
                    177:        /// VMethodFrame: self_transparent
1.85      misha     178:        override VStateless_class* base() { return self().base(); }
1.1       paf       179: 
1.64      misha     180:        /// VMethodFrame: my or self_transparent
1.94      moko      181:        override const VJunction* put_element(const String& aname, Value* avalue) {
1.64      misha     182:                return (this->*put_element_impl)(aname, avalue);
                    183:        }
                    184: 
1.69      misha     185:        /// VMethodFrame: appends a fstring to result
                    186:        override void write(const String& astring, String::Language alang) {
1.79      misha     187: #ifdef OPTIMIZE_RESULT
1.87      moko      188:                switch (method.result_optimization){
1.97      moko      189:                        case Method::RO_USE_RESULT:
1.79      misha     190:                                return;
                    191:                        case Method::RO_UNKNOWN:
                    192:                                if(get_result_variable()){
1.87      moko      193:                                        ((Method *)&method)->result_optimization=Method::RO_USE_RESULT;
1.79      misha     194:                                        return;
                    195:                                }
1.97      moko      196:                        case Method::RO_USE_WCONTEXT: break;
1.69      misha     197:                }
1.79      misha     198: #endif
                    199:                WContext::write(astring, alang);
1.69      misha     200:        }
                    201: 
1.64      misha     202: private:
                    203: 
                    204:        const VJunction* put_element_local(const String& aname, Value* avalue){
                    205:                set_my_variable(aname, *avalue);
                    206:                return PUT_ELEMENT_REPLACED_ELEMENT;
                    207:        }
                    208: 
                    209:        const VJunction* put_element_global(const String& aname, Value* avalue){
                    210:                if(my && my->put_replaced(aname, avalue))
                    211:                        return PUT_ELEMENT_REPLACED_ELEMENT;
1.94      moko      212:                return self().put_element(aname, avalue);
1.64      misha     213:        }
                    214: 
1.30      paf       215: public: // WContext
1.1       paf       216: 
1.105     moko      217:        override Value& result() {
1.80      misha     218:                if(my){
                    219:                        // check the $result value
                    220:                        Value* result_value=get_result_variable();
                    221:                        // if we have one, return it, else return as usual: accumulated fstring or fvalue
                    222:                        if(result_value)
1.105     moko      223:                                return *result_value;
1.79      misha     224: #ifdef OPTIMIZE_RESULT
1.87      moko      225:                        if(method.result_optimization==Method::RO_USE_RESULT)
1.105     moko      226:                                return *VVoid::get();
1.87      moko      227:                        ((Method *)&method)->result_optimization=Method::RO_USE_WCONTEXT;
1.79      misha     228: #ifdef OPTIMIZE_CALL // nested as CO_WITHOUT_WCONTEXT assumes that $result not used
1.87      moko      229:                        ((Method *)&method)->call_optimization=Method::CO_WITHOUT_WCONTEXT;
1.79      misha     230: #endif
                    231: #endif
1.80      misha     232:                }
1.79      misha     233:                return WContext::result();
1.1       paf       234:        }
                    235: 
1.85      misha     236:        override void write(Value& avalue) {
                    237:                WContext::write(avalue);
1.46      paf       238:        }
                    239: 
1.1       paf       240: public: // usage
                    241: 
1.87      moko      242:        VMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself);
1.1       paf       243: 
1.40      paf       244:        VMethodFrame *caller() { return fcaller; }
1.25      paf       245: 
1.76      misha     246: #ifdef USE_DESTRUCTORS
1.75      misha     247:        ~VMethodFrame(){
                    248:                if(my){
                    249:                        delete my;
                    250:                }
                    251:        }
1.76      misha     252: #endif
1.75      misha     253: 
1.87      moko      254:        Value& self() { return fself; }
1.1       paf       255: 
1.77      misha     256:        size_t method_params_count() {
                    257:                return method.params_names ? method.params_names->count():0;
1.27      paf       258:        }
1.77      misha     259: 
                    260:        void store_params(Value **params, size_t count) {
1.92      moko      261:                if(my) {
                    262:                        size_t param_count=method.params_names ? method.params_names->count():0;
1.89      moko      263:                        size_t i=0;
                    264: 
                    265:                        if(count>param_count){
                    266:                                if(method.extra_params){
                    267:                                        for(; i<param_count; i++) {
                    268:                                                const String& fname=*(*method.params_names)[i];
                    269:                                                set_my_variable(fname, *params[i]);
                    270:                                        }
                    271: 
                    272:                                        VHash& vargs=*new VHash();
                    273:                                        HashStringValue& args = vargs.hash();
                    274: 
                    275:                                        for(; i<count; i++) {
                    276:                                                args.put(format(args.count(), 0), params[i]);
                    277:                                        }
1.86      misha     278: 
1.89      moko      279:                                        set_my_variable(*method.extra_params, vargs);
                    280:                                        return;
                    281:                                } else
1.86      misha     282:                                throw Exception(PARSER_RUNTIME,
                    283:                                        0, //&name(),
1.99      moko      284:                                        "method of %s accepts maximum %d parameter(s) (%d present)", 
1.87      moko      285:                                        self().type(),
1.86      misha     286:                                        param_count,
                    287:                                        count);
1.89      moko      288:                        }
1.78      misha     289: 
1.86      misha     290:                        for(; i<count; i++) {
1.77      misha     291:                                const String& fname=*(*method.params_names)[i];
                    292:                                set_my_variable(fname, *params[i]);
                    293:                        }
                    294: 
                    295:                        for(; i<param_count; i++) {
                    296:                                const String& fname=*(*method.params_names)[i];
1.68      misha     297:                                my->put(fname, VVoid::get());
1.1       paf       298:                        }
1.86      misha     299:                } else
1.77      misha     300:                        fnumbered_params.store_params(params,count);
1.1       paf       301:        }
                    302: 
1.78      misha     303:        void empty_params(){
                    304:                if(method.params_names){
                    305:                        size_t param_count=method.params_names->count();
1.91      moko      306:                        if(param_count>0){
1.104     moko      307:                                my->put(*(*method.params_names)[0], VString::empty());
                    308:                                for(size_t i=1; i<param_count; i++)
                    309:                                        my->put(*(*method.params_names)[i], VVoid::get());
1.78      misha     310:                        }
                    311:                }
                    312:        }
                    313: 
1.72      misha     314:        MethodParams* numbered_params() { return &fnumbered_params; }
1.18      paf       315: 
1.63      misha     316: protected:
1.18      paf       317: 
1.46      paf       318:        void set_my_variable(const String& fname, Value& value) {
                    319:                my->put(fname, &value); // remember param
1.25      paf       320:        }
                    321: 
1.46      paf       322:        Value* get_result_variable();
1.1       paf       323: 
                    324: public:
                    325:        
1.87      moko      326:        const Method& method;
1.1       paf       327: 
1.41      paf       328: };
                    329: 
1.85      misha     330: class VConstructorFrame: public VMethodFrame {
                    331: public:
1.87      moko      332:        VConstructorFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : VMethodFrame(amethod, acaller, aself) {
                    333:                // prevent non-string writes for better error reporting [constructors are not expected to return anything]
                    334:                VMethodFrame::write(aself);
                    335:        }
1.85      misha     336: 
                    337:        override void write(const String& /*astring*/, String::Language /*alang*/) {}
                    338: };
                    339: 
1.1       paf       340: #endif

E-mail: