Annotation of parser3/src/types/pa_vobject.C, revision 1.19

1.1       paf         1: /**    @file
                      2:        Parser: @b object class impl.
                      3: 
1.16      paf         4:        Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com)
1.1       paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
                      6: */
                      7: 
                      8: #include "pa_vobject.h"
1.7       paf         9: #include "pa_vhash.h"
                     10: #include "pa_vtable.h"
1.1       paf        11: 
1.19    ! paf        12: static const char * const IDENT_VOBJECT_C="$Date: 2005/07/26 12:43:05 $";
1.5       paf        13: 
1.13      paf        14: Value* VObject::as(const char* atype, bool looking_up) { 
1.5       paf        15:        if(!looking_up)
1.10      paf        16:                return get_last_derived().as(atype, true/*the only user*/); // figure out from last_derivate upwards
1.5       paf        17: 
                     18:        // is it me?
1.6       paf        19:        if(Value *result=Value::as(atype, false))
                     20:                return result;
1.5       paf        21: 
                     22:        // is it my base?
                     23:        if(fbase) {
1.6       paf        24:                if(Value *result=fbase->as(atype, true))
                     25:                        return result;
1.5       paf        26:        }
                     27: 
                     28:        // neither
1.6       paf        29:        return 0;
1.5       paf        30: }
1.1       paf        31: 
1.7       paf        32: /// VObject: from possible parent, if any
                     33: bool VObject::is_defined() const {
                     34:        return fbase?fbase->is_defined():Value::is_defined();
                     35: }
                     36: /// VObject: from possible parent, if any
1.13      paf        37: Value& VObject::as_expr_result(bool) { 
1.7       paf        38:        return fbase?fbase->as_expr_result():Value::as_expr_result();
                     39: }
                     40: /// VObject: from possible parent, if any
                     41: int VObject::as_int() const {
                     42:        return fbase?fbase->as_int():Value::as_int();
                     43: }
                     44: /// VObject: from possible parent, if any
                     45: double VObject::as_double() {
                     46:        return fbase?fbase->as_double():Value::as_double();
                     47: }
                     48: /// VObject: from possible parent, if any
                     49: bool VObject::as_bool() const { 
                     50:        return fbase?fbase->as_bool():Value::as_bool();
                     51: }
                     52: /// VObject: from possible parent, if any
1.13      paf        53: VFile* VObject::as_vfile(String::Language lang, const Request_charsets *charsets) {
                     54:        return fbase?fbase->as_vfile(lang, charsets):
                     55:                Value::as_vfile(lang, charsets);
1.7       paf        56: }
                     57: 
                     58: /// VObject: from possible parent, if any
1.13      paf        59: HashStringValue* VObject::get_hash() {
                     60:        if(Value* vhash=get_last_derived().as(VHASH_TYPE, false))
                     61:                return vhash->get_hash();
1.7       paf        62: 
                     63:        return 0;
                     64: }
                     65: /// VObject: from possible 'table' parent
                     66: Table *VObject::get_table() {
1.10      paf        67:        if(Value *vtable=get_last_derived().as(VTABLE_TYPE, false))
1.7       paf        68:                return vtable->get_table();
                     69: 
                     70:        return 0;
                     71: }
1.1       paf        72: 
                     73: /// VObject: (field)=value;(CLASS)=vclass;(method)=method_ref
1.14      paf        74: Value* VObject::get_element(const String& aname, Value&, bool looking_up) {
1.8       paf        75:        // simple things first: $field=ffields.field
1.13      paf        76:        if(Value* result=ffields.get(aname))
1.8       paf        77:                return result;
                     78: 
1.1       paf        79:        // gets element from last_derivate upwards
1.5       paf        80:        if(!looking_up) {
1.1       paf        81:                // $CLASS
                     82:                if(aname==CLASS_NAME)
                     83:                        return get_class();
                     84: 
1.10      paf        85:                // $virtual_method
                     86:                VObject& last_derived=get_last_derived();
1.13      paf        87:                if(Value* result=last_derived.stateless_object__get_element(aname, last_derived))
1.1       paf        88:                        return result;
                     89:        }
                     90: 
1.10      paf        91:        // up the tree for other $virtual_field try...
1.1       paf        92:        if(fbase)
1.13      paf        93:                if(Value* result=fbase->get_element(aname, *fbase, true))
1.1       paf        94:                        return result;
                     95: 
                     96:        return 0;
                     97: }
1.13      paf        98: Value* VObject::stateless_object__get_element(const String& aname, Value& aself) {
1.10      paf        99:        return VStateless_object::get_element(aname, aself, false);
                    100: }
1.1       paf       101: 
1.19    ! paf       102: // from pa_vclass.C
        !           103: const Method* pa_prevent_overwrite_property(Value* value);
        !           104: 
        !           105: /// VObject: (field/property)=value
1.18      paf       106: const Junction* VObject::put_element(const String& aname, Value* avalue, bool replace) {
1.19    ! paf       107:        if(fbase)
        !           108:                if(const Junction* result=fbase->put_element(aname, avalue, true))
        !           109:                        return result; // replaced in base dynamic(NOT static!) fields
1.1       paf       110: 
1.19    ! paf       111:        if(replace) {
        !           112:                // we're in some parent, we should NOT try to insert there, only IF that field/property existed there
        !           113:                if(const Method* method=ffields.maybe_put_replaced<const Method*>(aname, avalue, pa_prevent_overwrite_property) ) {
        !           114:                        if(method==reinterpret_cast<const Method*>(1)) // existed, but not were not property?
        !           115:                                return PUT_ELEMENT_REPLACED_ELEMENT;
        !           116:                        return new Junction(*this, method, true /*is_setter*/);
        !           117:                }
        !           118: 
        !           119:                return 0; // NOT replaced/putted anything to parent [there were NO such field/property for us to fill]
        !           120:        } else {
        !           121:                if(const Junction* result=VStateless_object::put_element(aname, avalue, true))
        !           122:                        return result; // replaced in base statics fields
1.11      paf       123: 
1.2       paf       124:                ffields.put(aname, avalue);
1.19    ! paf       125:                return 0; // were simply added [not existed before]
1.2       paf       126:        }
1.1       paf       127: }
                    128: 

E-mail: