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

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

E-mail: