Annotation of parser3/src/types/pa_vclass.C, revision 1.40
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.40 ! misha 8: static const char * const IDENT_VCLASS_C="$Date: 2009-08-08 13:30:21 $";
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.40 ! misha 32: void VClass::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.40 ! misha 39: get_property(aname.mid(4, aname.length())).setter=amethod;
1.36 misha 40: } else if(aname=="GET"){
1.40 ! misha 41: set_scalar(amethod);
1.36 misha 42: }
43:
1.40 ! misha 44: // NOT under 'else' for backward compatiblilty & recursion:
1.25 paf 45: // if someone used get_xxx names to name regular methods
1.24 paf 46: // still register method:
1.40 ! misha 47: VStateless_class::set_method(aname, amethod);
1.39 misha 48: }
49:
50: void VClass::set_base(VStateless_class* abase){
51: VStateless_class::set_base(abase);
52: if (abase)
53: if (HashStringProperty *props=abase->get_properties())
54: ffields.merge_dont_replace(*props);
55: }
56:
57: Value* VClass::as(const char* atype) {
58: Value* result=Value::as(atype);
59: return result!=0 ? result : fbase ? fbase->as(atype) : 0;
1.11 paf 60: }
61:
1.22 paf 62: /// VClass: $CLASS, (field/property)=STATIC value;(method)=method_ref with self=object_class
1.39 misha 63: Value* VClass::get_element(Value& aself, const String& aname) {
1.22 paf 64: // simple things first: $field=static field/property
1.39 misha 65: if (Property* prop=ffields.get(aname)) {
66: if (prop->getter)
67: return new VJunction(aself, prop->getter, true /*is_getter*/);
1.35 misha 68:
1.39 misha 69: if (prop->setter)
70: throw Exception(PARSER_RUNTIME,
71: 0,
72: "this property has no getter method (@GET_%s[])", aname.cstr());
73:
74: // just field, can be 0 as we don't remove
75: return prop->value;
1.21 paf 76: }
77:
1.14 paf 78: // $CLASS, $method, or other base element
1.39 misha 79: if (Value* result=VStateless_class::get_element(aself, aname))
80: return result;
1.7 paf 81:
1.35 misha 82: // no field or method found: looking for default getter
1.39 misha 83: if (Value* result=get_default_getter(aself, aname))
84: return result;
1.35 misha 85:
1.7 paf 86: return 0;
87: }
88:
1.25 paf 89: /// VClass: (field/property)=value - static values only
1.32 paf 90: const VJunction* VClass::put_element(Value& aself, const String& aname, Value* avalue, bool areplace) {
1.40 ! misha 91: if(Property* prop=ffields.get(aname)) {
1.39 misha 92: if (prop->setter)
93: return new VJunction(aself, prop->setter);
1.29 paf 94:
1.40 ! misha 95: if(prop->getter)
1.34 misha 96: throw Exception(PARSER_RUNTIME,
1.29 paf 97: 0,
1.31 paf 98: "this property has no setter method (@SET_%s[value])", aname.cstr());
1.39 misha 99:
100: // just field, value can be 0 and unlike usual we don't remove it
101: prop->value=avalue;
102: } else {
1.40 ! misha 103: if(areplace)
1.39 misha 104: return 0;
105:
1.40 ! misha 106: prop=new Property();
! 107: prop->value=avalue;
! 108: ffields.put(aname, prop);
! 109:
! 110: Array_iterator<VStateless_class *> i(fderived);
! 111: while(i.has_next()) {
! 112: HashStringProperty *props=i.next()->get_properties();
! 113: if(props)
! 114: props->put_dont_replace(aname, prop);
! 115: }
1.39 misha 116: }
1.29 paf 117:
1.39 misha 118: return PUT_ELEMENT_REPLACED_ELEMENT;
1.7 paf 119: }
120:
121: /// @returns object of this class
1.39 misha 122: Value* VClass::create_new_value(Pool&) {
123: return new VObject(*this);
1.1 paf 124: }
E-mail: