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