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

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

E-mail: