Annotation of parser3/src/types/pa_vmethod_frame.h, revision 1.80
1.3 paf 1: /** @file
1.5 paf 2: Parser: @b method_frame write context
1.3 paf 3:
1.68 misha 4: Copyright (c) 2001-2009 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.80 ! misha 11: static const char * const IDENT_VMETHOD_FRAME_H="$Date: 2009-05-13 07:35:27 $";
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: */
1.78 misha 32: class MethodParams {
1.46 paf 33: public:
1.78 misha 34: MethodParams() : felements(0), fused(0){}
35:
1.77 misha 36: void store_params(Value **params, size_t count){
1.78 misha 37: felements=params;
1.77 misha 38: fused=count;
39: }
40:
1.78 misha 41: inline size_t count() const { return fused; }
42:
43: inline Value *get(size_t index) const {
44: assert(index<count());
45: return felements[index];
46: }
47:
1.77 misha 48: inline Value& operator[] (size_t index) { return *get(index); }
1.46 paf 49:
50: Value& last() { return *get(count()-1); }
51:
52: /// handy is-value-a-junction ensurer
53: Value& as_junction(int index, const char* msg) {
1.56 paf 54: Value* value=get(index);
55: return as_junction(value, msg, index);
56: }
57: /// handy is-value-a-junction ensurer
58: Value& as_junction(Value* value, const char* msg, int index) {
59: return get_as(value, true, msg, index);
1.46 paf 60: }
61: /// handy value-is-not-a-junction ensurer
62: Value& as_no_junction(int index, const char* msg) {
1.56 paf 63: Value* value=get(index);
64: return as_no_junction(value, msg, index);
65: }
66: /// handy value-is-not-a-junction ensurer
67: Value& as_no_junction(Value* value, const char* msg, int index) {
68: return get_as(value, false, msg, index);
1.46 paf 69: }
1.62 misha 70: /// handy is-value-a-junction ensurer or can be auto-processed
71: Value& as_expression(int index, const char* msg) {
72: Value* value=get(index);
73: if(value->is_evaluated_expr()){
74: return *value;
75: } else {
76: return get_as(value, true, msg, index);
77: }
78: }
1.46 paf 79: /// handy expression auto-processing to double
80: double as_double(int index, const char* msg, Request& r) {
1.56 paf 81: Value* value=get(index);
1.60 paf 82: if(!value->is_evaluated_expr())
1.57 paf 83: value=&get_processed(value, msg, index, r);
84: return value->as_double();
1.46 paf 85: }
86: /// handy expression auto-processing to int
87: int as_int(int index, const char* msg, Request& r) {
1.56 paf 88: Value* value=get(index);
1.60 paf 89: if(!value->is_evaluated_expr())
1.57 paf 90: value=&get_processed(value, msg, index, r);
91: return value->as_int();
1.46 paf 92: }
93: /// handy expression auto-processing to bool
94: bool as_bool(int index, const char* msg, Request& r) {
1.56 paf 95: Value* value=get(index);
1.60 paf 96: if(!value->is_evaluated_expr())
1.59 paf 97: value=&get_processed(value, msg, index, r);
98: return value->as_bool();
1.46 paf 99: }
100: /// handy string ensurer
101: const String& as_string(int index, const char* msg) {
102: return as_no_junction(index, msg).as_string();
103: }
104: private:
105:
1.78 misha 106: Value **felements;
107: size_t fused;
108:
1.46 paf 109: /// handy value-is/not-a-junction ensurer
1.56 paf 110: Value& get_as(Value* value, bool as_junction, const char* msg, int index) {
111: if((value->get_junction()!=0) ^ as_junction)
1.61 misha 112: throw Exception(PARSER_RUNTIME,
1.46 paf 113: 0,
114: "%s (parameter #%d)", msg, 1+index);
115:
1.56 paf 116: return *value;
1.46 paf 117: }
118:
1.56 paf 119: Value& get_processed(Value* value, const char* msg, int index, Request& r);
1.46 paf 120:
121: };
122:
1.4 paf 123: /** Method frame write context
124: accepts values written by method code
125: also handles method parameters and local variables
1.3 paf 126: */
1.46 paf 127: class VMethodFrame: public WContext {
1.63 misha 128: protected:
1.46 paf 129: VMethodFrame *fcaller;
130:
1.72 misha 131: HashStringValue* my; /*OR*/ MethodParams fnumbered_params;
1.46 paf 132: Value* fself;
133:
1.64 misha 134: typedef const VJunction* (VMethodFrame::*put_element_t)(const String& aname, Value* avalue);
135: put_element_t put_element_impl;
1.46 paf 136:
1.1 paf 137: public: // Value
138:
1.46 paf 139: override const char* type() const { return "method_frame"; }
1.20 paf 140:
141: /// VMethodFrame: $result | parent get_string(=accumulated fstring)
1.46 paf 142: override const String* get_string() {
1.20 paf 143: // check the $result value
1.46 paf 144: Value* result=get_result_variable();
1.20 paf 145: // if we have one, return it's string value, else return as usual: accumulated fstring or fvalue
146: return result ? result->get_string() : WContext::get_string();
147: }
148:
1.40 paf 149: /// VMethodFrame: my or self_transparent or $caller
1.51 paf 150: override Value* get_element(const String& aname, Value& /*aself*/, bool looking_up) {
1.42 paf 151: if(aname==CALLER_ELEMENT_NAME)
1.40 paf 152: return caller();
1.42 paf 153:
1.67 misha 154: if(aname==SELF_ELEMENT_NAME)
155: return &self();
156:
1.66 misha 157: Value* result;
158: if(my && (result=my->get(aname)))
159: return result;
160:
161: if(result=self().get_element(aname, self(), looking_up))
162: return result;
1.40 paf 163:
164: return 0;
1.1 paf 165: }
166:
1.4 paf 167: /// VMethodFrame: self_transparent
1.46 paf 168: override VStateless_class* get_class() { return self().get_class(); }
1.44 paf 169:
170: /// VMethodFrame: self_transparent
1.46 paf 171: override Value* base() { return self().base(); }
1.1 paf 172:
1.64 misha 173: /// VMethodFrame: my or self_transparent
174: override const VJunction* put_element(Value& /*aself*/, const String& aname, Value* avalue, bool /*areplace*/) {
175: return (this->*put_element_impl)(aname, avalue);
176: }
177:
1.69 misha 178: /// VMethodFrame: appends a fstring to result
179: override void write(const String& astring, String::Language alang) {
1.79 misha 180: #ifdef OPTIMIZE_RESULT
181: switch (junction.method->result_optimization){
182: case Method::RO_USE_RESULT:
183: return;
184: case Method::RO_UNKNOWN:
185: if(get_result_variable()){
186: ((Method*)junction.method)->result_optimization=Method::RO_USE_RESULT;
187: return;
188: }
1.69 misha 189: }
1.79 misha 190: #endif
191: WContext::write(astring, alang);
1.69 misha 192: }
193:
1.64 misha 194: private:
195:
196: const VJunction* put_element_local(const String& aname, Value* avalue){
197: set_my_variable(aname, *avalue);
198: return PUT_ELEMENT_REPLACED_ELEMENT;
199: }
200:
201: const VJunction* put_element_global(const String& aname, Value* avalue){
202: if(my && my->put_replaced(aname, avalue))
203: return PUT_ELEMENT_REPLACED_ELEMENT;
204: return self().put_element(self(), aname, avalue, false/*=always, areplace*/);
205: }
206:
1.30 paf 207: public: // WContext
1.1 paf 208:
1.46 paf 209: override StringOrValue result() {
1.80 ! misha 210: if(my){
! 211: // check the $result value
! 212: Value* result_value=get_result_variable();
! 213: // if we have one, return it, else return as usual: accumulated fstring or fvalue
! 214: if(result_value)
! 215: return StringOrValue(*result_value);
1.79 misha 216: #ifdef OPTIMIZE_RESULT
1.80 ! misha 217: if(junction.method->result_optimization==Method::RO_USE_RESULT)
! 218: return StringOrValue(*VVoid::get());
! 219: ((Method *)junction.method)->result_optimization=Method::RO_USE_WCONTEXT;
1.79 misha 220: #ifdef OPTIMIZE_CALL // nested as CO_WITHOUT_WCONTEXT assumes that $result not used
1.80 ! misha 221: ((Method *)junction.method)->call_optimization=Method::CO_WITHOUT_WCONTEXT;
1.79 misha 222: #endif
223: #endif
1.80 ! misha 224: }
1.79 misha 225: return WContext::result();
1.1 paf 226: }
227:
1.46 paf 228: void write(Value& avalue, String::Language alang) {
229: WContext::write(avalue, alang);
230: }
231:
1.1 paf 232: public: // usage
233:
1.46 paf 234: VMethodFrame(
1.40 paf 235: const Junction& ajunction/*info: always method-junction*/,
1.46 paf 236: VMethodFrame *acaller);
1.1 paf 237:
1.40 paf 238: VMethodFrame *caller() { return fcaller; }
1.25 paf 239:
1.76 misha 240: #ifdef USE_DESTRUCTORS
1.75 misha 241: ~VMethodFrame(){
242: if(my){
243: delete my;
244: }
245: }
1.76 misha 246: #endif
1.75 misha 247:
1.38 paf 248: void set_self(Value& aself) { fself=&aself; }
1.41 paf 249: /// we sure that someone already set our self with VMethodFrame::set_self(Value&)
250: Value& self() { return *fself; }
1.1 paf 251:
1.77 misha 252: size_t method_params_count() {
1.27 paf 253: const Method& method=*junction.method;
1.77 misha 254: return method.params_names ? method.params_names->count():0;
1.27 paf 255: }
1.77 misha 256:
257: void store_params(Value **params, size_t count) {
1.1 paf 258: const Method& method=*junction.method;
1.46 paf 259: size_t max_params=
1.1 paf 260: method.max_numbered_params_count?method.max_numbered_params_count:
1.46 paf 261: method.params_names?method.params_names->count():
1.1 paf 262: 0;
1.77 misha 263:
264: if(count>max_params)
1.61 misha 265: throw Exception(PARSER_RUNTIME,
1.46 paf 266: 0, //&name(),
1.7 paf 267: "method of %s (%s) accepts maximum %d parameter(s)",
1.38 paf 268: junction.self.get_class()->name_cstr(),
269: junction.self.type(),
1.1 paf 270: max_params);
1.78 misha 271:
272: if(method.params_names) {
1.77 misha 273: size_t i=0;
274:
275: for (; i<count; i++){
276: const String& fname=*(*method.params_names)[i];
277: set_my_variable(fname, *params[i]);
278: }
279:
1.46 paf 280: size_t param_count=method.params_names->count();
1.77 misha 281: for(; i<param_count; i++) {
282: const String& fname=*(*method.params_names)[i];
1.68 misha 283: my->put(fname, VVoid::get());
1.1 paf 284: }
1.77 misha 285: } else {
286: fnumbered_params.store_params(params,count);
1.46 paf 287: }
1.1 paf 288: }
289:
1.78 misha 290: void empty_params(){
291: const Method& method=*junction.method;
292: if(method.params_names){
293: size_t param_count=method.params_names->count();
294: for(size_t i=0; i<param_count; i++) {
295: const String& fname=*(*method.params_names)[i];
296: my->put(fname, VVoid::get());
297: }
298: }
299: }
300:
1.72 misha 301: MethodParams* numbered_params() { return &fnumbered_params; }
1.18 paf 302:
1.63 misha 303: protected:
1.18 paf 304:
1.46 paf 305: void set_my_variable(const String& fname, Value& value) {
306: my->put(fname, &value); // remember param
1.25 paf 307: }
308:
1.46 paf 309: Value* get_result_variable();
1.1 paf 310:
311: public:
312:
313: const Junction& junction;
314:
1.41 paf 315: };
316:
317: /// Auto-object used for temporary changing VMethod_frame::fself.
318: class Temp_method_frame_self {
319: VMethodFrame& fmethod_frame;
1.46 paf 320: Value* saved_self;
1.41 paf 321: public:
322: Temp_method_frame_self(VMethodFrame& amethod_frame, Value& aself) :
323: fmethod_frame(amethod_frame),
1.46 paf 324: saved_self(&amethod_frame.self()) {
1.41 paf 325: fmethod_frame.set_self(aself);
326: }
327: ~Temp_method_frame_self() {
1.46 paf 328: fmethod_frame.set_self(*saved_self);
1.41 paf 329: }
1.1 paf 330: };
331:
332: #endif
E-mail: