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

1.1       paf         1: /**    @file
                      2:        Parser: @b object class impl.
                      3: 
                      4:        Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
                      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.9.4.3 ! paf        12: static const char* IDENT_VOBJECT_C="$Date: 2002/10/31 13:07:50 $";
1.5       paf        13: 
1.6       paf        14: Value *VObject::as(const char *atype, bool looking_up) { 
1.5       paf        15:        if(!looking_up)
1.9.4.1   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
                     37: Value *VObject::as_expr_result(bool) { 
                     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
                     53: VFile *VObject::as_vfile(String::Untaint_lang lang, bool origins_mode) {
                     54:        return fbase?fbase->as_vfile(lang, origins_mode):Value::as_vfile(lang, origins_mode);
                     55: }
                     56: 
                     57: /// VObject: from possible parent, if any
                     58: Hash *VObject::get_hash(const String *source) {
1.9.4.1   paf        59:        if(Value *vhash=get_last_derived().as(VHASH_TYPE, false))
1.7       paf        60:                return vhash->get_hash(source);
                     61: 
                     62:        return 0;
                     63: }
                     64: /// VObject: from possible 'table' parent
                     65: Table *VObject::get_table() {
1.9.4.1   paf        66:        if(Value *vtable=get_last_derived().as(VTABLE_TYPE, false))
1.7       paf        67:                return vtable->get_table();
                     68: 
                     69:        return 0;
                     70: }
1.1       paf        71: 
                     72: /// VObject: (field)=value;(CLASS)=vclass;(method)=method_ref
1.9.4.1   paf        73: Value *VObject::get_element(const String& aname, Value& aself, bool looking_up) {
1.8       paf        74:        // simple things first: $field=ffields.field
                     75:        if(Value *result=static_cast<Value *>(ffields.get(aname)))
                     76:                return result;
                     77: 
1.9.4.3 ! paf        78:        // gets element from last_derivate upwards
1.5       paf        79:        if(!looking_up) {
1.9.4.3 ! paf        80:                // $CLASS
        !            81:                if(aname==CLASS_NAME)
        !            82:                        return get_class();
        !            83: 
        !            84:                VObject& last_derived=get_last_derived();
        !            85: 
        !            86:                // $virtual_method
        !            87:                if(Value *result=last_derived.stateless_object__get_element(aname, last_derived))
1.1       paf        88:                        return result;
1.9.4.3 ! paf        89:                
        !            90:                // $virtual_field
        !            91:                return last_derived.get_element(aname, last_derived, true/*the only user*/);
1.1       paf        92:        }
                     93: 
1.9.4.3 ! paf        94:        // up the tree for other $virtual_field try...
        !            95:        if(fbase)
        !            96:                if(Value *result=fbase->get_element(aname, *fbase, true))
1.1       paf        97:                        return result;
                     98: 
                     99:        return 0;
1.9.4.3 ! paf       100: }
        !           101: Value *VObject::stateless_object__get_element(const String& aname, Value& aself) {
        !           102:        return VStateless_object::get_element(aname, aself, false);
1.1       paf       103: }
                    104: 
                    105: /// VObject: (field)=value
                    106: bool VObject::put_element(const String& aname, Value *avalue, bool replace) {
                    107:        // replaces element to last_derivate upwards or stores it in self
                    108:        // speed1:
                    109:        //   will not check for '$CLASS(subst)' trick
                    110:        //   will hope that user ain't THAT self-hating person
                    111:        // speed2:
                    112:        //   will not check for '$method_name(subst)' trick
                    113:        //   -same-
                    114: 
1.2       paf       115:        if(!replace) { 
                    116:                // for first call, pass call to last derived VObject
1.9.4.1   paf       117:                if(get_last_derived().put_element(aname, avalue, true))
1.2       paf       118:                        return true;
1.1       paf       119: 
1.2       paf       120:                ffields.put(aname, avalue);
                    121:                return false;
                    122:        }
1.1       paf       123: 
1.2       paf       124:        // replace
1.1       paf       125:        // upwards: copied from VClass::put_element...
                    126: 
                    127:        try {
                    128:                if(fbase && fbase->put_element(aname, avalue, true))
                    129:                        return true; // replaced in base
1.2       paf       130: 
                    131:                return ffields.put_replace(aname, avalue);
1.4       paf       132:        } catch(Exception) { /* allow override parent variables, useful for form descendants */ }
1.1       paf       133: 
1.2       paf       134:        // could not put to any base of last child
                    135:        return false;
1.1       paf       136: }
                    137: 

E-mail: