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