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

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

E-mail: