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