Annotation of parser3/src/types/pa_vstateless_class.h, revision 1.7
1.6 paf 1: /** @file
2: Parser: stateless class decls.
3:
1.2 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.6 paf 5:
1.2 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7:
1.7 ! paf 8: $Id: pa_vstateless_class.h,v 1.6 2001/03/19 21:39:37 paf Exp $
1.2 paf 9: */
10:
11: #ifndef PA_VSTATELESS_CLASS_H
12: #define PA_VSTATELESS_CLASS_H
13:
14: #include "pa_valiased.h"
15: #include "pa_vhash.h"
16: #include "pa_vjunction.h"
17:
18: class Temp_method;
19:
1.6 paf 20: /**
21: object' class.
22:
23: basically collection of methods.
24:
25: some classes are freeze()-ed after their creation so that
26: malicious users would not alter their method set using some tricks
27: like
28: @verbatim
29: $response.process[@new-resonse-method[]
30: defined here
31: ]
32: @endverbatim
33: affecting unaware neibours.
34:
35: @see VStateless_object, Temp_method
36: */
1.2 paf 37: class VStateless_class : public VAliased {
38: friend Temp_method;
39: public: // Value
40:
1.5 paf 41: /// all: for error reporting after fail(), etc
1.2 paf 42: const char *type() const { return "stateless_class"; }
43:
1.5 paf 44: /// VStateless_class: this
45: VStateless_class *get_class() { return this; }
1.7 ! paf 46:
! 47: /// VStateless_class: +$method
! 48: Value *get_element(const String& aname) {
! 49: // $CLASS, $BASE
! 50: if(Value *result=VAliased::get_element(aname))
! 51: return result;
! 52: // $method=junction(self+class+method)
! 53: if(Junction *junction=get_junction(*this, aname))
! 54: return NEW VJunction(*junction);
! 55:
! 56: return 0;
! 57: }
1.2 paf 58:
59: public: // usage
60:
1.3 paf 61: VStateless_class(Pool& apool, VStateless_class *abase=0) : VAliased(apool, *this),
62: fbase(abase),
1.2 paf 63: read_only(false),
64: fmethods(apool) {
65: }
66:
67: Method *get_method(const String& name) {
68: return static_cast<Method *>(fmethods.get(name));
69: }
70:
71: // make class read-only
72: // this blocks put_method // which could be done with ^process
73: void freeze() { read_only=true; }
74:
75: void add_method(const String& name, Method& method) {
76: put_method(name, &method);
77: }
78: void add_native_method(
79: const char *cstr_name,
80: Native_code_ptr native_code,
81: int min_numbered_params_count, int max_numbered_params_count);
82:
83: void set_base(VStateless_class& abase) {
84: // remember the guy
85: fbase=&abase;
86: }
87: VStateless_class *base() { return fbase; }
88:
89: bool is_or_derived_from(VStateless_class& vclass) {
90: return
91: this==&vclass ||
92: fbase && fbase->is_or_derived_from(vclass);
93: }
94:
95: Junction *get_junction(VAliased& self, const String& name) {
96: if(Method *method=static_cast<Method *>(fmethods.get(name)))
97: return NEW Junction(pool(), self, this, method, 0,0,0,0);
98: if(fbase)
99: return fbase->get_junction(self, name);
100: return 0;
101: }
102:
103: // just stubs, real onces defined below the hierarchy, in
104: virtual Value *get_field(const String& name) { return 0; }
105: virtual bool replace_field(const String& name, Value *value) { return false; }
106:
107: private: // Temp_method
108:
109: void put_method(const String& aname, Method *amethod);
110:
111: private:
112:
113: Hash fmethods;
114:
115: protected:
116:
117: VStateless_class *fbase;
118: bool read_only;
119:
120: };
121:
1.6 paf 122: /// Auto-object used for temporarily substituting/removing class method
1.2 paf 123: class Temp_method {
124: VStateless_class& fclass;
125: const String& fname;
126: Method *saved_method;
127: public:
128: Temp_method(VStateless_class& aclass, const String& aname, Method *amethod) :
129: fclass(aclass),
130: fname(aname),
131: saved_method(aclass.get_method(aname)) {
132: fclass.put_method(aname, amethod);
133: }
134: ~Temp_method() {
135: fclass.put_method(fname, saved_method);
136: }
137: };
138:
139: #endif
E-mail: