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