Annotation of parser3/src/types/pa_vclass.C, revision 1.58
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.58 ! moko 11: volatile const char * IDENT_PA_VCLASS_C="$Id: pa_vclass.C,v 1.57 2017/05/25 15:37:45 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;
1.58 ! moko 124: #ifdef CLASS_GETTER_UNPROTECTED
! 125: prop->getter=0;
! 126: #else
1.46 moko 127: throw Exception(PARSER_RUNTIME, 0, "this property has no setter method (@SET_%s[value])", aname.cstr());
1.58 ! moko 128: #endif
1.46 moko 129: }
1.39 misha 130:
131: // just field, value can be 0 and unlike usual we don't remove it
132: prop->value=avalue;
1.49 moko 133: return PUT_ELEMENT_REPLACED_ELEMENT;
1.39 misha 134: } else {
1.49 moko 135: if(VJunction *result=get_default_setter(aself, aname))
136: return result;
137:
1.40 misha 138: prop=new Property();
139: prop->value=avalue;
140: ffields.put(aname, prop);
141:
142: Array_iterator<VStateless_class *> i(fderived);
143: while(i.has_next()) {
144: HashStringProperty *props=i.next()->get_properties();
145: if(props)
146: props->put_dont_replace(aname, prop);
147: }
1.39 misha 148: }
1.49 moko 149: return 0;
1.7 paf 150: }
151:
1.48 moko 152: /// part of put_element
153: const VJunction* VClass::put_element_replace_only(Value& aself, const String& aname, Value* avalue) {
154: if(Property* prop=ffields.get(aname)) {
155: if (prop->setter)
156: return new VJunction(aself, prop->setter);
1.55 moko 157: #ifdef OBJECT_PROTOTYPE
158: if(!prototype)
159: #endif
160: {
1.57 moko 161: if(prop->getter){
162: if(VJunction *result=get_default_setter(aself, aname))
163: return result;
164: throw Exception(PARSER_RUNTIME, 0, "this property has no setter method (@SET_%s[value])", aname.cstr());
165: }
1.55 moko 166: // just field, value can be 0 and unlike usual we don't remove it
167: prop->value=avalue;
168: return PUT_ELEMENT_REPLACED_ELEMENT;
169: }
1.48 moko 170: }
171: return 0;
172: }
173:
1.7 paf 174: /// @returns object of this class
1.39 misha 175: Value* VClass::create_new_value(Pool&) {
176: return new VObject(*this);
1.1 paf 177: }
1.50 misha 178:
179: const String* VClass::get_json_string(Json_options& options){
180: if(options.default_method){
181: return default_method_2_json_string(*options.default_method, options);
182: }
1.52 moko 183: return options.hash_json_string(get_hash());
1.50 misha 184: }
E-mail: