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

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

E-mail: