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