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