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

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.42    ! misha       8: static const char * const IDENT_VCLASS_C="$Date: 2009-08-11 23:54:29 $";
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.39      misha      69:        if (Property* prop=ffields.get(aname)) {
                     70:                if (prop->getter)
                     71:                        return new VJunction(aself, prop->getter, true /*is_getter*/);
1.35      misha      72: 
1.39      misha      73:                if (prop->setter)
                     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.39      misha      83:        if (Value* result=VStateless_class::get_element(aself, aname))
                     84:                return result;
1.7       paf        85: 
1.35      misha      86:        // no field or method found: looking for default getter
1.39      misha      87:        if (Value* result=get_default_getter(aself, aname))
                     88:                return result;
1.35      misha      89: 
1.7       paf        90:        return 0;
                     91: }
                     92: 
1.25      paf        93: /// VClass: (field/property)=value - static values only
1.32      paf        94: const VJunction* VClass::put_element(Value& aself, const String& aname, Value* avalue, bool areplace) {
1.40      misha      95:        if(Property* prop=ffields.get(aname)) {
1.39      misha      96:                if (prop->setter)
                     97:                        return new VJunction(aself, prop->setter);
1.29      paf        98: 
1.40      misha      99:                if(prop->getter)
1.34      misha     100:                        throw Exception(PARSER_RUNTIME,
1.29      paf       101:                                0,
1.31      paf       102:                                "this property has no setter method (@SET_%s[value])", aname.cstr());
1.39      misha     103:                
                    104:                // just field, value can be 0 and unlike usual we don't remove it
                    105:                prop->value=avalue;
                    106:        } else {
1.40      misha     107:                if(areplace)
1.39      misha     108:                        return 0;
                    109: 
1.40      misha     110:                prop=new Property();
                    111:                prop->value=avalue;
                    112:                ffields.put(aname, prop);
                    113: 
                    114:                Array_iterator<VStateless_class *> i(fderived);
                    115:                while(i.has_next()) {
                    116:                        HashStringProperty *props=i.next()->get_properties();
                    117:                        if(props)
                    118:                                props->put_dont_replace(aname, prop);
                    119:                }
1.39      misha     120:        }
1.29      paf       121: 
1.39      misha     122:        return PUT_ELEMENT_REPLACED_ELEMENT;
1.7       paf       123: }
                    124: 
                    125: /// @returns object of this class
1.39      misha     126: Value* VClass::create_new_value(Pool&) { 
                    127:        return new VObject(*this);
1.1       paf       128: }

E-mail: