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

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

E-mail: