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

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.60    ! misha      11: static const char * const IDENT_VSTATELESS_CLASS_H="$Date: 2008-06-10 14:04:14 $";
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.60    ! misha      53:        Method* fscalar;
1.58      misha      54:        Method* fdefault_getter;
1.43      paf        55: 
1.2       paf        56: public: // Value
                     57:        
1.43      paf        58:        const char* type() const { return "stateless_class"; }
1.2       paf        59: 
1.5       paf        60:        /// VStateless_class: this
1.43      paf        61:        override VStateless_class *get_class() { return this; }
1.41      paf        62:        /// VStateless_class: fbase
1.43      paf        63:        override Value* base() { return fbase; }
1.58      misha      64:        override Value* get_element(const String& aname, Value& aself, bool alooking_up);
1.55      misha      65:        override Value& as_expr_result(bool /*return_string_as_is=false*/);
1.60    ! misha      66: 
1.58      misha      67:        override Value* get_default_getter(Value& aself, const String& aname);
                     68:        override void set_default_getter(Method* amethod);
1.2       paf        69: 
1.60    ! misha      70:        override Value* get_scalar(Value& aself);
        !            71:        override void set_scalar(Method* amethod);
        !            72: 
1.2       paf        73: public: // usage
                     74: 
1.43      paf        75:        VStateless_class(
                     76:                const String* aname=0, 
                     77:                VStateless_class* abase=0):
1.24      paf        78:                fname(aname),
1.43      paf        79:                flocked(false),
1.57      misha      80:                fbase(abase),
1.58      misha      81:                fall_vars_local(false),
1.60    ! misha      82:                fscalar(0),
1.58      misha      83:                fdefault_getter(0) {
1.2       paf        84:        }
                     85: 
1.43      paf        86:        void lock() { flocked=true; }
                     87: 
1.24      paf        88:        const String& name() const { 
                     89:                if(!fname) {
                     90:                        if(fbase)
                     91:                                return fbase->name();
                     92: 
1.56      misha      93:                        throw Exception(PARSER_RUNTIME,
1.24      paf        94:                                0,
                     95:                                "getting name of nameless class");
                     96:                }
                     97: 
                     98:                return *fname; 
                     99:        }
1.43      paf       100:        const char* name_cstr() const{
                    101:                if(this) {
                    102:                        if(!fname_cstr) // remembering last calculated, and can't reassign 'fname_cstr'!
                    103:                                fname_cstr=name().cstr();
                    104:                        return fname_cstr;
                    105:                } else
                    106:                        return "<unknown>";
1.24      paf       107:        }
                    108:        void set_name(const String& aname) {
                    109:                fname=&aname; 
                    110:        }
                    111: 
1.43      paf       112:        Method* get_method(const String& aname) const { 
                    113:                return fmethods.get(aname);
1.2       paf       114:        }
                    115: 
1.57      misha     116:        void all_vars_local(){
                    117:                fall_vars_local=true;
                    118:        }
                    119: 
                    120:        bool is_vars_local(){
                    121:                return fall_vars_local;
                    122:        }
                    123: 
1.51      paf       124:        /// virtual for VClass to override to pre-cache property accessors into fields
                    125:        virtual void add_method(const String& name, Method& method);
1.40      paf       126:        
1.2       paf       127:        void add_native_method(
1.43      paf       128:                const char* cstr_name,
1.9       paf       129:                Method::Call_type call_type,
1.43      paf       130:                NativeCodePtr native_code,
1.2       paf       131:                int min_numbered_params_count, int max_numbered_params_count);
                    132:        
1.43      paf       133:        void set_base(VStateless_class* abase) {
1.2       paf       134:                // remember the guy
1.32      paf       135:                fbase=abase;
1.2       paf       136:        }
1.43      paf       137:        VStateless_class* base_class() { return fbase; }
1.2       paf       138: 
1.30      paf       139:        bool derived_from(VStateless_class& vclass) {
1.2       paf       140:                return 
1.30      paf       141:                        fbase==&vclass || 
                    142:                        fbase && fbase->derived_from(vclass);
1.31      paf       143:        }
                    144: 
1.11      paf       145:        //@{
                    146:        /// @name just stubs, real onces defined below the hierarchy
1.43      paf       147:        virtual Value* get_field(const String&) { return 0; }
                    148:        virtual bool replace_field(const String&, Value*) { return false; }
1.21      paf       149:        //@}
                    150: 
                    151:        /// @returns new value for current class, used in classes/ & VClass
1.52      paf       152:        virtual Value* create_new_value(Pool&, HashStringValue& /*afields*/) { return 0; }
1.50      paf       153: 
                    154: protected:
                    155: 
                    156:        void fill_properties(HashStringValue& acache);
1.2       paf       157: 
1.49      paf       158: private:
1.2       paf       159: 
1.49      paf       160:        void put_method(const String& aname, Method* amethod);  
1.2       paf       161: };
                    162: 
1.6       paf       163: ///    Auto-object used for temporarily substituting/removing class method
1.2       paf       164: class Temp_method {
                    165:        VStateless_class& fclass;
                    166:        const String& fname;
1.43      paf       167:        Method* saved_method;
1.2       paf       168: public:
1.43      paf       169:        Temp_method(VStateless_class& aclass, const String& aname, Method* amethod) : 
1.2       paf       170:                fclass(aclass),
                    171:                fname(aname),
                    172:                saved_method(aclass.get_method(aname)) {
                    173:                fclass.put_method(aname, amethod);
                    174:        }
                    175:        ~Temp_method() { 
                    176:                fclass.put_method(fname, saved_method);
                    177:        }
                    178: };
                    179: 
                    180: #endif

E-mail: