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

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

E-mail: