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

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

E-mail: