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

1.3       paf         1: /** @file
1.5       paf         2:        Parser: @b method_frame write context
1.3       paf         3: 
1.51.6.1  paf         4:        Copyright (c) 2001-2005 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.51.6.2! paf        11: static const char * const IDENT_VMETHOD_FRAME_H="$Date: 2005/08/05 13:03:05 $";
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: */
                     32: class MethodParams: public Array<Value*> {
                     33: public:
                     34:        Value& operator[] (size_t index) { return *get(index); }
                     35: 
                     36:        Value& last() { return *get(count()-1); }
                     37: 
                     38:        /// handy is-value-a-junction ensurer
                     39:        Value& as_junction(int index, const char* msg) { 
1.51.6.2! paf        40:                Value* value=get(index);
        !            41:                return as_junction(value, msg, index); 
        !            42:        }
        !            43:        /// handy is-value-a-junction ensurer
        !            44:        Value& as_junction(Value* value, const char* msg, int index) { 
        !            45:                return get_as(value, true, msg, index); 
1.46      paf        46:        }
                     47:        /// handy value-is-not-a-junction ensurer
                     48:        Value& as_no_junction(int index, const char* msg) { 
1.51.6.2! paf        49:                Value* value=get(index);
        !            50:                return as_no_junction(value, msg, index); 
        !            51:        }
        !            52:        /// handy value-is-not-a-junction ensurer
        !            53:        Value& as_no_junction(Value* value, const char* msg, int index) { 
        !            54:                return get_as(value, false, msg, index); 
1.46      paf        55:        }
                     56:        /// handy expression auto-processing to double
                     57:        double as_double(int index, const char* msg, Request& r) { 
1.51.6.2! paf        58:                Value* value=get(index);
        !            59:                if(VDouble* vdouble=static_cast<VDouble*>(value->as("double", false)))
        !            60:                        return vdouble->get_double();
        !            61:                return get_processed(value, msg, index, r).as_double(); 
1.46      paf        62:        }
                     63:        /// handy expression auto-processing to int
                     64:        int as_int(int index, const char* msg, Request& r) { 
1.51.6.2! paf        65:                Value* value=get(index);
        !            66:                if(VDouble* vdouble=static_cast<VDouble*>(value->as("double", false)))
        !            67:                        return vdouble->get_int();
        !            68:                if(VInt* vint=static_cast<VInt*>(value->as("int", false)))
        !            69:                        return vint->get_int();
        !            70:                return get_processed(value, msg, index, r).as_int(); 
1.46      paf        71:        }
                     72:        /// handy expression auto-processing to bool
                     73:        bool as_bool(int index, const char* msg, Request& r) { 
1.51.6.2! paf        74:                Value* value=get(index);
        !            75:                return get_processed(value, msg, index, r).as_bool(); 
1.46      paf        76:        }
                     77:        /// handy string ensurer
                     78:        const String& as_string(int index, const char* msg) { 
                     79:                return as_no_junction(index, msg).as_string();
                     80:        }
                     81: 
                     82: private:
                     83: 
                     84:        /// handy value-is/not-a-junction ensurer
1.51.6.2! paf        85:        Value& get_as(Value* value, bool as_junction, const char* msg, int index) { 
        !            86:                if((value->get_junction()!=0) ^ as_junction)
1.46      paf        87:                        throw Exception("parser.runtime",
                     88:                                0,
                     89:                                "%s (parameter #%d)", msg, 1+index);
                     90: 
1.51.6.2! paf        91:                return *value;
1.46      paf        92:        }
                     93: 
1.51.6.2! paf        94:        Value& get_processed(Value* value, const char* msg, int index, Request& r);
1.46      paf        95: 
                     96: };
                     97: 
1.4       paf        98: /**    Method frame write context
                     99:        accepts values written by method code
                    100:        also handles method parameters and local variables
1.3       paf       101: */
1.46      paf       102: class VMethodFrame: public WContext {
                    103:        VMethodFrame *fcaller;
                    104: 
                    105:        size_t store_param_index;
                    106:        HashStringValue* my;/*OR*/MethodParams* fnumbered_params;
                    107:        Value* fself;
                    108: 
                    109:        Value* fresult_initial_void;
                    110: 
1.1       paf       111: public: // Value
                    112: 
1.46      paf       113:        override const char* type() const { return "method_frame"; }
1.20      paf       114: 
                    115:        /// VMethodFrame: $result | parent get_string(=accumulated fstring)
1.46      paf       116:        override const String* get_string() { 
1.20      paf       117:                // check the $result value
1.46      paf       118:                Value* result=get_result_variable();
1.20      paf       119:                // if we have one, return it's string value, else return as usual: accumulated fstring or fvalue
                    120:                return result ? result->get_string() : WContext::get_string();
                    121:        }
                    122:        
1.40      paf       123:        /// VMethodFrame: my or self_transparent or $caller
1.51      paf       124:        override Value* get_element(const String& aname, Value& /*aself*/, bool looking_up) { 
1.46      paf       125:                if(my) {
                    126:                        if(Value* result=my->get(aname))
1.1       paf       127:                                return result;
                    128:                }
1.51      paf       129:                if(Value* result=self().get_element(aname, self(), looking_up))
1.40      paf       130:                        return result;
                    131: 
1.42      paf       132:                if(aname==CALLER_ELEMENT_NAME)
1.40      paf       133:                        return caller();
1.42      paf       134: 
                    135:                if(aname==SELF_ELEMENT_NAME)
                    136:                        return &self();
1.40      paf       137: 
                    138:                return 0;
1.1       paf       139:        }
1.4       paf       140:        /// VMethodFrame: my or self_transparent
1.46      paf       141:        override bool put_element(const String& aname, Value* avalue, bool replace) { 
                    142:                if(my && my->put_replace(aname, avalue))
1.32      paf       143:                        return true;
                    144: 
1.41      paf       145:                return self().put_element(aname, avalue, replace);
1.1       paf       146:        }
                    147: 
1.4       paf       148:        /// VMethodFrame: self_transparent
1.46      paf       149:        override VStateless_class* get_class() { return self().get_class(); }
1.44      paf       150: 
                    151:        /// VMethodFrame: self_transparent
1.46      paf       152:        override Value* base() { return self().base(); }
1.1       paf       153: 
1.30      paf       154: public: // WContext
1.1       paf       155: 
1.46      paf       156:        override StringOrValue result() {
1.1       paf       157:                // check the $result value
1.46      paf       158:                Value* result_value=get_result_variable();
1.1       paf       159:                // if we have one, return it, else return as usual: accumulated fstring or fvalue
1.24      paf       160:                return result_value ? StringOrValue(0, result_value) : WContext::result();
1.1       paf       161:        }
                    162: 
1.46      paf       163:        void write(Value& avalue, String::Language alang) {
                    164:                WContext::write(avalue, alang);
                    165:        }
                    166: 
1.1       paf       167: public: // usage
                    168: 
1.46      paf       169:        VMethodFrame(
                    170:                //const String& aname,
1.40      paf       171:                const Junction& ajunction/*info: always method-junction*/,
1.46      paf       172:                VMethodFrame *acaller);
1.1       paf       173: 
1.46      paf       174: //     const String& name() { return fname; }
1.40      paf       175:        VMethodFrame *caller() { return fcaller; }
1.25      paf       176: 
1.38      paf       177:        void set_self(Value& aself) { fself=&aself; }
1.41      paf       178:        /// we sure that someone already set our self with VMethodFrame::set_self(Value&)
                    179:        Value& self() { return *fself; }
1.1       paf       180: 
1.27      paf       181:        bool can_store_param() {
                    182:                const Method& method=*junction.method;
1.46      paf       183:                return method.params_names && store_param_index<method.params_names->count();
1.27      paf       184:        }
1.46      paf       185:        void store_param(Value& value) {
1.1       paf       186:                const Method& method=*junction.method;
1.46      paf       187:                size_t max_params=
1.1       paf       188:                        method.max_numbered_params_count?method.max_numbered_params_count:
1.46      paf       189:                        method.params_names?method.params_names->count():
1.1       paf       190:                        0;
                    191:                if(store_param_index==max_params)
1.23      paf       192:                        throw Exception("parser.runtime",
1.46      paf       193:                                0, //&name(),
1.7       paf       194:                                "method of %s (%s) accepts maximum %d parameter(s)", 
1.38      paf       195:                                        junction.self.get_class()->name_cstr(),
                    196:                                        junction.self.type(),
1.1       paf       197:                                        max_params);
                    198:                
1.46      paf       199:                if(fnumbered_params) { // are this method params numbered?
                    200:                        *fnumbered_params+=&value;
1.1       paf       201:                } else { // named param
1.25      paf       202:                        // speedup: not checking for clash with "result" fname
1.46      paf       203:                        const String& fname=*(*method.params_names)[store_param_index];
1.25      paf       204:                        set_my_variable(fname, value);
1.1       paf       205:                }
                    206:                store_param_index++;
                    207:        }
                    208:        void fill_unspecified_params() {
                    209:                const Method &method=*junction.method;
1.46      paf       210:                if(method.params_names) { // there are any named parameters might need filling?
                    211:                        size_t param_count=method.params_names->count();
                    212:                        for(; store_param_index<param_count; store_param_index++) {
                    213:                                const String& fname=*(*method.params_names)[store_param_index];
                    214:                                my->put(fname, new VVoid);
1.1       paf       215:                        }
1.46      paf       216:                }
1.1       paf       217:        }
                    218: 
1.46      paf       219:        MethodParams* numbered_params() { return fnumbered_params; }
1.18      paf       220: 
                    221: private:
                    222: 
1.46      paf       223:        void set_my_variable(const String& fname, Value& value) {
                    224:                my->put(fname, &value); // remember param
1.25      paf       225:        }
                    226: 
1.46      paf       227:        Value* get_result_variable();
1.1       paf       228: 
                    229: public:
                    230:        
                    231:        const Junction& junction;
                    232: 
1.41      paf       233: };
                    234: 
                    235: ///    Auto-object used for temporary changing VMethod_frame::fself.
                    236: class Temp_method_frame_self {
                    237:        VMethodFrame& fmethod_frame;
1.46      paf       238:        Value* saved_self;
1.41      paf       239: public:
                    240:        Temp_method_frame_self(VMethodFrame& amethod_frame, Value& aself) :
                    241:                fmethod_frame(amethod_frame),
1.46      paf       242:                saved_self(&amethod_frame.self()) {
1.41      paf       243:                fmethod_frame.set_self(aself);
                    244:        }
                    245:        ~Temp_method_frame_self() {
1.46      paf       246:                fmethod_frame.set_self(*saved_self);
1.41      paf       247:        }
1.1       paf       248: };
                    249: 
                    250: #endif

E-mail: