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

1.1       paf         1: /**    @file
                      2:        Parser: @b object class impl.
                      3: 
1.51    ! moko        4:        Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
        !             5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1       paf         6: */
                      7: 
                      8: #include "pa_vobject.h"
1.7       paf         9: #include "pa_vhash.h"
                     10: #include "pa_vtable.h"
1.25      misha      11: #include "pa_vstring.h"
1.26      misha      12: #include "pa_vmethod_frame.h"
                     13: #include "pa_request.h"
1.1       paf        14: 
1.51    ! moko       15: volatile const char * IDENT_PA_VOBJECT_C="$Id: pa_vobject.C,v 1.50 2020/12/15 17:10:42 moko Exp $" IDENT_PA_VOBJECT_H;
1.26      misha      16: 
1.37      moko       17: Value* VObject::get_scalar_value(const char* as_something) const {
1.26      misha      18:        VObject* unconst_this=const_cast<VObject*>(this);
1.29      misha      19:        if(Value* scalar=fclass.get_scalar(*unconst_this))
1.26      misha      20:                if(Junction* junction=scalar->get_junction())
                     21:                        if(const Method *method=junction->method){
1.47      moko       22:                                if(method->params_count>1)
                     23:                                        throw Exception(PARSER_RUNTIME, 0, "scalar getter method can't have more then 1 parameter (has %d parameters)", method->params_count);
                     24:                                METHOD_FRAME_ACTION(*method, 0 /*no caller*/, *unconst_this, {
                     25:                                        Value *param;
                     26:                                        if(method->params_count==1){
1.30      misha      27:                                                param=new VString(*new String(as_something));
                     28:                                                frame.store_params(&param, 1);
1.47      moko       29:                                        } /* no need for else frame.empty_params() */
                     30:                                        pa_thread_request().call(frame);
                     31:                                        return &frame.result();
                     32:                                });
1.26      misha      33:                        }
                     34:        return 0;
                     35: }
1.5       paf        36: 
1.29      misha      37: Value* VObject::as(const char* atype) {
                     38:        return fclass.as(atype) ? this:0;
1.5       paf        39: }
1.1       paf        40: 
1.7       paf        41: bool VObject::is_defined() const {
1.30      misha      42:        if(Value* value=get_scalar_value("def"))
1.40      misha      43:                return value->as_bool();
1.29      misha      44:        return Value::is_defined();
1.7       paf        45: }
1.29      misha      46: 
1.35      moko       47: Value& VObject::as_expr_result() {
1.30      misha      48:        if(Value* value=get_scalar_value("expression"))
1.26      misha      49:                return value->as_expr_result();
1.29      misha      50:        return Value::as_expr_result();
1.7       paf        51: }
1.29      misha      52: 
1.7       paf        53: int VObject::as_int() const {
1.30      misha      54:        if(Value* value=get_scalar_value("int"))
1.26      misha      55:                return value->as_int();
1.29      misha      56:        return Value::as_int();
1.7       paf        57: }
1.29      misha      58: 
1.26      misha      59: double VObject::as_double() const {
1.30      misha      60:        if(Value* value=get_scalar_value("double"))
1.26      misha      61:                return value->as_double();
1.29      misha      62:        return Value::as_double();
1.7       paf        63: }
1.29      misha      64: 
1.7       paf        65: bool VObject::as_bool() const { 
1.30      misha      66:        if(Value* value=get_scalar_value("bool"))
1.26      misha      67:                return value->as_bool();
1.29      misha      68:        return Value::as_bool();
1.7       paf        69: }
1.29      misha      70: 
1.13      paf        71: VFile* VObject::as_vfile(String::Language lang, const Request_charsets *charsets) {
1.30      misha      72:        if(Value* value=get_scalar_value("file"))
1.26      misha      73:                return value->as_vfile(lang, charsets);
1.29      misha      74:        return Value::as_vfile(lang, charsets);
1.7       paf        75: }
                     76: 
1.13      paf        77: HashStringValue* VObject::get_hash() {
1.30      misha      78:        if(Value* value=get_scalar_value("hash"))
1.29      misha      79:                return value->get_hash();
                     80:        return &ffields;
                     81: }
1.7       paf        82: 
                     83: Table *VObject::get_table() {
1.30      misha      84:        if(Value* value=get_scalar_value("table"))
1.29      misha      85:                return value->get_table();
                     86:        return Value::get_table();
1.7       paf        87: }
1.1       paf        88: 
1.29      misha      89: Value* VObject::get_element(const String& aname) {
1.32      moko       90:        // object field
1.13      paf        91:        if(Value* result=ffields.get(aname))
1.8       paf        92:                return result;
                     93: 
1.32      moko       94:        // class method or property, or _object_ default getter
                     95:        return fclass.get_element(*this, aname);
1.1       paf        96: }
1.19      paf        97: 
1.43      moko       98: #ifdef FEATURE_GET_ELEMENT4CALL
                     99: // get_element copy to remove extra virtual call
                    100: Value* VObject::get_element4call(const String& aname) {
                    101:        // object field
                    102:        if(Value* result=ffields.get(aname))
                    103:                return result;
                    104: 
                    105:        // class method or property, or _object_ default getter
                    106:        return fclass.get_element(*this, aname);
                    107: }
                    108: #endif
                    109: 
1.38      moko      110: const VJunction* VObject::put_element(const String& aname, Value* avalue){
1.45      moko      111:        // class setter
1.38      moko      112:        if(const VJunction* result=fclass.put_element_replace_only(*this, aname, avalue))
1.49      moko      113:                return result == PUT_ELEMENT_REPLACED_FIELD ? 0 : result;
1.32      moko      114:        
1.39      moko      115:        // object field or default setter, avoiding virtual is_enabled_default_setter call
                    116:        if (state & IS_SETTER_ACTIVE){
1.32      moko      117:                return ffields.put_replaced(aname, avalue) ? 0 : fclass.get_default_setter(*this, aname); 
                    118:        } else {
                    119:                ffields.put(aname, avalue);
                    120:        }
1.29      misha     121:        return 0;
1.1       paf       122: }
1.36      moko      123: 
                    124: const String* VObject::get_json_string(Json_options& options){
                    125:        if(options.default_method){
1.42      moko      126:                return default_method_2_json_string(*options.default_method, options);
1.36      moko      127:        }
1.42      moko      128:        return options.hash_json_string(get_hash());
1.36      moko      129: }

E-mail: