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

1.3       paf         1: /** @file
1.5       paf         2:        Parser: @b method_frame write context
1.3       paf         3: 
1.21      paf         4:        Copyright (c) 2001, 2002 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.35    ! paf        11: static const char* IDENT_VMETHOD_FRAME_H="$Date: 2002/08/14 14:18: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"
1.35    ! paf        16: #include "pa_request.h"
1.1       paf        17: 
1.4       paf        18: /**    Method frame write context
                     19:        accepts values written by method code
                     20:        also handles method parameters and local variables
1.3       paf        21: */
1.1       paf        22: class VMethodFrame : public WContext {
                     23: public: // Value
                     24: 
                     25:        const char *type() const { return "method_frame"; }
1.20      paf        26: 
                     27:        /// VMethodFrame: $result | parent get_string(=accumulated fstring)
                     28:        const String *get_string() { 
                     29:                // check the $result value
                     30:                Value *result=get_result_variable();
                     31:                // if we have one, return it's string value, else return as usual: accumulated fstring or fvalue
                     32:                return result ? result->get_string() : WContext::get_string();
                     33:        }
                     34:        
1.4       paf        35:        /// VMethodFrame: my or self_transparent
1.34      paf        36:        Value *get_element(const String& aname, Value *aself, bool looking_up) { 
1.25      paf        37:                if(junction.method->max_numbered_params_count==0) {
1.32      paf        38:                        if(Value *result=static_cast<Value *>(my.get(aname)))
1.1       paf        39:                                return result;
                     40:                }
1.34      paf        41:                return fself->get_element(aname, aself, looking_up); 
1.1       paf        42:        }
1.4       paf        43:        /// VMethodFrame: my or self_transparent
1.32      paf        44:        /*override*/ bool put_element(const String& aname, Value *avalue, bool replace) { 
                     45:                if(junction.method->max_numbered_params_count==0 && my.put_replace(aname, avalue))
                     46:                        return true;
                     47: 
                     48:                return fself->put_element(aname, avalue, replace);
1.1       paf        49:        }
                     50: 
1.4       paf        51:        /// VMethodFrame: self_transparent
1.1       paf        52:        VStateless_class* get_class() { return fself->get_class(); }
                     53: 
1.30      paf        54: public: // WContext
1.1       paf        55: 
1.35    ! paf        56:        /* override */ StringOrValue result() {
1.1       paf        57:                // check the $result value
1.24      paf        58:                Value *result_value=get_result_variable();
1.1       paf        59:                // if we have one, return it, else return as usual: accumulated fstring or fvalue
1.24      paf        60:                return result_value ? StringOrValue(0, result_value) : WContext::result();
1.1       paf        61:        }
                     62: 
                     63: public: // usage
                     64: 
                     65:        VMethodFrame(Pool& apool, 
1.25      paf        66:                const String& aname,
1.13      parser     67:                const Junction& ajunction/*info: always method-junction*/) : 
1.14      parser     68:                WContext(apool, 0 /* empty */),
1.1       paf        69: 
1.25      paf        70:                fname(aname),
1.1       paf        71:                junction(ajunction),
                     72:                store_param_index(0),
1.25      paf        73: 
                     74:                my(apool),
                     75:                fnumbered_params(apool, aname),
                     76: 
1.15      parser     77:                fself(0),
1.35    ! paf        78:                fresult_initial_void(0),
        !            79: 
        !            80:                junctions(apool) {
1.1       paf        81: 
1.25      paf        82:                if(has_my()) { // this method uses named params?
                     83:                        const Method &method=*junction.method;
1.1       paf        84:                        if(method.locals_names) { // are there any local var names?
                     85:                                // remember them
1.25      paf        86:                                // those are flags that fname is local == to be looked up in 'my'
1.1       paf        87:                                for(int i=0; i<method.locals_names->size(); i++) {
1.25      paf        88:                                        // speedup: not checking for clash with "result" fname
1.12      parser     89:                                        Value *value=NEW VVoid(pool());
1.25      paf        90:                                        const String& fname=*method.locals_names->get_string(i);
                     91:                                        set_my_variable(fname, value);
1.1       paf        92:                                }
                     93:                        }
                     94:                        { // always there is one local: $result
1.15      parser     95:                                fresult_initial_void=NEW VVoid(pool());
1.18      paf        96:                                set_my_variable(*result_var_name, fresult_initial_void);
1.1       paf        97:                        }
                     98:                }
                     99:        }
                    100: 
1.35    ! paf       101:        ~VMethodFrame() {
        !           102:                invalidate_junctions();
        !           103:        }
1.25      paf       104:        const String& name() { return fname; }
                    105: 
1.1       paf       106:        void set_self(Value& aself) { fself=&aself; }
                    107:        Value *self() { return fself; }
                    108: 
1.27      paf       109:        bool can_store_param() {
                    110:                const Method& method=*junction.method;
                    111:                return method.params_names && store_param_index<method.params_names->size();
                    112:        }
1.26      paf       113:        void store_param(Value *value) {
1.1       paf       114:                const Method& method=*junction.method;
                    115:                int max_params=
                    116:                        method.max_numbered_params_count?method.max_numbered_params_count:
                    117:                        method.params_names?method.params_names->size():
                    118:                        0;
                    119:                if(store_param_index==max_params)
1.23      paf       120:                        throw Exception("parser.runtime",
1.26      paf       121:                                &name(),
1.7       paf       122:                                "method of %s (%s) accepts maximum %d parameter(s)", 
1.25      paf       123:                                        junction.self.get_class()->name_cstr(),
1.1       paf       124:                                        junction.self.type(),
                    125:                                        max_params);
                    126:                
                    127:                if(method.max_numbered_params_count) { // are this method params numbered?
1.25      paf       128:                        fnumbered_params+=value;
1.1       paf       129:                } else { // named param
1.25      paf       130:                        // speedup: not checking for clash with "result" fname
                    131:                        const String& fname=*method.params_names->get_string(store_param_index);
                    132:                        set_my_variable(fname, value);
1.1       paf       133:                }
                    134:                store_param_index++;
                    135:        }
                    136:        void fill_unspecified_params() {
                    137:                const Method &method=*junction.method;
                    138:                if(method.params_names) // there are any named parameters might need filling?
                    139:                        for(; store_param_index<method.params_names->size(); store_param_index++) {
1.25      paf       140:                                const String& fname=*method.params_names->get_string(store_param_index);
                    141:                                my.put(fname, NEW VVoid(pool()));
1.1       paf       142:                        }
                    143:        }
                    144: 
1.25      paf       145:        MethodParams *numbered_params() { return &fnumbered_params; }
1.18      paf       146: 
1.35    ! paf       147:        void register_junction(Junction& ajunction) {
        !           148:                junctions+=&ajunction;
        !           149:        }
        !           150: 
1.18      paf       151: private:
                    152: 
1.25      paf       153:        bool has_my() {
                    154:                return junction.method->max_numbered_params_count==0;
                    155:        }
                    156: 
                    157:        void set_my_variable(const String& fname, Value *value) {
                    158:                my.put(fname, value); // remember param
1.20      paf       159:        }
                    160: 
                    161:        Value *get_result_variable() {
1.25      paf       162:                Value *result=has_my()?static_cast<Value*>(my.get(*result_var_name)):0;
1.20      paf       163:                return result && result!=fresult_initial_void ? result : 0;
1.18      paf       164:        }
1.1       paf       165: 
1.35    ! paf       166:        void invalidate_junctions() {
        !           167:                Array_iter i(junctions);
        !           168:                while(i.has_next())
        !           169:                        static_cast<Junction *>(i.next())->invalidate();
        !           170:                // someday free junctions
        !           171:        }
        !           172: 
1.1       paf       173: public:
                    174:        
                    175:        const Junction& junction;
                    176: 
                    177: private:
1.35    ! paf       178: 
1.25      paf       179:        const String& fname;
                    180: 
1.1       paf       181:        int store_param_index;
1.25      paf       182:        Hash my;/*OR*/MethodParams fnumbered_params;
1.1       paf       183:        Value *fself;
1.15      parser    184: 
                    185: private:
1.35    ! paf       186: 
1.15      parser    187:        Value *fresult_initial_void;
1.35    ! paf       188: 
        !           189: private:
        !           190: 
        !           191:        Array  junctions;
1.1       paf       192: 
                    193: };
                    194: 
                    195: #endif

E-mail: