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

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

E-mail: