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