Annotation of parser3/src/types/pa_vclass.C, revision 1.49

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

E-mail: