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

1.1       paf         1: /**    @file
                      2:        Parser: @b object class impl.
                      3: 
1.44      moko        4:        Copyright (c) 2001-2015 Art. Lebedev Studio (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.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.45    ! moko       15: volatile const char * IDENT_PA_VOBJECT_C="$Id: pa_vobject.C,v 1.44 2015/10/26 01:22:02 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.31      moko       22:                                VMethodFrame frame(*method, 0 /*no caller*/, *unconst_this);
1.30      misha      23: 
                     24:                                Value *param;
                     25: 
                     26:                                if(size_t param_count=frame.method_params_count()){
                     27:                                        if(param_count==1){
                     28:                                                param=new VString(*new String(as_something));
                     29:                                                frame.store_params(&param, 1);
                     30:                                        } else
                     31:                                                throw Exception(PARSER_RUNTIME,
                     32:                                                        0,
                     33:                                                        "scalar getter method can't have more then 1 parameter (has %d parameters)", param_count);
                     34:                                } // no need for else frame.empty_params()
                     35: 
1.33      moko       36:                                pa_thread_request().execute_method(frame);
                     37:                                return &frame.result().as_value();
1.26      misha      38:                        }
                     39:        return 0;
                     40: }
1.5       paf        41: 
1.29      misha      42: Value* VObject::as(const char* atype) {
                     43:        return fclass.as(atype) ? this:0;
1.5       paf        44: }
1.1       paf        45: 
1.7       paf        46: bool VObject::is_defined() const {
1.30      misha      47:        if(Value* value=get_scalar_value("def"))
1.40      misha      48:                return value->as_bool();
1.29      misha      49:        return Value::is_defined();
1.7       paf        50: }
1.29      misha      51: 
1.35      moko       52: Value& VObject::as_expr_result() {
1.30      misha      53:        if(Value* value=get_scalar_value("expression"))
1.26      misha      54:                return value->as_expr_result();
1.29      misha      55:        return Value::as_expr_result();
1.7       paf        56: }
1.29      misha      57: 
1.7       paf        58: int VObject::as_int() const {
1.30      misha      59:        if(Value* value=get_scalar_value("int"))
1.26      misha      60:                return value->as_int();
1.29      misha      61:        return Value::as_int();
1.7       paf        62: }
1.29      misha      63: 
1.26      misha      64: double VObject::as_double() const {
1.30      misha      65:        if(Value* value=get_scalar_value("double"))
1.26      misha      66:                return value->as_double();
1.29      misha      67:        return Value::as_double();
1.7       paf        68: }
1.29      misha      69: 
1.7       paf        70: bool VObject::as_bool() const { 
1.30      misha      71:        if(Value* value=get_scalar_value("bool"))
1.26      misha      72:                return value->as_bool();
1.29      misha      73:        return Value::as_bool();
1.7       paf        74: }
1.29      misha      75: 
1.13      paf        76: VFile* VObject::as_vfile(String::Language lang, const Request_charsets *charsets) {
1.30      misha      77:        if(Value* value=get_scalar_value("file"))
1.26      misha      78:                return value->as_vfile(lang, charsets);
1.29      misha      79:        return Value::as_vfile(lang, charsets);
1.7       paf        80: }
                     81: 
1.13      paf        82: HashStringValue* VObject::get_hash() {
1.30      misha      83:        if(Value* value=get_scalar_value("hash"))
1.29      misha      84:                return value->get_hash();
                     85:        return &ffields;
                     86: }
1.7       paf        87: 
                     88: Table *VObject::get_table() {
1.30      misha      89:        if(Value* value=get_scalar_value("table"))
1.29      misha      90:                return value->get_table();
                     91:        return Value::get_table();
1.7       paf        92: }
1.1       paf        93: 
1.29      misha      94: Value* VObject::get_element(const String& aname) {
1.32      moko       95:        // object field
1.13      paf        96:        if(Value* result=ffields.get(aname))
1.8       paf        97:                return result;
                     98: 
1.32      moko       99:        // class method or property, or _object_ default getter
                    100:        return fclass.get_element(*this, aname);
1.1       paf       101: }
1.19      paf       102: 
1.43      moko      103: #ifdef FEATURE_GET_ELEMENT4CALL
                    104: // get_element copy to remove extra virtual call
                    105: Value* VObject::get_element4call(const String& aname) {
                    106:        // object field
                    107:        if(Value* result=ffields.get(aname))
                    108:                return result;
                    109: 
                    110:        // class method or property, or _object_ default getter
                    111:        return fclass.get_element(*this, aname);
                    112: }
                    113: #endif
                    114: 
1.38      moko      115: const VJunction* VObject::put_element(const String& aname, Value* avalue){
1.45    ! moko      116:        // class setter
1.38      moko      117:        if(const VJunction* result=fclass.put_element_replace_only(*this, aname, avalue))
1.45    ! moko      118:                return result;
1.32      moko      119:        
1.39      moko      120:        // object field or default setter, avoiding virtual is_enabled_default_setter call
                    121:        if (state & IS_SETTER_ACTIVE){
1.32      moko      122:                return ffields.put_replaced(aname, avalue) ? 0 : fclass.get_default_setter(*this, aname); 
                    123:        } else {
                    124:                ffields.put(aname, avalue);
                    125:        }
1.29      misha     126:        return 0;
1.1       paf       127: }
1.36      moko      128: 
                    129: const String* VObject::get_json_string(Json_options& options){
                    130:        if(options.default_method){
1.42      moko      131:                return default_method_2_json_string(*options.default_method, options);
1.36      moko      132:        }
1.42      moko      133:        return options.hash_json_string(get_hash());
1.36      moko      134: }

E-mail: