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

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

E-mail: