Annotation of parser3/src/types/pa_vstateless_class.h, revision 1.59

1.6       paf         1: /** @file
                      2:        Parser: stateless class decls.
                      3: 
1.53      paf         4:        Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com)
1.23      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.2       paf         6: */
                      7: 
                      8: #ifndef PA_VSTATELESS_CLASS_H
                      9: #define PA_VSTATELESS_CLASS_H
1.27      paf        10: 
1.59    ! misha      11: static const char * const IDENT_VSTATELESS_CLASS_H="$Date: 2008-06-05 13:26:57 $";
1.43      paf        12: 
                     13: // include
1.2       paf        14: 
1.45      paf        15: #include "pa_pool.h"
1.13      paf        16: #include "pa_hash.h"
1.2       paf        17: #include "pa_vjunction.h"
1.43      paf        18: #include "pa_method.h"
1.49      paf        19: #include "pa_vproperty.h"
1.2       paf        20: 
1.38      paf        21: // defines
                     22: 
                     23: #define CLASS_NAME "CLASS"
1.54      misha      24: #define CLASS_NAMETEXT "CLASS_NAME"
1.38      paf        25: 
                     26: // forwards
                     27: 
1.43      paf        28: class VStateless_class;
1.59    ! misha      29: typedef Array<VStateless_class*> ArrayClass;
1.43      paf        30: 
1.2       paf        31: class Temp_method;
                     32: 
1.6       paf        33: /**
1.30      paf        34:        object' class. stores
                     35:        - base: VClass::base()
                     36:        - methods: VStateless_class::fmethods
1.6       paf        37: 
1.30      paf        38:        @see Method, VStateless_object, Temp_method
1.6       paf        39: */
1.39      paf        40: class VStateless_class: public Value {
1.19      paf        41:        friend class Temp_method;
1.43      paf        42: 
                     43:        const String* fname;
                     44:        mutable const char* fname_cstr;
1.44      paf        45:        Hash<const String::Body, Method*> fmethods;
1.43      paf        46: 
                     47:        bool flocked;
1.57      misha      48:        bool fall_vars_local;
1.43      paf        49: 
                     50: protected:
                     51: 
                     52:        VStateless_class* fbase;
1.58      misha      53:        Method* fdefault_getter;
1.43      paf        54: 
1.2       paf        55: public: // Value
                     56:        
1.43      paf        57:        const char* type() const { return "stateless_class"; }
1.2       paf        58: 
1.5       paf        59:        /// VStateless_class: this
1.43      paf        60:        override VStateless_class *get_class() { return this; }
1.41      paf        61:        /// VStateless_class: fbase
1.43      paf        62:        override Value* base() { return fbase; }
1.58      misha      63:        override Value* get_element(const String& aname, Value& aself, bool alooking_up);
1.55      misha      64:        override Value& as_expr_result(bool /*return_string_as_is=false*/);
1.58      misha      65:        override Value* get_default_getter(Value& aself, const String& aname);
                     66:        override void set_default_getter(Method* amethod);
1.2       paf        67: 
                     68: public: // usage
                     69: 
1.43      paf        70:        VStateless_class(
                     71:                const String* aname=0, 
                     72:                VStateless_class* abase=0):
1.24      paf        73:                fname(aname),
1.43      paf        74:                flocked(false),
1.57      misha      75:                fbase(abase),
1.58      misha      76:                fall_vars_local(false),
                     77:                fdefault_getter(0) {
1.2       paf        78:        }
                     79: 
1.43      paf        80:        void lock() { flocked=true; }
                     81: 
1.24      paf        82:        const String& name() const { 
                     83:                if(!fname) {
                     84:                        if(fbase)
                     85:                                return fbase->name();
                     86: 
1.56      misha      87:                        throw Exception(PARSER_RUNTIME,
1.24      paf        88:                                0,
                     89:                                "getting name of nameless class");
                     90:                }
                     91: 
                     92:                return *fname; 
                     93:        }
1.43      paf        94:        const char* name_cstr() const{
                     95:                if(this) {
                     96:                        if(!fname_cstr) // remembering last calculated, and can't reassign 'fname_cstr'!
                     97:                                fname_cstr=name().cstr();
                     98:                        return fname_cstr;
                     99:                } else
                    100:                        return "<unknown>";
1.24      paf       101:        }
                    102:        void set_name(const String& aname) {
                    103:                fname=&aname; 
                    104:        }
                    105: 
1.43      paf       106:        Method* get_method(const String& aname) const { 
                    107:                return fmethods.get(aname);
1.2       paf       108:        }
                    109: 
1.57      misha     110:        void all_vars_local(){
                    111:                fall_vars_local=true;
                    112:        }
                    113: 
                    114:        bool is_vars_local(){
                    115:                return fall_vars_local;
                    116:        }
                    117: 
1.51      paf       118:        /// virtual for VClass to override to pre-cache property accessors into fields
                    119:        virtual void add_method(const String& name, Method& method);
1.40      paf       120:        
1.2       paf       121:        void add_native_method(
1.43      paf       122:                const char* cstr_name,
1.9       paf       123:                Method::Call_type call_type,
1.43      paf       124:                NativeCodePtr native_code,
1.2       paf       125:                int min_numbered_params_count, int max_numbered_params_count);
                    126:        
1.43      paf       127:        void set_base(VStateless_class* abase) {
1.2       paf       128:                // remember the guy
1.32      paf       129:                fbase=abase;
1.2       paf       130:        }
1.43      paf       131:        VStateless_class* base_class() { return fbase; }
1.2       paf       132: 
1.30      paf       133:        bool derived_from(VStateless_class& vclass) {
1.2       paf       134:                return 
1.30      paf       135:                        fbase==&vclass || 
                    136:                        fbase && fbase->derived_from(vclass);
1.31      paf       137:        }
                    138: 
1.11      paf       139:        //@{
                    140:        /// @name just stubs, real onces defined below the hierarchy
1.43      paf       141:        virtual Value* get_field(const String&) { return 0; }
                    142:        virtual bool replace_field(const String&, Value*) { return false; }
1.21      paf       143:        //@}
                    144: 
                    145:        /// @returns new value for current class, used in classes/ & VClass
1.52      paf       146:        virtual Value* create_new_value(Pool&, HashStringValue& /*afields*/) { return 0; }
1.50      paf       147: 
                    148: protected:
                    149: 
                    150:        void fill_properties(HashStringValue& acache);
1.2       paf       151: 
1.49      paf       152: private:
1.2       paf       153: 
1.49      paf       154:        void put_method(const String& aname, Method* amethod);  
1.2       paf       155: };
                    156: 
1.6       paf       157: ///    Auto-object used for temporarily substituting/removing class method
1.2       paf       158: class Temp_method {
                    159:        VStateless_class& fclass;
                    160:        const String& fname;
1.43      paf       161:        Method* saved_method;
1.2       paf       162: public:
1.43      paf       163:        Temp_method(VStateless_class& aclass, const String& aname, Method* amethod) : 
1.2       paf       164:                fclass(aclass),
                    165:                fname(aname),
                    166:                saved_method(aclass.get_method(aname)) {
                    167:                fclass.put_method(aname, amethod);
                    168:        }
                    169:        ~Temp_method() { 
                    170:                fclass.put_method(fname, saved_method);
                    171:        }
                    172: };
                    173: 
                    174: #endif

E-mail: