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

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.78    ! misha      11: static const char * const IDENT_VMETHOD_FRAME_H="$Date: 2009-04-29 03:27:08 $";
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.40      paf        22: 
1.46      paf        23: // forwards
                     24: 
                     25: class Request;
                     26: 
                     27: /**
                     28:        @b method parameters passed in this array.
                     29:        contains handy typecast ad junction/not junction ensurers
                     30: 
                     31: */
1.78    ! misha      32: class MethodParams {
1.46      paf        33: public:
1.78    ! misha      34:        MethodParams() : felements(0), fused(0){}
        !            35: 
1.77      misha      36:        void store_params(Value **params, size_t count){ 
1.78    ! misha      37:                felements=params;
1.77      misha      38:                fused=count;
                     39:        }
                     40: 
1.78    ! misha      41:        inline size_t count() const { return fused; }
        !            42: 
        !            43:        inline Value *get(size_t index) const {
        !            44:                assert(index<count());
        !            45:                return felements[index];
        !            46:        }
        !            47: 
1.77      misha      48:        inline Value& operator[] (size_t index) { return *get(index); }
1.46      paf        49: 
                     50:        Value& last() { return *get(count()-1); }
                     51: 
                     52:        /// handy is-value-a-junction ensurer
                     53:        Value& as_junction(int index, const char* msg) { 
1.56      paf        54:                Value* value=get(index);
                     55:                return as_junction(value, msg, index); 
                     56:        }
                     57:        /// handy is-value-a-junction ensurer
                     58:        Value& as_junction(Value* value, const char* msg, int index) { 
                     59:                return get_as(value, true, msg, index); 
1.46      paf        60:        }
                     61:        /// handy value-is-not-a-junction ensurer
                     62:        Value& as_no_junction(int index, const char* msg) { 
1.56      paf        63:                Value* value=get(index);
                     64:                return as_no_junction(value, msg, index); 
                     65:        }
                     66:        /// handy value-is-not-a-junction ensurer
                     67:        Value& as_no_junction(Value* value, const char* msg, int index) { 
                     68:                return get_as(value, false, msg, index); 
1.46      paf        69:        }
1.62      misha      70:        /// handy is-value-a-junction ensurer or can be auto-processed
                     71:        Value& as_expression(int index, const char* msg) { 
                     72:                Value* value=get(index);
                     73:                if(value->is_evaluated_expr()){
                     74:                        return *value;
                     75:                } else {
                     76:                        return get_as(value, true, msg, index);
                     77:                }
                     78:        }
1.46      paf        79:        /// handy expression auto-processing to double
                     80:        double as_double(int index, const char* msg, Request& r) { 
1.56      paf        81:                Value* value=get(index);
1.60      paf        82:                if(!value->is_evaluated_expr())
1.57      paf        83:                        value=&get_processed(value, msg, index, r);
                     84:                return value->as_double(); 
1.46      paf        85:        }
                     86:        /// handy expression auto-processing to int
                     87:        int as_int(int index, const char* msg, Request& r) { 
1.56      paf        88:                Value* value=get(index);
1.60      paf        89:                if(!value->is_evaluated_expr())
1.57      paf        90:                        value=&get_processed(value, msg, index, r);
                     91:                return value->as_int(); 
1.46      paf        92:        }
                     93:        /// handy expression auto-processing to bool
                     94:        bool as_bool(int index, const char* msg, Request& r) { 
1.56      paf        95:                Value* value=get(index);
1.60      paf        96:                if(!value->is_evaluated_expr())
1.59      paf        97:                        value=&get_processed(value, msg, index, r);
                     98:                return value->as_bool(); 
1.46      paf        99:        }
                    100:        /// handy string ensurer
                    101:        const String& as_string(int index, const char* msg) { 
                    102:                return as_no_junction(index, msg).as_string();
                    103:        }
                    104: private:
                    105: 
1.78    ! misha     106:        Value **felements;
        !           107:        size_t fused;
        !           108: 
1.46      paf       109:        /// handy value-is/not-a-junction ensurer
1.56      paf       110:        Value& get_as(Value* value, bool as_junction, const char* msg, int index) { 
                    111:                if((value->get_junction()!=0) ^ as_junction)
1.61      misha     112:                        throw Exception(PARSER_RUNTIME,
1.46      paf       113:                                0,
                    114:                                "%s (parameter #%d)", msg, 1+index);
                    115: 
1.56      paf       116:                return *value;
1.46      paf       117:        }
                    118: 
1.56      paf       119:        Value& get_processed(Value* value, const char* msg, int index, Request& r);
1.46      paf       120: 
                    121: };
                    122: 
1.4       paf       123: /**    Method frame write context
                    124:        accepts values written by method code
                    125:        also handles method parameters and local variables
1.3       paf       126: */
1.46      paf       127: class VMethodFrame: public WContext {
1.63      misha     128: protected:
1.46      paf       129:        VMethodFrame *fcaller;
                    130: 
1.72      misha     131:        HashStringValue* my; /*OR*/ MethodParams fnumbered_params;
1.46      paf       132:        Value* fself;
                    133: 
1.64      misha     134:        typedef const VJunction* (VMethodFrame::*put_element_t)(const String& aname, Value* avalue);
                    135:        put_element_t put_element_impl;
1.46      paf       136: 
1.1       paf       137: public: // Value
                    138: 
1.46      paf       139:        override const char* type() const { return "method_frame"; }
1.20      paf       140: 
                    141:        /// VMethodFrame: $result | parent get_string(=accumulated fstring)
1.46      paf       142:        override const String* get_string() { 
1.20      paf       143:                // check the $result value
1.46      paf       144:                Value* result=get_result_variable();
1.20      paf       145:                // if we have one, return it's string value, else return as usual: accumulated fstring or fvalue
                    146:                return result ? result->get_string() : WContext::get_string();
                    147:        }
                    148:        
1.40      paf       149:        /// VMethodFrame: my or self_transparent or $caller
1.51      paf       150:        override Value* get_element(const String& aname, Value& /*aself*/, bool looking_up) { 
1.42      paf       151:                if(aname==CALLER_ELEMENT_NAME)
1.40      paf       152:                        return caller();
1.42      paf       153: 
1.67      misha     154:                if(aname==SELF_ELEMENT_NAME)
                    155:                        return &self();
                    156: 
1.66      misha     157:                Value* result;
                    158:                if(my && (result=my->get(aname)))
                    159:                        return result;
                    160: 
                    161:                if(result=self().get_element(aname, self(), looking_up))
                    162:                        return result;
1.40      paf       163: 
                    164:                return 0;
1.1       paf       165:        }
                    166: 
1.4       paf       167:        /// VMethodFrame: self_transparent
1.46      paf       168:        override VStateless_class* get_class() { return self().get_class(); }
1.44      paf       169: 
                    170:        /// VMethodFrame: self_transparent
1.46      paf       171:        override Value* base() { return self().base(); }
1.1       paf       172: 
1.64      misha     173:        /// VMethodFrame: my or self_transparent
                    174:        override const VJunction* put_element(Value& /*aself*/, const String& aname, Value* avalue, bool /*areplace*/) {
                    175:                return (this->*put_element_impl)(aname, avalue);
                    176:        }
                    177: 
1.69      misha     178:        /// VMethodFrame: appends a fstring to result
                    179:        override void write(const String& astring, String::Language alang) {
1.74      misha     180:                if(!junction.method->allways_use_result){
1.73      misha     181:                        if(!get_result_variable()){
                    182:                                WContext::write(astring, alang);
                    183:                        } else {
1.74      misha     184:                                ((Method *)junction.method)->allways_use_result=true;
1.73      misha     185:                        }
1.69      misha     186:                }
                    187:        }
                    188: 
1.64      misha     189: private:
                    190: 
                    191:        const VJunction* put_element_local(const String& aname, Value* avalue){
                    192:                set_my_variable(aname, *avalue);
                    193:                return PUT_ELEMENT_REPLACED_ELEMENT;
                    194:        }
                    195: 
                    196:        const VJunction* put_element_global(const String& aname, Value* avalue){
                    197:                if(my && my->put_replaced(aname, avalue))
                    198:                        return PUT_ELEMENT_REPLACED_ELEMENT;
                    199:                return self().put_element(self(), aname, avalue, false/*=always, areplace*/);
                    200:        }
                    201: 
1.30      paf       202: public: // WContext
1.1       paf       203: 
1.46      paf       204:        override StringOrValue result() {
1.1       paf       205:                // check the $result value
1.46      paf       206:                Value* result_value=get_result_variable();
1.1       paf       207:                // if we have one, return it, else return as usual: accumulated fstring or fvalue
1.52      paf       208:                return result_value ? StringOrValue(*result_value) : WContext::result();
1.1       paf       209:        }
                    210: 
1.46      paf       211:        void write(Value& avalue, String::Language alang) {
                    212:                WContext::write(avalue, alang);
                    213:        }
                    214: 
1.1       paf       215: public: // usage
                    216: 
1.46      paf       217:        VMethodFrame(
1.40      paf       218:                const Junction& ajunction/*info: always method-junction*/,
1.46      paf       219:                VMethodFrame *acaller);
1.1       paf       220: 
1.40      paf       221:        VMethodFrame *caller() { return fcaller; }
1.25      paf       222: 
1.76      misha     223: #ifdef USE_DESTRUCTORS
1.75      misha     224:        ~VMethodFrame(){
                    225:                if(my){
                    226:                        delete my;
                    227:                }
                    228:        }
1.76      misha     229: #endif
1.75      misha     230: 
1.38      paf       231:        void set_self(Value& aself) { fself=&aself; }
1.41      paf       232:        /// we sure that someone already set our self with VMethodFrame::set_self(Value&)
                    233:        Value& self() { return *fself; }
1.1       paf       234: 
1.77      misha     235:        size_t method_params_count() {
1.27      paf       236:                const Method& method=*junction.method;
1.77      misha     237:                return method.params_names ? method.params_names->count():0;
1.27      paf       238:        }
1.77      misha     239: 
                    240:        void store_params(Value **params, size_t count) {
1.1       paf       241:                const Method& method=*junction.method;
1.46      paf       242:                size_t max_params=
1.1       paf       243:                        method.max_numbered_params_count?method.max_numbered_params_count:
1.46      paf       244:                        method.params_names?method.params_names->count():
1.1       paf       245:                        0;
1.77      misha     246: 
                    247:                if(count>max_params)
1.61      misha     248:                        throw Exception(PARSER_RUNTIME,
1.46      paf       249:                                0, //&name(),
1.7       paf       250:                                "method of %s (%s) accepts maximum %d parameter(s)", 
1.38      paf       251:                                        junction.self.get_class()->name_cstr(),
                    252:                                        junction.self.type(),
1.1       paf       253:                                        max_params);
1.78    ! misha     254: 
        !           255:                if(method.params_names) {
1.77      misha     256:                        size_t i=0;
                    257: 
                    258:                        for (; i<count; i++){
                    259:                                const String& fname=*(*method.params_names)[i];
                    260:                                set_my_variable(fname, *params[i]);
                    261:                        }
                    262: 
1.46      paf       263:                        size_t param_count=method.params_names->count();
1.77      misha     264:                        for(; i<param_count; i++) {
                    265:                                const String& fname=*(*method.params_names)[i];
1.68      misha     266:                                my->put(fname, VVoid::get());
1.1       paf       267:                        }
1.77      misha     268:                } else {
                    269:                        fnumbered_params.store_params(params,count);
1.46      paf       270:                }
1.1       paf       271:        }
                    272: 
1.78    ! misha     273:        void empty_params(){
        !           274:                const Method& method=*junction.method;
        !           275:                if(method.params_names){
        !           276:                        size_t param_count=method.params_names->count();
        !           277:                        for(size_t i=0; i<param_count; i++) {
        !           278:                                const String& fname=*(*method.params_names)[i];
        !           279:                                my->put(fname, VVoid::get());
        !           280:                        }
        !           281:                }
        !           282:        }
        !           283: 
1.72      misha     284:        MethodParams* numbered_params() { return &fnumbered_params; }
1.18      paf       285: 
1.63      misha     286: protected:
1.18      paf       287: 
1.46      paf       288:        void set_my_variable(const String& fname, Value& value) {
                    289:                my->put(fname, &value); // remember param
1.25      paf       290:        }
                    291: 
1.46      paf       292:        Value* get_result_variable();
1.1       paf       293: 
                    294: public:
                    295:        
                    296:        const Junction& junction;
                    297: 
1.41      paf       298: };
                    299: 
                    300: ///    Auto-object used for temporary changing VMethod_frame::fself.
                    301: class Temp_method_frame_self {
                    302:        VMethodFrame& fmethod_frame;
1.46      paf       303:        Value* saved_self;
1.41      paf       304: public:
                    305:        Temp_method_frame_self(VMethodFrame& amethod_frame, Value& aself) :
                    306:                fmethod_frame(amethod_frame),
1.46      paf       307:                saved_self(&amethod_frame.self()) {
1.41      paf       308:                fmethod_frame.set_self(aself);
                    309:        }
                    310:        ~Temp_method_frame_self() {
1.46      paf       311:                fmethod_frame.set_self(*saved_self);
1.41      paf       312:        }
1.1       paf       313: };
                    314: 
                    315: #endif

E-mail: