Annotation of parser3/src/types/pa_vstateless_class.h, revision 1.27
1.6 paf 1: /** @file
2: Parser: stateless class decls.
3:
1.22 paf 4: Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.23 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.2 paf 6: */
7:
8: #ifndef PA_VSTATELESS_CLASS_H
9: #define PA_VSTATELESS_CLASS_H
1.27 ! paf 10:
! 11: static const char* IDENT_VSTATELESS_CLASS_H="$Id: zzz $";
1.2 paf 12:
13: #include "pa_valiased.h"
1.13 paf 14: #include "pa_hash.h"
1.2 paf 15: #include "pa_vjunction.h"
16:
17: class Temp_method;
18:
1.6 paf 19: /**
20: object' class.
21:
1.10 paf 22: basically collection of methods [VStateless_class::fmethods, Method]
1.6 paf 23:
24: @see VStateless_object, Temp_method
25: */
1.2 paf 26: class VStateless_class : public VAliased {
1.19 paf 27: friend class Temp_method;
1.2 paf 28: public: // Value
29:
30: const char *type() const { return "stateless_class"; }
31:
1.5 paf 32: /// VStateless_class: this
33: VStateless_class *get_class() { return this; }
1.7 paf 34:
35: /// VStateless_class: +$method
36: Value *get_element(const String& aname) {
1.16 parser 37: // $CLASS
1.7 paf 38: if(Value *result=VAliased::get_element(aname))
39: return result;
1.14 paf 40:
1.7 paf 41: // $method=junction(self+class+method)
42: if(Junction *junction=get_junction(*this, aname))
1.15 parser 43: return new(junction->pool()) VJunction(*junction);
1.7 paf 44:
45: return 0;
46: }
1.2 paf 47:
48: public: // usage
49:
1.24 paf 50: VStateless_class(Pool& apool,
51: const String *aname=0,
1.25 paf 52: VStateless_class *abase=0) : VAliased(apool),
1.24 paf 53: fname(aname),
1.3 paf 54: fbase(abase),
1.2 paf 55: fmethods(apool) {
56: }
57:
1.24 paf 58: const String& name() const {
59: if(!fname) {
60: if(fbase)
61: return fbase->name();
62:
63: throw Exception("parser.runtime",
64: 0,
65: "getting name of nameless class");
66: }
67:
68: return *fname;
69: }
70: const char *name_cstr() const {
71: return this?name().cstr():"<unknown>";
72: }
73: void set_name(const String& aname) {
74: fname=&aname;
75: }
76:
1.26 paf 77: Method *get_method(const String& name) const {
1.2 paf 78: return static_cast<Method *>(fmethods.get(name));
79: }
80:
81: void add_method(const String& name, Method& method) {
82: put_method(name, &method);
83: }
84: void add_native_method(
85: const char *cstr_name,
1.9 paf 86: Method::Call_type call_type,
1.2 paf 87: Native_code_ptr native_code,
88: int min_numbered_params_count, int max_numbered_params_count);
89:
90: void set_base(VStateless_class& abase) {
91: // remember the guy
92: fbase=&abase;
93: }
94: VStateless_class *base() { return fbase; }
95:
96: bool is_or_derived_from(VStateless_class& vclass) {
97: return
98: this==&vclass ||
99: fbase && fbase->is_or_derived_from(vclass);
100: }
101:
102: Junction *get_junction(VAliased& self, const String& name) {
103: if(Method *method=static_cast<Method *>(fmethods.get(name)))
1.15 parser 104: return
105: new(name.pool()) Junction(name.pool(), self, this, method, 0,0,0,0);
1.2 paf 106: if(fbase)
107: return fbase->get_junction(self, name);
108: return 0;
109: }
110:
1.11 paf 111: //@{
112: /// @name just stubs, real onces defined below the hierarchy
1.18 parser 113: virtual Value *get_field(const String& ) { return 0; }
114: virtual bool replace_field(const String& , Value *) { return false; }
1.21 paf 115: //@}
116:
117: /// @returns new value for current class, used in classes/ & VClass
1.18 parser 118: virtual Value *create_new_value(Pool& ) { return 0; }
1.2 paf 119:
120: private: // Temp_method
121:
1.8 paf 122: void put_method(const String& aname, Method *amethod) {
123: fmethods.put(aname, amethod);
124: }
1.2 paf 125:
126: private:
127:
1.24 paf 128: const String *fname;
1.2 paf 129: Hash fmethods;
130:
131: protected:
132:
133: VStateless_class *fbase;
134:
135: };
136:
1.6 paf 137: /// Auto-object used for temporarily substituting/removing class method
1.2 paf 138: class Temp_method {
139: VStateless_class& fclass;
140: const String& fname;
141: Method *saved_method;
142: public:
143: Temp_method(VStateless_class& aclass, const String& aname, Method *amethod) :
144: fclass(aclass),
145: fname(aname),
146: saved_method(aclass.get_method(aname)) {
147: fclass.put_method(aname, amethod);
148: }
149: ~Temp_method() {
150: fclass.put_method(fname, saved_method);
151: }
152: };
153:
154: #endif
E-mail: