Annotation of parser3/src/types/pa_vstateless_class.h, revision 1.95
1.6 paf 1: /** @file
2: Parser: stateless class decls.
3:
1.95 ! moko 4: Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
! 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <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.95 ! moko 11: #define IDENT_PA_VSTATELESS_CLASS_H "$Id: pa_vstateless_class.h,v 1.94 2021/01/02 23:01:11 moko Exp $"
1.43 paf 12:
13: // include
1.2 paf 14:
1.45 paf 15: #include "pa_pool.h"
1.13 paf 16: #include "pa_hash.h"
1.2 paf 17: #include "pa_vjunction.h"
1.43 paf 18: #include "pa_method.h"
1.2 paf 19:
1.38 paf 20: // forwards
21:
1.43 paf 22: class VStateless_class;
1.59 misha 23: typedef Array<VStateless_class*> ArrayClass;
1.91 moko 24: typedef HASH_STRING<Property *> HashStringProperty;
25: typedef HASH_STRING<Method *> HashStringMethod;
1.43 paf 26:
1.2 paf 27: class Temp_method;
28:
1.6 paf 29: /**
1.30 paf 30: object' class. stores
31: - base: VClass::base()
32: - methods: VStateless_class::fmethods
1.6 paf 33:
1.30 paf 34: @see Method, VStateless_object, Temp_method
1.6 paf 35: */
1.39 paf 36: class VStateless_class: public Value {
1.19 paf 37: friend class Temp_method;
1.43 paf 38:
1.66 misha 39: HashStringMethod fmethods;
1.43 paf 40:
41: bool flocked;
1.57 misha 42: bool fall_vars_local;
1.61 misha 43: bool fpartial;
1.70 misha 44: Method::Call_type fcall_type;
1.43 paf 45:
46: protected:
47:
48: VStateless_class* fbase;
1.68 misha 49: /// all derived classes, recursively
1.66 misha 50: ArrayClass fderived;
51:
1.60 misha 52: Method* fscalar;
1.58 misha 53: Method* fdefault_getter;
1.71 moko 54: Method* fdefault_setter;
1.43 paf 55:
1.2 paf 56: public: // Value
57:
1.5 paf 58: /// VStateless_class: this
1.69 misha 59: override VStateless_class* get_class() { return this; }
1.41 paf 60: /// VStateless_class: fbase
1.69 misha 61: override VStateless_class* base() { return fbase; }
1.66 misha 62:
63: override Value* get_element(const String& aname) { return get_element(*this, aname); }
64: /// get_element with aself for VObject junctions
65: virtual Value* get_element(Value& aself, const String& aname);
66:
1.87 moko 67: override const VJunction* put_element(const String& aname, Value* avalue) { return put_element(*this, aname, avalue); }
1.66 misha 68: /// put_element with aself for VObject junctions
1.87 moko 69: virtual const VJunction* put_element(Value& aself, const String& aname, Value* /*avalue*/) {
1.72 moko 70: aself.bark("element can not be stored to %s", &aname);
71: return 0;
72: }
1.66 misha 73:
1.74 moko 74: override Value& as_expr_result();
1.60 misha 75:
1.66 misha 76: Value* get_scalar(Value& aself);
77: void set_scalar(Method* amethod);
1.2 paf 78:
1.66 misha 79: Value* get_default_getter(Value& aself, const String& aname);
80: void set_default_getter(Method* amethod);
1.71 moko 81: bool has_default_getter();
82:
83: VJunction* get_default_setter(Value& aself, const String& aname);
84: void set_default_setter(Method* amethod);
85: bool has_default_setter();
1.60 misha 86:
1.66 misha 87: void add_derived(VStateless_class &aclass);
1.61 misha 88:
1.2 paf 89: public: // usage
90:
1.94 moko 91: static bool gall_vars_local; // @conf[] $MAIN:LOCALS
92:
1.80 moko 93: VStateless_class(VStateless_class* amethoded_donor=0):
1.43 paf 94: flocked(false),
1.94 moko 95: fall_vars_local(gall_vars_local),
1.89 moko 96: fpartial(false),
97: fcall_type(Method::CT_ANY),
1.66 misha 98: fbase(0),
99: fderived(0),
1.60 misha 100: fscalar(0),
1.70 misha 101: fdefault_getter(0),
1.89 moko 102: fdefault_setter(0)
1.80 moko 103: {
104: if(amethoded_donor)
105: fmethods.merge_dont_replace(amethoded_donor->fmethods);
1.2 paf 106: }
107:
1.43 paf 108: void lock() { flocked=true; }
109:
1.87 moko 110: Method* get_method(const String::Body &aname) const {
1.43 paf 111: return fmethods.get(aname);
1.2 paf 112: }
113:
1.86 moko 114: HashStringMethod& get_methods(){
1.65 misha 115: return fmethods;
116: }
117:
1.61 misha 118: bool is_vars_local(){
119: return fall_vars_local;
120: }
121:
122: void set_all_vars_local(){
1.57 misha 123: fall_vars_local=true;
124: }
125:
1.61 misha 126: bool is_partial(){
127: return fpartial;
128: }
129:
130: void set_partial(){
131: fpartial=true;
1.57 misha 132: }
133:
1.70 misha 134: Method::Call_type get_methods_call_type(){
135: return fcall_type;
136: }
137:
138: void set_methods_call_type(Method::Call_type call_type){
139: if(fcall_type!=Method::CT_ANY)
1.87 moko 140: throw Exception(PARSER_RUNTIME, 0, "You can specify call type option in a class only once");
1.70 misha 141: fcall_type=call_type;
142: }
143:
1.2 paf 144: void add_native_method(
1.43 paf 145: const char* cstr_name,
1.9 paf 146: Method::Call_type call_type,
1.43 paf 147: NativeCodePtr native_code,
1.87 moko 148: int min_numbered_params_count,
149: int max_numbered_params_count,
1.62 misha 150: Method::Call_optimization call_optimization=Method::CO_WITHOUT_WCONTEXT);
1.66 misha 151:
1.68 misha 152: void set_method(const String& aname, Method* amethod);
153:
1.66 misha 154: /// overrided in VClass
1.68 misha 155: virtual void real_set_method(const String& aname, Method* amethod);
1.66 misha 156: virtual HashStringProperty* get_properties(){ return 0; };
157: virtual void set_base(VStateless_class* abase);
1.2 paf 158:
1.43 paf 159: VStateless_class* base_class() { return fbase; }
1.2 paf 160:
1.66 misha 161: bool derived_from(VStateless_class& vclass){
1.77 moko 162: return fbase==&vclass || ( fbase && fbase->derived_from(vclass) );
1.31 paf 163: }
164:
1.21 paf 165: /// @returns new value for current class, used in classes/ & VClass
1.66 misha 166: virtual Value* create_new_value(Pool&) { return 0; }
1.92 moko 167:
168: const Method* get_element_method(const String &aname) { // not get_method() to allow $aname[$method]
169: if(Value* value=get_element(aname))
170: if(Junction* junction=value->get_junction())
171: return junction->method;
172: return 0;
173: }
174:
1.2 paf 175: };
176:
1.6 paf 177: /// Auto-object used for temporarily substituting/removing class method
1.2 paf 178: class Temp_method {
179: VStateless_class& fclass;
180: const String& fname;
1.43 paf 181: Method* saved_method;
1.2 paf 182: public:
1.87 moko 183: Temp_method(VStateless_class& aclass, const String& aname, Method* amethod) :
1.2 paf 184: fclass(aclass),
185: fname(aname),
186: saved_method(aclass.get_method(aname)) {
1.67 misha 187: fclass.set_method(aname, amethod);
1.2 paf 188: }
189: ~Temp_method() {
1.67 misha 190: fclass.set_method(fname, saved_method);
1.2 paf 191: }
192: };
193:
1.85 moko 194:
195: class VBaseClassWrapper: public Value {
196:
197: VStateless_class& fclass;
198: Value& fself;
199:
200: public: // Value
201:
202: override const char* type() const { return fclass.type(); }
203: override VStateless_class* get_class() { return &fclass; }
204: override VStateless_class* base() { return fclass.base(); }
205:
206: override Value* get_element(const String& aname) { return fclass.get_element(fself, aname); }
207: override const VJunction* put_element(const String& aname, Value* avalue) { return fclass.put_element(fself, aname, avalue); }
208: override Value& as_expr_result(){ return fclass.as_expr_result(); }
209:
210: public: // usage
211:
212: VBaseClassWrapper(VStateless_class& aclass, Value& aself): fclass(aclass), fself(aself) {};
213: };
214:
1.2 paf 215: #endif
E-mail: