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

1.6     ! paf         1: /** @file
        !             2:        Parser: stateless class decls.
        !             3: 
1.2       paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.6     ! paf         5: 
1.2       paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      7: 
1.6     ! paf         8:        $Id: pa_vstateless_class.h,v 1.5 2001/03/19 19:17:47 paf Exp $
1.2       paf         9: */
                     10: 
                     11: #ifndef PA_VSTATELESS_CLASS_H
                     12: #define PA_VSTATELESS_CLASS_H
                     13: 
                     14: #include "pa_valiased.h"
                     15: #include "pa_vhash.h"
                     16: #include "pa_vjunction.h"
                     17: 
1.4       paf        18: #define CLASS_NAME "CLASS"
                     19: #define BASE_NAME "BASE"
1.2       paf        20: 
                     21: class Temp_method;
                     22: 
1.6     ! paf        23: /**
        !            24:        object' class. 
        !            25:        
        !            26:        basically collection of methods.
        !            27: 
        !            28:        some classes are freeze()-ed after their creation so that
        !            29:        malicious users would not alter their method set using some tricks
        !            30:        like
        !            31:        @verbatim
        !            32:                $response.process[@new-resonse-method[]
        !            33:                defined here
        !            34:                ]
        !            35:        @endverbatim
        !            36:        affecting unaware neibours.
        !            37: 
        !            38:        @see VStateless_object, Temp_method
        !            39: */
1.2       paf        40: class VStateless_class : public VAliased {
                     41:        friend Temp_method;
                     42: public: // Value
                     43:        
1.5       paf        44:        /// all: for error reporting after fail(), etc
1.2       paf        45:        const char *type() const { return "stateless_class"; }
                     46: 
1.5       paf        47:        /// VStateless_class: this
                     48:        VStateless_class *get_class() { return this; }
                     49: 
                     50:        /// VStateless_class: $CLASS,$BASE,$method
1.2       paf        51:        Value *get_element(const String& aname);
                     52: 
                     53: public: // usage
                     54: 
1.3       paf        55:        VStateless_class(Pool& apool, VStateless_class *abase=0) : VAliased(apool, *this), 
                     56:                fbase(abase),
1.2       paf        57:                read_only(false),
                     58:                fmethods(apool) {
                     59:        }
                     60: 
                     61:        Method *get_method(const String& name) { 
                     62:                return static_cast<Method *>(fmethods.get(name)); 
                     63:        }
                     64: 
                     65:        // make class read-only
                     66:        //      this blocks put_method  // which could be done with ^process
                     67:        void freeze() { read_only=true; }
                     68: 
                     69:        void add_method(const String& name, Method& method) {
                     70:                put_method(name, &method);
                     71:        }
                     72:        void add_native_method(
                     73:                const char *cstr_name,
                     74:                Native_code_ptr native_code,
                     75:                int min_numbered_params_count, int max_numbered_params_count);
                     76:        
                     77:        void set_base(VStateless_class& abase) {
                     78:                // remember the guy
                     79:                fbase=&abase;
                     80:        }
                     81:        VStateless_class *base() { return fbase; }
                     82: 
                     83:        bool is_or_derived_from(VStateless_class& vclass) {
                     84:                return 
                     85:                        this==&vclass || 
                     86:                        fbase && fbase->is_or_derived_from(vclass);
                     87:        }
                     88: 
                     89:        Junction *get_junction(VAliased& self, const String& name) {
                     90:                if(Method *method=static_cast<Method *>(fmethods.get(name)))
                     91:                        return NEW Junction(pool(), self, this, method, 0,0,0,0);
                     92:                if(fbase)
                     93:                        return fbase->get_junction(self, name);
                     94:                return 0; 
                     95:        }
                     96: 
                     97:        // just stubs, real onces defined below the hierarchy, in 
                     98:        virtual Value *get_field(const String& name) { return 0; }
                     99:        virtual bool replace_field(const String& name, Value *value) { return false; }
                    100: 
                    101: private: // Temp_method
                    102: 
                    103:        void put_method(const String& aname, Method *amethod);
                    104:        
                    105: private:
                    106: 
                    107:        Hash fmethods;
                    108: 
                    109: protected:
                    110: 
                    111:        VStateless_class *fbase;
                    112:        bool read_only;
                    113: 
                    114: };
                    115: 
1.6     ! paf       116: ///    Auto-object used for temporarily substituting/removing class method
1.2       paf       117: class Temp_method {
                    118:        VStateless_class& fclass;
                    119:        const String& fname;
                    120:        Method *saved_method;
                    121: public:
                    122:        Temp_method(VStateless_class& aclass, const String& aname, Method *amethod) : 
                    123:                fclass(aclass),
                    124:                fname(aname),
                    125:                saved_method(aclass.get_method(aname)) {
                    126:                fclass.put_method(aname, amethod);
                    127:        }
                    128:        ~Temp_method() { 
                    129:                fclass.put_method(fname, saved_method);
                    130:        }
                    131: };
                    132: 
                    133: #endif

E-mail: