Annotation of parser3/src/types/pa_vobject.C, revision 1.27
1.1 paf 1: /** @file
2: Parser: @b object class impl.
3:
1.24 paf 4: Copyright (c) 2001-2005 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.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.27 ! misha 15: static const char * const IDENT_VOBJECT_C="$Date: 2008-06-16 12:47:25 $";
1.26 misha 16:
17: Value* VObject::get_scalar_value() const {
18: VObject* unconst_this=const_cast<VObject*>(this);
19: VObject& last_derived=unconst_this->get_last_derived();
20: if(Value* scalar=unconst_this->get_scalar(last_derived))
21: if(Junction* junction=scalar->get_junction())
22: if(const Method *method=junction->method){
23: VMethodFrame frame(*junction, 0/*no caller*/);
24: frame.set_self(last_derived);
25: return &pa_thread_request().execute_method(frame, *method).as_value();
26: }
27:
28: return 0;
29: }
1.5 paf 30:
1.13 paf 31: Value* VObject::as(const char* atype, bool looking_up) {
1.5 paf 32: if(!looking_up)
1.10 paf 33: return get_last_derived().as(atype, true/*the only user*/); // figure out from last_derivate upwards
1.5 paf 34:
35: // is it me?
1.6 paf 36: if(Value *result=Value::as(atype, false))
37: return result;
1.5 paf 38:
39: // is it my base?
40: if(fbase) {
1.6 paf 41: if(Value *result=fbase->as(atype, true))
42: return result;
1.5 paf 43: }
44:
45: // neither
1.6 paf 46: return 0;
1.5 paf 47: }
1.1 paf 48:
1.7 paf 49: /// VObject: from possible parent, if any
50: bool VObject::is_defined() const {
1.26 misha 51: if(Value* value=get_scalar_value())
52: return value->is_defined();
1.7 paf 53: return fbase?fbase->is_defined():Value::is_defined();
54: }
55: /// VObject: from possible parent, if any
1.26 misha 56: Value& VObject::as_expr_result(bool) {
57: if(Value* value=get_scalar_value())
58: return value->as_expr_result();
1.7 paf 59: return fbase?fbase->as_expr_result():Value::as_expr_result();
60: }
61: /// VObject: from possible parent, if any
62: int VObject::as_int() const {
1.26 misha 63: if(Value* value=get_scalar_value())
64: return value->as_int();
1.7 paf 65: return fbase?fbase->as_int():Value::as_int();
66: }
67: /// VObject: from possible parent, if any
1.26 misha 68: double VObject::as_double() const {
69: if(Value* value=get_scalar_value())
70: return value->as_double();
1.7 paf 71: return fbase?fbase->as_double():Value::as_double();
72: }
73: /// VObject: from possible parent, if any
74: bool VObject::as_bool() const {
1.26 misha 75: if(Value* value=get_scalar_value())
76: return value->as_bool();
1.7 paf 77: return fbase?fbase->as_bool():Value::as_bool();
78: }
79: /// VObject: from possible parent, if any
1.13 paf 80: VFile* VObject::as_vfile(String::Language lang, const Request_charsets *charsets) {
1.26 misha 81: if(Value* value=get_scalar_value())
82: return value->as_vfile(lang, charsets);
1.13 paf 83: return fbase?fbase->as_vfile(lang, charsets):
84: Value::as_vfile(lang, charsets);
1.7 paf 85: }
86:
87: /// VObject: from possible parent, if any
1.13 paf 88: HashStringValue* VObject::get_hash() {
89: if(Value* vhash=get_last_derived().as(VHASH_TYPE, false))
90: return vhash->get_hash();
1.7 paf 91:
92: return 0;
93: }
94: /// VObject: from possible 'table' parent
95: Table *VObject::get_table() {
1.10 paf 96: if(Value *vtable=get_last_derived().as(VTABLE_TYPE, false))
1.7 paf 97: return vtable->get_table();
98:
99: return 0;
100: }
1.1 paf 101:
102: /// VObject: (field)=value;(CLASS)=vclass;(method)=method_ref
1.25 misha 103: Value* VObject::get_element(const String& aname, Value&, bool alooking_up) {
1.8 paf 104: // simple things first: $field=ffields.field
1.13 paf 105: if(Value* result=ffields.get(aname))
1.8 paf 106: return result;
107:
1.1 paf 108: // gets element from last_derivate upwards
1.25 misha 109: if(alooking_up) {
1.27 ! misha 110: VObject& last_derived=get_last_derived();
1.1 paf 111: // $CLASS
112: if(aname==CLASS_NAME)
1.27 ! misha 113: return last_derived.get_class();
1.25 misha 114: // $CLASS_NAME
115: if(aname==CLASS_NAMETEXT)
1.27 ! misha 116: return new VString(last_derived.get_class()->name());
1.22 paf 117: // $virtual_method $virtual_property
1.13 paf 118: if(Value* result=last_derived.stateless_object__get_element(aname, last_derived))
1.1 paf 119: return result;
1.25 misha 120:
121: if(Value* result=last_derived.get_default_getter(last_derived, aname))
122: return result;
1.1 paf 123: }
124:
1.21 paf 125: return 0;
126: }
127:
1.23 paf 128: const VJunction* VObject::prevent_append_if_exists_in_static_or_base(Value* value, Prevent_append_if_exists_in_static_or_base_info* info) {
1.21 paf 129: // $virtual_property, any this/bases $static_property
130: VObject& last_derived=info->_this->get_last_derived();
1.23 paf 131: if(const VJunction* result=last_derived.stateless_object__put_element(last_derived, *info->name, value))
1.21 paf 132: return result; // replaced in any(derivate or base) statics fields/properties
1.1 paf 133:
134: return 0;
135: }
1.19 paf 136:
137: /// VObject: (field/property)=value
1.25 misha 138: const VJunction* VObject::put_element(Value& /*aself*/, const String& aname, Value* avalue, bool /*areplace*/){
1.21 paf 139: Prevent_append_if_exists_in_static_or_base_info info={this, &aname};
1.23 paf 140: return ffields.replace_maybe_append<const VJunction*>(aname, avalue,
1.21 paf 141: prevent_append_if_exists_in_static_or_base,
142: &info);
1.1 paf 143: }
E-mail: