Annotation of parser3/src/types/pa_vclass.C, revision 1.56
1.7 paf 1: /** @file
2: Parser: @b class parser class impl.
1.1 paf 3:
1.56 ! moko 4: Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)
1.7 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6: */
7:
1.45 moko 8: #include "pa_vclass.h"
1.48 moko 9: #include "pa_vobject.h"
1.7 paf 10:
1.56 ! moko 11: volatile const char * IDENT_PA_VCLASS_C="$Id: pa_vclass.C,v 1.55 2016/09/13 16:12:55 moko Exp $" IDENT_PA_VCLASS_H;
1.55 moko 12:
13: #ifdef OBJECT_PROTOTYPE
14: bool VClass::prototype = true;
15: #endif
1.1 paf 16:
1.39 misha 17: Property& VClass::get_property(const String& aname) {
1.40 misha 18: Property* result=ffields.get(aname);
19: if (result) {
1.39 misha 20: if (!result->getter && !result->setter) {
21: // can occur in ^process
22: Value *v=result->value;
1.24 paf 23: throw Exception("parser.compile",
1.39 misha 24: &aname,
1.54 moko 25: "property can not be created, already exists field (%s) with that name", v ? v->type() : "unknown");
1.39 misha 26: }
1.40 misha 27:
28: // cloning existing property
29: result=new Property(*result);
1.24 paf 30: } else {
1.39 misha 31: result=new Property();
1.24 paf 32: }
1.40 misha 33: ffields.put(aname, result);
1.24 paf 34: return *result;
35: }
36:
1.41 misha 37: void VClass::real_set_method(const String& aname, Method* amethod) {
1.35 misha 38: if(aname.starts_with("GET_")){
1.36 misha 39: if(aname=="GET_DEFAULT")
1.40 misha 40: set_default_getter(amethod);
1.35 misha 41: else
1.40 misha 42: get_property(aname.mid(4, aname.length())).getter=amethod;
1.36 misha 43: } else if(aname.starts_with("SET_")){
1.44 moko 44: if(aname=="SET_DEFAULT")
45: set_default_setter(amethod);
46: else
47: get_property(aname.mid(4, aname.length())).setter=amethod;
1.36 misha 48: } else if(aname=="GET"){
1.40 misha 49: set_scalar(amethod);
1.36 misha 50: }
51:
1.41 misha 52: // NOT under 'else' for backward compatiblilty:
1.25 paf 53: // if someone used get_xxx names to name regular methods
1.24 paf 54: // still register method:
1.41 misha 55: VStateless_class::real_set_method(aname, amethod);
1.39 misha 56: }
57:
58: void VClass::set_base(VStateless_class* abase){
59: VStateless_class::set_base(abase);
1.51 moko 60: if(abase) {
1.42 misha 61: if(HashStringProperty *props=abase->get_properties())
1.39 misha 62: ffields.merge_dont_replace(*props);
1.51 moko 63: else
1.42 misha 64: throw Exception("parser.compile",
65: 0,
1.54 moko 66: "Class %s base class (%s) is not user-defined", type(), abase->type());
1.51 moko 67: }
1.39 misha 68: }
69:
70: Value* VClass::as(const char* atype) {
71: Value* result=Value::as(atype);
72: return result!=0 ? result : fbase ? fbase->as(atype) : 0;
1.11 paf 73: }
74:
1.22 paf 75: /// VClass: $CLASS, (field/property)=STATIC value;(method)=method_ref with self=object_class
1.39 misha 76: Value* VClass::get_element(Value& aself, const String& aname) {
1.22 paf 77: // simple things first: $field=static field/property
1.43 misha 78: if(Property* prop=ffields.get(aname)) {
79: if(prop->getter)
1.39 misha 80: return new VJunction(aself, prop->getter, true /*is_getter*/);
1.35 misha 81:
1.46 moko 82: if(prop->setter){
83: if(Value *result=get_default_getter(aself, aname))
84: return result;
85: throw Exception(PARSER_RUNTIME, 0, "this property has no getter method (@GET_%s[])", aname.cstr());
86: }
1.39 misha 87:
88: // just field, can be 0 as we don't remove
89: return prop->value;
1.21 paf 90: }
91:
1.14 paf 92: // $CLASS, $method, or other base element
1.43 misha 93: if(Value* result=VStateless_class::get_element(aself, aname))
1.39 misha 94: return result;
1.7 paf 95:
1.35 misha 96: // no field or method found: looking for default getter
1.44 moko 97: return get_default_getter(aself, aname);
1.7 paf 98: }
99:
1.43 misha 100: static void add_field(
101: HashStringProperty::key_type key,
102: HashStringProperty::value_type prop,
103: HashStringValue* result
104: ){
105: if(prop->value)
106: result->put(key, prop->value);
107: }
108:
1.47 misha 109: HashStringValue* VClass::get_hash() {
1.43 misha 110: HashStringValue* result=new HashStringValue();
111: ffields.for_each(add_field, result);
112: return result;
113: }
114:
1.25 paf 115: /// VClass: (field/property)=value - static values only
1.48 moko 116: const VJunction* VClass::put_element(Value& aself, const String& aname, Value* avalue) {
1.40 misha 117: if(Property* prop=ffields.get(aname)) {
1.39 misha 118: if (prop->setter)
119: return new VJunction(aself, prop->setter);
1.29 paf 120:
1.46 moko 121: if(prop->getter){
122: if(VJunction *result=get_default_setter(aself, aname))
123: return result;
124: throw Exception(PARSER_RUNTIME, 0, "this property has no setter method (@SET_%s[value])", aname.cstr());
125: }
1.39 misha 126:
127: // just field, value can be 0 and unlike usual we don't remove it
128: prop->value=avalue;
1.49 moko 129: return PUT_ELEMENT_REPLACED_ELEMENT;
1.39 misha 130: } else {
1.49 moko 131: if(VJunction *result=get_default_setter(aself, aname))
132: return result;
133:
1.40 misha 134: prop=new Property();
135: prop->value=avalue;
136: ffields.put(aname, prop);
137:
138: Array_iterator<VStateless_class *> i(fderived);
139: while(i.has_next()) {
140: HashStringProperty *props=i.next()->get_properties();
141: if(props)
142: props->put_dont_replace(aname, prop);
143: }
1.39 misha 144: }
1.49 moko 145: return 0;
1.7 paf 146: }
147:
1.48 moko 148: /// part of put_element
149: const VJunction* VClass::put_element_replace_only(Value& aself, const String& aname, Value* avalue) {
150: if(Property* prop=ffields.get(aname)) {
151: if (prop->setter)
152: return new VJunction(aself, prop->setter);
153:
154: if(prop->getter){
155: if(VJunction *result=get_default_setter(aself, aname))
156: return result;
157: throw Exception(PARSER_RUNTIME, 0, "this property has no setter method (@SET_%s[value])", aname.cstr());
158: }
159:
1.55 moko 160: #ifdef OBJECT_PROTOTYPE
161: if(!prototype)
162: #endif
163: {
164: // just field, value can be 0 and unlike usual we don't remove it
165: prop->value=avalue;
166: return PUT_ELEMENT_REPLACED_ELEMENT;
167: }
1.48 moko 168: }
169: return 0;
170: }
171:
1.7 paf 172: /// @returns object of this class
1.39 misha 173: Value* VClass::create_new_value(Pool&) {
174: return new VObject(*this);
1.1 paf 175: }
1.50 misha 176:
177: const String* VClass::get_json_string(Json_options& options){
178: if(options.default_method){
179: return default_method_2_json_string(*options.default_method, options);
180: }
1.52 moko 181: return options.hash_json_string(get_hash());
1.50 misha 182: }
E-mail: