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

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.43    ! misha       8: static const char * const IDENT_VCLASS_C="$Date: 2009-08-14 10:39:31 $";
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.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.41      misha      44:        // NOT under 'else' for backward compatiblilty: 
1.25      paf        45:        // if someone used get_xxx names to name regular methods
1.24      paf        46:        // still register method:
1.41      misha      47:        VStateless_class::real_set_method(aname, amethod);
1.39      misha      48: }
                     49: 
                     50: void VClass::set_base(VStateless_class* abase){
                     51:        VStateless_class::set_base(abase);
1.42      misha      52:        if(abase)
                     53:                if(HashStringProperty *props=abase->get_properties())
1.39      misha      54:                        ffields.merge_dont_replace(*props);
1.42      misha      55:                else 
                     56:                        throw Exception("parser.compile",
                     57:                                0,
                     58:                                "Class %s base class (%s) is not user-defined", name_cstr(), abase->name_cstr());
1.39      misha      59: }
                     60: 
                     61: Value* VClass::as(const char* atype) {
                     62:        Value* result=Value::as(atype);
                     63:        return result!=0 ? result : fbase ? fbase->as(atype) : 0;
1.11      paf        64: }
                     65: 
1.22      paf        66: /// VClass: $CLASS, (field/property)=STATIC value;(method)=method_ref with self=object_class
1.39      misha      67: Value* VClass::get_element(Value& aself, const String& aname) {
1.22      paf        68:        // simple things first: $field=static field/property
1.43    ! misha      69:        if(Property* prop=ffields.get(aname)) {
        !            70:                if(prop->getter)
1.39      misha      71:                        return new VJunction(aself, prop->getter, true /*is_getter*/);
1.35      misha      72: 
1.43    ! misha      73:                if(prop->setter)
1.39      misha      74:                        throw Exception(PARSER_RUNTIME,
                     75:                                0,
                     76:                                "this property has no getter method (@GET_%s[])", aname.cstr());
                     77:                 
                     78:                // just field, can be 0 as we don't remove 
                     79:                return prop->value;
1.21      paf        80:        }
                     81: 
1.14      paf        82:        // $CLASS, $method, or other base element
1.43    ! misha      83:        if(Value* result=VStateless_class::get_element(aself, aname))
1.39      misha      84:                return result;
1.7       paf        85: 
1.35      misha      86:        // no field or method found: looking for default getter
1.43    ! misha      87:        if(Value* result=get_default_getter(aself, aname))
1.39      misha      88:                return result;
1.35      misha      89: 
1.7       paf        90:        return 0;
                     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: