Annotation of parser3/src/types/pa_vstateless_class.C, revision 1.47

1.8       paf         1: /**    @file
                      2:        Parser: stateless class.
                      3: 
1.47    ! moko        4:        Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.13      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\
1.14      paf         6: */
1.8       paf         7: 
1.2       paf         8: #include "pa_vstateless_class.h"
1.29      misha       9: #include "pa_vstring.h"
1.30      misha      10: #include "pa_vbool.h"
1.45      moko       11: #include "pa_request.h"
1.30      misha      12: 
1.47    ! moko       13: volatile const char * IDENT_PA_VSTATELESS_CLASS_C="$Id: 2010-08-11 16:21:52 $" IDENT_PA_VSTATELESS_CLASS_H IDENT_PA_METHOD_H;
        !            14: 
1.41      misha      15: /// globals
                     16: const String class_name(CLASS_NAME), class_nametext(CLASS_NAMETEXT);
                     17: 
1.30      misha      18: override Value& VStateless_class::as_expr_result(bool /*return_string_as_is=false*/) {
1.35      misha      19:        return VBool::get(as_bool());
1.30      misha      20: }
1.17      paf        21: 
1.43      misha      22: /// @TODO why?! request must be different ptr from global [used in VStateless_class.set_method]
                     23: void VStateless_class::set_method(const String& aname, Method* amethod) {
1.20      paf        24:        if(flocked)
1.31      misha      25:                throw Exception(PARSER_RUNTIME,
1.42      misha      26:                        &aname,
1.17      paf        27:                        "can not add method to system class (maybe you have forgotten .CLASS in ^process[$caller.CLASS]{...}?)");
1.20      paf        28: 
1.43      misha      29:        if(fderived.count()) {
                     30:                Method *omethod=fmethods.get(aname);
                     31:                Array_iterator<VStateless_class *> i(fderived);
                     32:                while(i.has_next()) {
                     33:                        VStateless_class *c=i.next();
                     34:                        if(c->fmethods.get(aname)==omethod)
1.44      misha      35:                                c->real_set_method(aname, amethod);
1.43      misha      36:                }
                     37:        }
1.44      misha      38:        real_set_method(aname, amethod); 
                     39: }
                     40: 
                     41: void VStateless_class::real_set_method(const String& aname, Method* amethod) {
                     42:        fmethods.put(aname, amethod);
1.17      paf        43: }
1.2       paf        44: 
                     45: void VStateless_class::add_native_method(
1.20      paf        46:        const char* cstr_name,
1.7       paf        47:        Method::Call_type call_type,
1.20      paf        48:        NativeCodePtr native_code,
1.38      misha      49:        int min_numbered_params_count, 
1.40      misha      50:        int max_numbered_params_count,
                     51:        Method::Call_optimization
                     52: #ifdef OPTIMIZE_CALL
                     53:                call_optimization
                     54: #endif
                     55:        ) {
1.2       paf        56: 
1.43      misha      57:        Method* method=new Method(
1.7       paf        58:                call_type,
1.2       paf        59:                min_numbered_params_count, max_numbered_params_count,
                     60:                0/*params_names*/, 0/*locals_names*/,
1.40      misha      61:                0/*parser_code*/, native_code, false/*all_vars_local*/
                     62: #ifdef OPTIMIZE_RESULT
                     63:                , Method::RO_USE_WCONTEXT
                     64: #endif
                     65: #ifdef OPTIMIZE_CALL
                     66:                , call_optimization
                     67: #endif
                     68:                );
1.39      misha      69: 
1.43      misha      70:        set_method(*new String(cstr_name), method);
1.16      paf        71: }
                     72: 
1.30      misha      73: /// VStateless_class: $CLASS, $CLASS_NAME, $method
1.42      misha      74: Value* VStateless_class::get_element(Value& aself, const String& aname) {
1.16      paf        75:        // $CLASS
1.41      misha      76:        if(aname==class_name)
1.16      paf        77:                return this;
1.36      misha      78: 
1.29      misha      79:        // $CLASS_NAME
1.41      misha      80:        if(aname==class_nametext)
1.42      misha      81:                return new VString(name());
1.36      misha      82: 
1.16      paf        83:        // $method=junction(self+class+method)
1.36      misha      84:        if(Method* method=get_method(aname)){
                     85:                if(!method->junction_template)
                     86:                        return method->junction_template=new VJunction(aself, method);
1.37      misha      87:                return method->junction_template->get(aself);
1.36      misha      88:        }
1.18      paf        89: 
1.42      misha      90:        return 0;
                     91: }
                     92: 
                     93: Value* VStateless_class::get_scalar(Value& aself){
                     94:        if(fscalar)
                     95:                return new VJunction(aself, fscalar, true /*getter*/);
1.16      paf        96:        return 0;
1.23      paf        97: }
                     98: 
1.42      misha      99: void VStateless_class::set_scalar(Method* amethod){
                    100:        fscalar=amethod;
1.2       paf       101: }
1.32      misha     102: 
                    103: Value* VStateless_class::get_default_getter(Value& aself, const String& aname){
1.46      moko      104:        if(fdefault_getter && aself.is_enabled_default_getter())
1.32      misha     105:                return new VJunction(aself, fdefault_getter, true /*getter*/, (String*)&aname);
                    106:        return 0;
                    107: }
                    108: 
                    109: void VStateless_class::set_default_getter(Method* amethod){
                    110:        fdefault_getter=amethod;
                    111: }
                    112: 
1.46      moko      113: bool VStateless_class::has_default_getter(){
                    114:        return fdefault_getter != NULL;
                    115: }
                    116: 
                    117: VJunction* VStateless_class::get_default_setter(Value& aself, const String& aname){
                    118:        if(fdefault_setter)
                    119:                return new VJunction(aself, fdefault_setter, false /*setter*/, (String*)&aname);
                    120:        return 0;
                    121: }
                    122: 
                    123: void VStateless_class::set_default_setter(Method* amethod){
                    124:        fdefault_setter=amethod;
                    125: }
                    126: 
                    127: bool VStateless_class::has_default_setter(){
                    128:        return fdefault_setter != NULL;
                    129: }
                    130: 
1.42      misha     131: void VStateless_class::set_base(VStateless_class* abase){
                    132:        if(abase){
                    133:                fbase=abase;
                    134:                fbase->add_derived(*this);
                    135: 
1.45      moko      136:                bool no_auto = fmethods.get(auto_method_name) == NULL;
1.42      misha     137:                // we assume there is no derivatives at this point
                    138:                fmethods.merge_dont_replace(abase->fmethods);
1.45      moko      139:                // we don't want to inherit @auto (issue #75)
                    140:                if (no_auto) fmethods.remove(auto_method_name);
1.42      misha     141: 
                    142:                if(fbase->fscalar && !fscalar)
                    143:                        fscalar=fbase->fscalar;
                    144:                if(fbase->fdefault_getter && !fdefault_getter)
                    145:                        fdefault_getter=fbase->fdefault_getter;
1.46      moko      146:                if(fbase->fdefault_setter && !fdefault_setter)
                    147:                        fdefault_setter=fbase->fdefault_setter;
1.42      misha     148:        }
1.33      misha     149: }
                    150: 
1.42      misha     151: void VStateless_class::add_derived(VStateless_class &aclass){
                    152:        fderived+=&aclass;
                    153:        if (fbase)
                    154:                fbase->add_derived(aclass);
1.34      misha     155: }

E-mail: