Annotation of parser3/src/types/pa_vstateless_class.h, revision 1.76
1.6 paf 1: /** @file
2: Parser: stateless class decls.
3:
1.73 moko 4: Copyright (c) 2001-2012 Art. Lebedev Studio (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.76 ! misha 11: #define IDENT_PA_VSTATELESS_CLASS_H "$Id: pa_vstateless_class.h,v 1.75 2013/10/04 21:21:57 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: // defines
21:
22: #define CLASS_NAME "CLASS"
1.76 ! misha 23: #ifndef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
1.54 misha 24: #define CLASS_NAMETEXT "CLASS_NAME"
1.63 misha 25: extern const String class_name, class_nametext;
1.76 ! misha 26: #endif
1.38 paf 27:
28: // forwards
29:
1.43 paf 30: class VStateless_class;
1.59 misha 31: typedef Array<VStateless_class*> ArrayClass;
1.66 misha 32: typedef HashString<Property *> HashStringProperty;
33: typedef HashString<Method *> HashStringMethod;
1.43 paf 34:
1.2 paf 35: class Temp_method;
36:
1.6 paf 37: /**
1.30 paf 38: object' class. stores
39: - base: VClass::base()
40: - methods: VStateless_class::fmethods
1.6 paf 41:
1.30 paf 42: @see Method, VStateless_object, Temp_method
1.6 paf 43: */
1.39 paf 44: class VStateless_class: public Value {
1.19 paf 45: friend class Temp_method;
1.43 paf 46:
47: const String* fname;
48: mutable const char* fname_cstr;
1.66 misha 49: HashStringMethod fmethods;
1.43 paf 50:
51: bool flocked;
1.57 misha 52: bool fall_vars_local;
1.61 misha 53: bool fpartial;
1.70 misha 54: Method::Call_type fcall_type;
1.43 paf 55:
56: protected:
57:
58: VStateless_class* fbase;
1.68 misha 59: /// all derived classes, recursively
1.66 misha 60: ArrayClass fderived;
61:
1.60 misha 62: Method* fscalar;
1.58 misha 63: Method* fdefault_getter;
1.71 moko 64: Method* fdefault_setter;
1.43 paf 65:
1.2 paf 66: public: // Value
67:
1.43 paf 68: const char* type() const { return "stateless_class"; }
1.2 paf 69:
1.5 paf 70: /// VStateless_class: this
1.69 misha 71: override VStateless_class* get_class() { return this; }
1.41 paf 72: /// VStateless_class: fbase
1.69 misha 73: override VStateless_class* base() { return fbase; }
1.66 misha 74:
75: override Value* get_element(const String& aname) { return get_element(*this, aname); }
76: /// get_element with aself for VObject junctions
77: virtual Value* get_element(Value& aself, const String& aname);
78:
1.75 moko 79: override const VJunction* put_element(const String& aname, Value* avalue) { return put_element(*this, aname, avalue); }
1.66 misha 80: /// put_element with aself for VObject junctions
1.75 moko 81: virtual const VJunction* put_element(Value& aself, const String& aname, Value* /*avalue*/) {
1.72 moko 82: aself.bark("element can not be stored to %s", &aname);
83: return 0;
84: }
1.66 misha 85:
1.74 moko 86: override Value& as_expr_result();
1.60 misha 87:
1.66 misha 88: Value* get_scalar(Value& aself);
89: void set_scalar(Method* amethod);
1.2 paf 90:
1.66 misha 91: Value* get_default_getter(Value& aself, const String& aname);
92: void set_default_getter(Method* amethod);
1.71 moko 93: bool has_default_getter();
94:
95: VJunction* get_default_setter(Value& aself, const String& aname);
96: void set_default_setter(Method* amethod);
97: bool has_default_setter();
1.60 misha 98:
1.66 misha 99: void add_derived(VStateless_class &aclass);
1.61 misha 100:
1.2 paf 101: public: // usage
102:
1.43 paf 103: VStateless_class(
104: const String* aname=0,
105: VStateless_class* abase=0):
1.24 paf 106: fname(aname),
1.43 paf 107: flocked(false),
1.66 misha 108: fbase(0),
109: fderived(0),
1.58 misha 110: fall_vars_local(false),
1.61 misha 111: fpartial(false),
1.60 misha 112: fscalar(0),
1.70 misha 113: fdefault_getter(0),
1.71 moko 114: fdefault_setter(0),
1.70 misha 115: fcall_type(Method::CT_ANY)
1.61 misha 116: {
1.66 misha 117: set_base(abase);
1.2 paf 118: }
119:
1.43 paf 120: void lock() { flocked=true; }
121:
1.24 paf 122: const String& name() const {
123: if(!fname) {
124: if(fbase)
125: return fbase->name();
126:
1.56 misha 127: throw Exception(PARSER_RUNTIME,
1.24 paf 128: 0,
129: "getting name of nameless class");
130: }
131: return *fname;
132: }
1.66 misha 133:
1.43 paf 134: const char* name_cstr() const{
1.66 misha 135: if(!fname_cstr) // remembering last calculated, and can't reassign 'fname_cstr'!
136: fname_cstr=name().cstr();
137: return fname_cstr;
1.24 paf 138: }
1.61 misha 139:
1.24 paf 140: void set_name(const String& aname) {
141: fname=&aname;
1.66 misha 142: fname_cstr=0;
1.24 paf 143: }
144:
1.43 paf 145: Method* get_method(const String& aname) const {
146: return fmethods.get(aname);
1.2 paf 147: }
148:
1.66 misha 149: HashStringMethod get_methods(){
1.65 misha 150: return fmethods;
151: }
152:
1.61 misha 153: bool is_vars_local(){
154: return fall_vars_local;
155: }
156:
157: void set_all_vars_local(){
1.57 misha 158: fall_vars_local=true;
159: }
160:
1.61 misha 161: bool is_partial(){
162: return fpartial;
163: }
164:
165: void set_partial(){
166: fpartial=true;
1.57 misha 167: }
168:
1.70 misha 169: Method::Call_type get_methods_call_type(){
170: return fcall_type;
171: }
172:
173: void set_methods_call_type(Method::Call_type call_type){
174: if(fcall_type!=Method::CT_ANY)
175: throw Exception(PARSER_RUNTIME,
176: 0,
177: "You can specify call type option in a class only once"
178: );
179: fcall_type=call_type;
180: }
181:
1.2 paf 182: void add_native_method(
1.43 paf 183: const char* cstr_name,
1.9 paf 184: Method::Call_type call_type,
1.43 paf 185: NativeCodePtr native_code,
1.62 misha 186: int min_numbered_params_count,
187: int max_numbered_params_count,
188: Method::Call_optimization call_optimization=Method::CO_WITHOUT_WCONTEXT);
1.66 misha 189:
1.68 misha 190: void set_method(const String& aname, Method* amethod);
191:
1.66 misha 192: /// overrided in VClass
1.68 misha 193: virtual void real_set_method(const String& aname, Method* amethod);
1.66 misha 194: virtual HashStringProperty* get_properties(){ return 0; };
195: virtual void set_base(VStateless_class* abase);
1.2 paf 196:
1.43 paf 197: VStateless_class* base_class() { return fbase; }
1.2 paf 198:
1.66 misha 199: bool derived_from(VStateless_class& vclass){
200: return fbase==&vclass || fbase && fbase->derived_from(vclass);
1.31 paf 201: }
202:
1.21 paf 203: /// @returns new value for current class, used in classes/ & VClass
1.66 misha 204: virtual Value* create_new_value(Pool&) { return 0; }
1.2 paf 205: };
206:
1.6 paf 207: /// Auto-object used for temporarily substituting/removing class method
1.2 paf 208: class Temp_method {
209: VStateless_class& fclass;
210: const String& fname;
1.43 paf 211: Method* saved_method;
1.2 paf 212: public:
1.43 paf 213: Temp_method(VStateless_class& aclass, const String& aname, Method* amethod) :
1.2 paf 214: fclass(aclass),
215: fname(aname),
216: saved_method(aclass.get_method(aname)) {
1.67 misha 217: fclass.set_method(aname, amethod);
1.2 paf 218: }
219: ~Temp_method() {
1.67 misha 220: fclass.set_method(fname, saved_method);
1.2 paf 221: }
222: };
223:
224: #endif
E-mail: