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

1.22      paf         1: /** @file
1.109.2.11  paf         2:        Parser: Value, Method, Junction .
1.24      paf         3: 
1.109.2.14  paf         4:        Copyright (c) 2001-2003 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.109.2.15! paf        11: static const char* IDENT_VALUE_H="$Date: 2003/01/31 12:34:41 $";
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.109.2.11  paf        17: #include "pa_operation.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.109.2.4  paf        24: class Junction; DECLARE_OBJECT_PTR(Junction);
1.109.2.12  paf        25: class Method; DECLARE_OBJECT_PTR(Method);
1.109.2.2  paf        26: template<typename K, typename V> class Hash;
1.109.2.12  paf        27: class Value; DECLARE_OBJECT_PTR(Value);
1.109.2.13  paf        28: typedef Hash<StringPtr, ValuePtr> HashStringValue; DECLARE_OBJECT_PTR(HashStringValue);
1.64      parser     29: class MethodParams;
1.109.2.4  paf        30: class VObject; DECLARE_OBJECT_PTR(VObject);
1.99      paf        31: class VMethodFrame;
1.109.2.15! paf        32: class VFile; DECLARE_OBJECT_PTR(VFile);
        !            33: class Table;
1.1       paf        34: 
1.31      paf        35: ///    grandfather of all @a values in @b Parser
1.109.2.11  paf        36: class Value: public PA_Object {
1.1       paf        37: public: // Value
                     38: 
1.106     paf        39:        /// value type, used for error reporting and 'is' expression operator
1.109.2.14  paf        40:        virtual const char* type() const =0;
1.89      paf        41: 
1.91      paf        42:        /** remember derived class instance 
                     43:            - VObject: the only client
                     44:        */
1.109.2.4  paf        45:        virtual VObjectPtr set_derived(VObjectPtr /*aderived*/) { return 0; }
1.91      paf        46: 
1.89      paf        47:        /**
1.95      paf        48:                all except VObject/VClass: this if @atype eq type()
                     49:                VObject/VClass: can locate parent class by it's type
1.89      paf        50:        */
1.109.2.14  paf        51:        virtual ValuePtr as(const char* atype, bool looking_up) {
1.96      paf        52:                return atype && strcmp(type(), atype)==0?this:0;
1.89      paf        53:        }
1.97      paf        54:        /// type checking helper, uses Value::as
1.109.2.14  paf        55:        bool is(const char* atype) { return as(atype, false)!=0; }
1.59      parser     56:        
1.106     paf        57:        /// is this value defined?
1.38      paf        58:        virtual bool is_defined() const { return true; }
1.59      parser     59:        
1.106     paf        60:        /// is this value string?
1.38      paf        61:        virtual bool is_string() const { return false; }
1.59      parser     62:        
1.106     paf        63:        /// what's the meaning of this value in context of expression?
1.109.2.2  paf        64:        virtual ValuePtr as_expr_result(bool /*return_string_as_is*/=false) { 
1.34      paf        65:                bark("(%s) can not be used in expression"); return 0; 
                     66:        }
1.59      parser     67:        
1.109.2.7  paf        68:        /** extract HashStringValue if any
                     69:                WARNING: FOR LOCAL USE ONLY, THIS POINTER IS NOT TO PASS TO ANYBODY!
                     70:        */
1.109.2.13  paf        71:        virtual HashStringValue* get_hash(StringPtr /*source*/) { return 0; }
1.59      parser     72:        
1.106     paf        73:        /// extract const String
1.109.2.13  paf        74:        virtual StringPtr get_string(Pool *pool) { return 0; }
1.59      parser     75:        
1.106     paf        76:        /// extract double
1.72      parser     77:        virtual double as_double() const { bark("(%s) does not have numerical (double) value"); return 0; }
1.59      parser     78:        
1.106     paf        79:        /// extract integer
1.72      parser     80:        virtual int as_int () const { bark("(%s) does not have numerical (int) value"); return 0; }
1.59      parser     81: 
1.106     paf        82:        /// extract bool
1.72      parser     83:        virtual bool as_bool() const { bark("(%s) does not have logical value"); return 0; }
1.59      parser     84:        
1.106     paf        85:        /// extract file
1.109.2.4  paf        86:        virtual VFilePtr as_vfile(Pool& pool, String::Untaint_lang lang=String::UL_UNSPECIFIED,
                     87:                bool origins_mode=false);
1.59      parser     88:        
1.106     paf        89:        /// extract Junction
1.109.2.2  paf        90:        virtual JunctionPtr get_junction() { return 0; }
1.59      parser     91:        
1.93      paf        92:        /** extract base object of Value
                     93:                @return for
                     94:                - VObject: fbase
                     95:        */
1.109.2.2  paf        96:        virtual ValuePtr base_object() { bark("(%s) has no base object"); return 0; }
1.93      paf        97:        
1.106     paf        98:        /// extract Value element
1.109.2.13  paf        99:        virtual ValuePtr get_element(StringPtr /*aname*/, Value& /*aself*/, bool /*looking_up*/) { bark("(%s) has no elements"); return 0; }
1.92      paf       100: 
1.106     paf       101:        /// store Value element under @a name
1.109.2.13  paf       102:        virtual bool put_element(StringPtr name, ValuePtr /*value*/, bool /*replace*/) { 
1.65      parser    103:                // to prevent modification of system classes,
                    104:                // created at system startup, and not having exception
                    105:                // handler installed, we neet to bark using request.pool
                    106:                bark("(%s) does not accept elements", 
1.109.2.2  paf       107:                        "element can not be stored to %s", name); 
1.93      paf       108:                return false;
1.65      parser    109:        }
1.59      parser    110:        
1.106     paf       111:        /// extract VStateless_class
1.85      paf       112:        virtual VStateless_class *get_class()=0;
1.107     paf       113: 
                    114:        /// extract VStateless_class
                    115:        virtual VStateless_class *get_last_derived_class() {
                    116:                return get_class();
                    117:        };
1.108     paf       118:        /// extract base object or class of Value, if any
1.109.2.6  paf       119:        virtual ValuePtr base() { bark("(%s) has can not have base"); return 0; }
1.32      paf       120: 
1.106     paf       121:        /// extract VTable
1.109.2.15! paf       122:        virtual Table* get_table() { return 0; }
1.1       paf       123: 
                    124: public: // usage
                    125: 
1.26      paf       126:        /// @return sure String. if it doesn't have string value barks
1.109.2.13  paf       127:        StringPtr as_string(Pool *pool) {
                    128:                StringPtr result=get_string(pool); 
1.109.2.2  paf       129:                if(!result.get())
1.35      paf       130:                        bark("(%s) has no string representation");
1.6       paf       131: 
1.109.2.2  paf       132:                return result;
1.1       paf       133:        }
                    134: 
1.6       paf       135: protected: 
1.1       paf       136: 
1.22      paf       137:        /// throws exception specifying bark-reason and name() type() of problematic value
1.65      parser    138:        void bark(char *reason, 
1.109.2.14  paf       139:                const char* alt_reason=0, StringPtr problem_source=Exception::undefined_source) const {
1.80      paf       140:                throw Exception("parser.runtime",
1.84      paf       141:                        problem_source,
                    142:                        alt_reason?alt_reason:reason, type());
1.1       paf       143:        }
                    144: 
1.17      paf       145: };
                    146: 
1.23      paf       147: /** \b junction is some code joined with context of it's evaluation.
1.22      paf       148: 
                    149:        there are code-junctions and method-junctions
                    150:        - code-junctions are used when some parameter passed in cury brackets
                    151:        - method-junctions used in ^method[] calls or $method references
1.99      paf       152: 
                    153:        Junctions register themselves in method_frame [if any] for consequent invalidation.
                    154:        This prevents evaluation of junctions in outdated context
                    155: 
                    156:        To stop situations like this:
                    157: @code
                    158:        @main[]
                    159:        ^method1[]
                    160:        ^method2[]
                    161: 
                    162:        @method1[]
                    163:        $junction{
                    164:                some code
                    165:        }
                    166: 
                    167:        @method2[]
                    168:        ^junction[]
                    169: @endcode
                    170: 
1.101     paf       171:        On wcontext[most dynamic context of all] scope exit (WContext::~WContext()) got cleaned - 
                    172:        Junction::wcontext becomes WContext.fparent (if any), 
                    173:        or Junction::method_frame becomes 0 (if no parent), which later in Request::process triggers exception
                    174: 
                    175:        parent changing helps ^switch implementation to remain simple
1.22      paf       176: */
1.109.2.2  paf       177: class Junction: public PA_Object {
1.17      paf       178: public:
                    179: 
1.109.2.2  paf       180:        Junction(
1.104     paf       181:                Value& aself,
1.109.2.11  paf       182:                const Method* amethod,
                    183:                VMethodFrame* amethod_frame,
                    184:                Value* arcontext,
                    185:                WContext* awcontext,
                    186:                ArrayOperationPtr acode);
1.81      paf       187: 
1.100     paf       188:        void reattach(WContext *new_wcontext);
1.17      paf       189: 
1.105     paf       190:        /// always present
1.104     paf       191:        Value& self;
1.22      paf       192:        //@{
                    193:        /// @name either these // so called 'method-junction'
1.109.2.11  paf       194:        const Method* method;
1.22      paf       195:        //@}
                    196:        //@{
                    197:        /// @name or these are present // so called 'code-junction'
1.109.2.11  paf       198:        VMethodFrame* method_frame;
                    199:        Value* rcontext;
                    200:        WContext* wcontext;
                    201:        ArrayOperationPtr code;
1.22      paf       202:        //@}
1.48      paf       203: };
                    204: 
                    205: /**
                    206:        native code method
                    207:        params can be NULL when 
1.53      paf       208:        method min&max params (see VStateless_class::add_native_method)
1.48      paf       209:        counts are zero.        
                    210: */
1.109.2.11  paf       211: typedef void (*NativeCodePtr)(Request& request, 
1.109.2.13  paf       212:                                                                StringPtr method_name, 
1.109.2.10  paf       213:                                                                MethodParams& params);
                    214: 
1.109.2.13  paf       215: typedef Array<StringPtr> ArrayString; DECLARE_OBJECT_PTR(ArrayString);
1.109.2.10  paf       216:        
1.39      paf       217: 
1.23      paf       218: /** 
1.22      paf       219:        class method.
                    220: 
                    221:        methods can have 
                    222:        - named or
                    223:        - numbered parameters
                    224: 
                    225:        methods can be
                    226:        - parser or 
                    227:        - native onces
                    228: 
1.40      paf       229:        holds
1.22      paf       230:        - parameter names or number limits
                    231:        - local names
                    232:        - code [parser or native]
                    233: */
1.109.2.2  paf       234: class Method: public PA_Object {
1.17      paf       235: public:
1.39      paf       236: 
1.41      paf       237:        /// allowed method call types
1.39      paf       238:        enum Call_type {
1.41      paf       239:                CT_ANY, ///< method can be called either statically or dynamically
                    240:                CT_STATIC, ///< method can be called only statically
                    241:                CT_DYNAMIC ///< method can be called only dynamically
1.39      paf       242:        };
                    243:        
                    244:        /// name for error reporting
1.109.2.13  paf       245:        StringPtr name;
1.39      paf       246:        ///
                    247:        Call_type call_type;
1.22      paf       248:        //@{
                    249:        /// @name either numbered params // for native-code methods = operators
1.17      paf       250:        int min_numbered_params_count, max_numbered_params_count;
1.22      paf       251:        //@}
                    252:        //@{
                    253:        /// @name or named params&locals // for parser-code methods
1.109.2.13  paf       254:        ArrayStringPtr params_names;  ArrayStringPtr locals_names;
1.22      paf       255:        //@}
                    256:        //@{
                    257:        /// @name the Code
1.109.2.11  paf       258:        ArrayOperationPtr parser_code;/*OR*/NativeCodePtr native_code;
1.22      paf       259:        //@}
1.17      paf       260: 
1.109.2.11  paf       261:        Method::Method(
1.109.2.13  paf       262:                StringPtr aname,
1.39      paf       263:                Call_type call_type,
1.17      paf       264:                int amin_numbered_params_count, int amax_numbered_params_count,
1.109.2.13  paf       265:                ArrayStringPtr aparams_names, ArrayStringPtr alocals_names,
1.109.2.11  paf       266:                ArrayOperationPtr aparser_code, NativeCodePtr anative_code) : 
1.17      paf       267: 
                    268:                name(aname),
1.39      paf       269:                call_type(call_type),
1.17      paf       270:                min_numbered_params_count(amin_numbered_params_count),
                    271:                max_numbered_params_count(amax_numbered_params_count),
                    272:                params_names(aparams_names), locals_names(alocals_names),
                    273:                parser_code(aparser_code), native_code(anative_code) {
                    274:        }
                    275: 
1.22      paf       276:        /// call this before invoking to ensure proper actual numbered params count
1.75      parser    277:        void check_actual_numbered_params(
1.109.2.13  paf       278:                Value& self, StringPtr actual_name, MethodParams& actual_numbered_params) const;
1.86      paf       279: };
                    280: 
                    281: ///    Auto-object used for temporarily substituting/removing elements
                    282: class Temp_value_element {
                    283:        Value& fwhere;
1.109.2.13  paf       284:        StringPtr fname;
1.109.2.2  paf       285:        ValuePtr saved;
1.86      paf       286: public:
1.109.2.13  paf       287:        Temp_value_element(Value& awhere, StringPtr aname, ValuePtr awhat) : 
1.86      paf       288:                fwhere(awhere),
                    289:                fname(aname),
1.108     paf       290:                saved(awhere.get_element(aname, awhere, false)) {
1.93      paf       291:                fwhere.put_element(aname, awhat, false);
1.86      paf       292:        }
                    293:        ~Temp_value_element() { 
1.93      paf       294:                fwhere.put_element(fname, saved, false);
1.86      paf       295:        }
1.1       paf       296: };
1.109.2.1  paf       297: 
                    298: /**
                    299:        $content-type[text/html] -> 
                    300:                content-type: text/html
                    301:        $content-type[$value[text/html] charset[windows-1251]] -> 
                    302:                content-type: text/html; charset=windows-1251
                    303: */
1.109.2.13  paf       304: StringPtr attributed_meaning_to_string(Pool& pool,
1.109.2.11  paf       305:                                                                                        ValuePtr meaning, String::Untaint_lang lang, bool forced);
1.109.2.7  paf       306: 
                    307: // defines
                    308: 
                    309: ///@{common field names
                    310: #define CHARSET_NAME "charset"
1.109.2.8  paf       311: #define VALUE_NAME "value"
1.109.2.9  paf       312: #define CONTENT_TYPE_NAME "content-type"
                    313: //@}
1.109.2.8  paf       314: 
                    315: ///@{common field names
                    316: extern StringPtr value_name;
1.109.2.9  paf       317: extern StringPtr content_type_name;
1.109.2.7  paf       318: ///@}
1.1       paf       319: 
                    320: #endif
1.109.2.5  paf       321:  

E-mail: