Annotation of parser3/src/types/pa_value.h, revision 1.100

1.22      paf         1: /** @file
1.24      paf         2:        Parser: Value, Method, Junction class decls.
                      3: 
1.78      paf         4:        Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.79      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: */
                      7: 
                      8: #ifndef PA_VALUE_H
                      9: #define PA_VALUE_H
1.87      paf        10: 
1.100   ! paf        11: static const char* IDENT_VALUE_H="$Date: 2002/08/29 12:22:48 $";
1.1       paf        12: 
                     13: #include "pa_pool.h"
                     14: #include "pa_string.h"
                     15: #include "pa_array.h"
                     16: #include "pa_exception.h"
1.13      paf        17: #include "pa_globals.h"
1.1       paf        18: 
1.99      paf        19: // forwards
                     20: 
1.9       paf        21: class VStateless_class;
1.1       paf        22: class WContext;
                     23: class Request;
1.35      paf        24: class Table;
1.17      paf        25: class Junction;
                     26: class Method;
1.20      paf        27: class Hash;
1.42      paf        28: class VFile;
1.64      parser     29: class MethodParams;
1.93      paf        30: class VObject;
1.99      paf        31: class VMethodFrame;
1.1       paf        32: 
1.31      paf        33: ///    grandfather of all @a values in @b Parser
1.1       paf        34: class Value : public Pooled {
                     35: public: // Value
                     36: 
1.53      paf        37:        /// all: value type, used for error reporting and 'is' expression operator
1.1       paf        38:        virtual const char *type() const =0;
1.89      paf        39: 
1.91      paf        40:        /** remember derived class instance 
                     41:            - VObject: the only client
                     42:        */
1.93      paf        43:        virtual VObject *set_derived(VObject * /*aderived*/) { return 0; }
1.91      paf        44: 
1.89      paf        45:        /**
1.95      paf        46:                all except VObject/VClass: this if @atype eq type()
                     47:                VObject/VClass: can locate parent class by it's type
1.89      paf        48:        */
1.96      paf        49:        virtual Value *as(const char *atype, bool looking_up) {
                     50:                return atype && strcmp(type(), atype)==0?this:0;
1.89      paf        51:        }
1.97      paf        52:        /// type checking helper, uses Value::as
                     53:        bool is(const char *atype) { return as(atype, false)!=0; }
1.59      parser     54:        
1.23      paf        55:        /** is this value defined?
1.22      paf        56:                @return for
1.63      parser     57:                - VVoid: false
1.69      parser     58:                - VString: eq ''=false, ne ''=true
1.72      parser     59:                - VHash: count!=0
1.71      parser     60:                - VTable: count!=0
1.22      paf        61:                - others: true
                     62:        */
1.38      paf        63:        virtual bool is_defined() const { return true; }
1.59      parser     64:        
1.35      paf        65:        /** is this value string?
                     66:                @return for
                     67:                - VString: true
                     68:                - others: false
                     69:        */
1.38      paf        70:        virtual bool is_string() const { return false; }
1.59      parser     71:        
1.23      paf        72:        /** what's the meaning of this value in context of expression?
1.22      paf        73:                @return for
1.34      paf        74:                - VString: fstring as VDouble or this depending on return_string_as_is
1.70      parser     75:                - VBool: clone
                     76:                - VDouble: clone
                     77:                - VInt: clone
1.63      parser     78:                - VVoid: this
1.27      paf        79:                - VFile: this
1.45      paf        80:                - VImage: this
1.64      parser     81:                - VDate: ftime -> float days
1.72      parser     82:                - VTable: count
                     83:                - VHash: count
1.22      paf        84:        */
1.75      parser     85:        virtual Value *as_expr_result(bool /*return_string_as_is*/=false) { 
1.34      paf        86:                bark("(%s) can not be used in expression"); return 0; 
                     87:        }
1.59      parser     88:        
1.23      paf        89:        /** extract Hash
1.22      paf        90:                @return for
1.25      paf        91:                - VHash: fhash
                     92:                - VResponse: ffields
1.22      paf        93:        */
1.76      parser     94:        virtual Hash *get_hash(const String * /*source*/) { return 0; }
1.59      parser     95:        
1.23      paf        96:        /** extract const String
1.22      paf        97:                @return for
1.25      paf        98:                - VString: value
1.63      parser     99:                - VVoid: ""
1.25      paf       100:                - VDouble: value
1.66      parser    101:                - VInt: value
1.25      paf       102:                - VBool: must be 0: so in ^if(1>2) it would'nt become "FALSE" string which is 'true'
1.22      paf       103:                - others: 0
                    104:                - WContext: accumulated fstring
                    105:        */
1.1       paf       106:        virtual const String *get_string() { return 0; }
1.59      parser    107:        
1.23      paf       108:        /** extract double
1.22      paf       109:                @return for
1.25      paf       110:                - VString: value
                    111:                - VDouble: value
1.59      parser    112:                - VInt: value
1.25      paf       113:                - VBool: value
1.63      parser    114:                - VVoid: 0
1.64      parser    115:                - VDate: ftime -> float days
1.22      paf       116:        */
1.72      parser    117:        virtual double as_double() const { bark("(%s) does not have numerical (double) value"); return 0; }
1.59      parser    118:        
                    119:        /**     extract integer
                    120:                - VString: value
                    121:                - VDouble: value
                    122:                - VInt: value
                    123:                - VBool: value
1.63      parser    124:                - VVoid: 0
1.73      parser    125:                - VTable: count
                    126:                - VHash: count
1.59      parser    127:        */
1.72      parser    128:        virtual int as_int () const { bark("(%s) does not have numerical (int) value"); return 0; }
1.59      parser    129: 
1.23      paf       130:        /** extract bool
1.22      paf       131:                @return for
1.63      parser    132:                - VVoid: false
1.25      paf       133:                - VBool: value
                    134:                - VInt: 0 or !0
                    135:                - VDouble: 0 or !0
1.27      paf       136:                - VFile: true
1.64      parser    137:                - VDate: 0 or !0
1.73      parser    138:                - VTable: count
                    139:                - VHash: count
1.22      paf       140:        */
1.72      parser    141:        virtual bool as_bool() const { bark("(%s) does not have logical value"); return 0; }
1.59      parser    142:        
1.42      paf       143:        /** extract file
                    144:                @return for
                    145:                - VFile: this
                    146:                - VString: vfile
1.45      paf       147:                - VImage: true
1.42      paf       148:        */
1.75      parser    149:        virtual VFile *as_vfile(String::Untaint_lang /*lang*/=String::UL_UNSPECIFIED,
                    150:                bool /*origins_mode*/=false) { 
1.42      paf       151:                bark("(%s) does not have file value"); return 0; 
                    152:        }
1.59      parser    153:        
1.23      paf       154:        /** extract Junction
1.22      paf       155:                @return for
                    156:                - junction: itself
                    157:        */
1.1       paf       158:        virtual Junction *get_junction() { return 0; }
1.59      parser    159:        
1.93      paf       160:        /** extract base object of Value
                    161:                @return for
                    162:                - VObject: fbase
                    163:        */
                    164:        virtual Value *base_object() { bark("(%s) has no base object"); return 0; }
                    165:        
1.23      paf       166:        /** extract Value element
1.93      paf       167:                @a self =0 means =this
1.22      paf       168:                @return for
1.25      paf       169:                - VHash: (key)=value
1.29      paf       170:                - VStateless_class: +$method
                    171:                - VStateless_object: +$method
1.58      paf       172:                - VClass: (field)=STATIC value;(method)=method_ref with self=object_class
1.51      paf       173:                - VCodeFrame: wcontext_transparent
                    174:                - VMethodFrame: my or self_transparent
1.52      paf       175:                - VTable: columns,methods
1.28      paf       176:                - VEnv: field
1.67      parser    177:                - VForm: CLASS,method,field
1.45      paf       178:                - VString: $method
1.30      paf       179:                - VRequest: fields
1.45      paf       180:                - VResponse: method,fields
1.28      paf       181:                - VCookie: field
1.45      paf       182:                - VFile: method,field
1.67      parser    183:                - VDate: CLASS,method,field
1.26      paf       184:                */
1.95      paf       185:        virtual Value *get_element(const String& /*aname*/, Value * /*aself*/, bool /*looking_up*/) { bark("(%s) has no elements"); return 0; }
1.92      paf       186: 
1.31      paf       187:        /** store Value element under @a name
1.22      paf       188:                @return for
1.25      paf       189:                - VHash: (key)=value
1.67      parser    190:                - VStateless_object: (CLASS)=vclass;(method)=method_ref
1.26      paf       191:                - VStateless_class: (field)=value - static values only
                    192:                - VStateless_object: (field)=value
1.51      paf       193:                - VCodeFrame: wcontext_transparent
                    194:                - VMethodFrame: my or self_transparent
1.25      paf       195:                - VResponse: (attribute)=value
                    196:                - VCookie: field
1.22      paf       197:        */
1.93      paf       198:        virtual bool put_element(const String& name, Value * /*value*/, bool /*replace*/) { 
1.65      parser    199:                // to prevent modification of system classes,
                    200:                // created at system startup, and not having exception
                    201:                // handler installed, we neet to bark using request.pool
                    202:                bark("(%s) does not accept elements", 
                    203:                        "element can not be stored to %s", &name); 
1.93      paf       204:                return false;
1.65      parser    205:        }
1.59      parser    206:        
1.23      paf       207:        /** extract VStateless_class
1.22      paf       208:                @return for
1.85      paf       209:                - VX: x_class
1.25      paf       210:                - VStateless_class: this
1.92      paf       211:                - VObject: fclass
1.26      paf       212:                - WContext: none yet | transparent
1.85      paf       213:                these are methodless classes:
                    214:                - VBool: 0
                    215:                - VJunction: 0
                    216:                - VEnv: 0
                    217:                - VRequest: 0
                    218:                - VCookie: 0
1.22      paf       219:        */
1.85      paf       220:        virtual VStateless_class *get_class()=0;
1.32      paf       221: 
                    222:        /** extract VTable
                    223:                @return for
1.35      paf       224:                - VTable: ftable
1.97      paf       225:                - VObject: from possible 'table' parent
1.32      paf       226:        */
1.35      paf       227:        virtual Table *get_table() { return 0; }
1.1       paf       228: 
                    229: public: // usage
                    230: 
1.84      paf       231:        Value(Pool& apool) : Pooled(apool) {
1.68      parser    232:        }
1.1       paf       233: 
1.26      paf       234:        /// @return sure String. if it doesn't have string value barks
1.1       paf       235:        const String& as_string() {
                    236:                const String *result=get_string(); 
                    237:                if(!result)
1.35      paf       238:                        bark("(%s) has no string representation");
1.6       paf       239: 
1.1       paf       240:                return *result;
                    241:        }
                    242: 
1.6       paf       243: protected: 
1.1       paf       244: 
1.22      paf       245:        /// throws exception specifying bark-reason and name() type() of problematic value
1.65      parser    246:        void bark(char *reason, 
                    247:                const char *alt_reason=0, const String *problem_source=0) const {
1.80      paf       248:                throw Exception("parser.runtime",
1.84      paf       249:                        problem_source,
                    250:                        alt_reason?alt_reason:reason, type());
1.1       paf       251:        }
                    252: 
1.17      paf       253: };
                    254: 
1.23      paf       255: /** \b junction is some code joined with context of it's evaluation.
1.22      paf       256: 
                    257:        there are code-junctions and method-junctions
                    258:        - code-junctions are used when some parameter passed in cury brackets
                    259:        - method-junctions used in ^method[] calls or $method references
1.99      paf       260: 
                    261:        Junctions register themselves in method_frame [if any] for consequent invalidation.
                    262:        This prevents evaluation of junctions in outdated context
                    263: 
                    264:        To stop situations like this:
                    265: @code
                    266:        @main[]
                    267:        ^method1[]
                    268:        ^method2[]
                    269: 
                    270:        @method1[]
                    271:        $junction{
                    272:                some code
                    273:        }
                    274: 
                    275:        @method2[]
                    276:        ^junction[]
                    277: @endcode
                    278: 
                    279:        On scope exit (VMethodFrame::~VMethodFrame()) got cleaned - Junction::method_frame becomes 0,
                    280:        which later in Request::process triggers exception
1.22      paf       281: */
1.17      paf       282: class Junction : public Pooled {
                    283: public:
                    284: 
                    285:        Junction(Pool& apool,
                    286:                Value& aself,
                    287:                VStateless_class *avclass, const Method *amethod,
1.99      paf       288:                VMethodFrame *amethod_frame,
1.17      paf       289:                Value *arcontext,
                    290:                WContext *awcontext,
1.99      paf       291:                const Array *acode);
1.81      paf       292: 
1.100   ! paf       293:        void reattach(WContext *new_wcontext);
1.17      paf       294: 
1.22      paf       295:        /// always present
1.17      paf       296:        Value& self;
1.22      paf       297:        //@{
                    298:        /// @name either these // so called 'method-junction'
1.17      paf       299:        VStateless_class *vclass;  const Method *method;
1.22      paf       300:        //@}
                    301:        //@{
                    302:        /// @name or these are present // so called 'code-junction'
1.99      paf       303:        VMethodFrame *method_frame;
1.17      paf       304:        Value *rcontext;
                    305:        WContext *wcontext;
                    306:        const Array *code;
1.22      paf       307:        //@}
1.48      paf       308: };
                    309: 
                    310: /**
                    311:        native code method
                    312:        params can be NULL when 
1.53      paf       313:        method min&max params (see VStateless_class::add_native_method)
1.48      paf       314:        counts are zero.        
                    315: */
1.39      paf       316: typedef void (*Native_code_ptr)(Request& request, 
                    317:                                                                const String& method_name, 
1.48      paf       318:                                                                MethodParams *params);
1.39      paf       319: 
1.23      paf       320: /** 
1.22      paf       321:        class method.
                    322: 
                    323:        methods can have 
                    324:        - named or
                    325:        - numbered parameters
                    326: 
                    327:        methods can be
                    328:        - parser or 
                    329:        - native onces
                    330: 
1.40      paf       331:        holds
1.22      paf       332:        - parameter names or number limits
                    333:        - local names
                    334:        - code [parser or native]
                    335: */
1.17      paf       336: class Method : public Pooled {
                    337: public:
1.39      paf       338: 
1.41      paf       339:        /// allowed method call types
1.39      paf       340:        enum Call_type {
1.41      paf       341:                CT_ANY, ///< method can be called either statically or dynamically
                    342:                CT_STATIC, ///< method can be called only statically
                    343:                CT_DYNAMIC ///< method can be called only dynamically
1.39      paf       344:        };
                    345:        
                    346:        /// name for error reporting
1.17      paf       347:        const String& name;
1.39      paf       348:        ///
                    349:        Call_type call_type;
1.22      paf       350:        //@{
                    351:        /// @name either numbered params // for native-code methods = operators
1.17      paf       352:        int min_numbered_params_count, max_numbered_params_count;
1.22      paf       353:        //@}
                    354:        //@{
                    355:        /// @name or named params&locals // for parser-code methods
1.17      paf       356:        Array *params_names;  Array *locals_names;
1.22      paf       357:        //@}
                    358:        //@{
                    359:        /// @name the Code
1.17      paf       360:        const Array *parser_code;/*OR*/Native_code_ptr native_code;
1.22      paf       361:        //@}
1.17      paf       362: 
                    363:        Method(
                    364:                Pool& apool,
                    365:                const String& aname,
1.39      paf       366:                Call_type call_type,
1.17      paf       367:                int amin_numbered_params_count, int amax_numbered_params_count,
                    368:                Array *aparams_names, Array *alocals_names,
                    369:                const Array *aparser_code, Native_code_ptr anative_code) : 
                    370: 
                    371:                Pooled(apool),
                    372:                name(aname),
1.39      paf       373:                call_type(call_type),
1.17      paf       374:                min_numbered_params_count(amin_numbered_params_count),
                    375:                max_numbered_params_count(amax_numbered_params_count),
                    376:                params_names(aparams_names), locals_names(alocals_names),
                    377:                parser_code(aparser_code), native_code(anative_code) {
                    378:        }
                    379: 
1.22      paf       380:        /// call this before invoking to ensure proper actual numbered params count
1.75      parser    381:        void check_actual_numbered_params(
1.84      paf       382:                Value& self, const String& actual_name, Array *actual_numbered_params) const;
1.86      paf       383: };
                    384: 
                    385: ///    Auto-object used for temporarily substituting/removing elements
                    386: class Temp_value_element {
                    387:        Value& fwhere;
                    388:        const String& fname;
                    389:        Value *saved;
                    390: public:
                    391:        Temp_value_element(Value& awhere, const String& aname, Value *awhat) : 
                    392:                fwhere(awhere),
                    393:                fname(aname),
1.94      paf       394:                saved(awhere.get_element(aname, &awhere, false)) {
1.93      paf       395:                fwhere.put_element(aname, awhat, false);
1.86      paf       396:        }
                    397:        ~Temp_value_element() { 
1.93      paf       398:                fwhere.put_element(fname, saved, false);
1.86      paf       399:        }
1.1       paf       400: };
                    401: 
                    402: #endif

E-mail: