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

1.22      paf         1: /** @file
1.110     paf         2:        Parser: Value, Method, Junction .
1.24      paf         3: 
1.131     misha       4:        Copyright (c) 2001-2009 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.140   ! moko       11: static const char * const IDENT_VALUE_H="$Date: 2010-09-16 23:33:52 $";
1.1       paf        12: 
1.133     misha      13: #include "pa_common.h"
1.1       paf        14: #include "pa_array.h"
                     15: #include "pa_exception.h"
1.120     paf        16: #include "pa_property.h"
1.1       paf        17: 
1.99      paf        18: // forwards
                     19: 
1.9       paf        20: class VStateless_class;
1.1       paf        21: class WContext;
1.133     misha      22: class Request;
                     23: class Request_charsets;
1.17      paf        24: class Junction;
1.123     paf        25: class VJunction;
1.17      paf        26: class Method;
1.131     misha      27: class Value;
1.64      parser     28: class MethodParams;
1.93      paf        29: class VObject;
1.99      paf        30: class VMethodFrame;
1.110     paf        31: class VFile;
                     32: class Table;
1.133     misha      33: typedef Array<Value*> ArrayValue;
1.1       paf        34: 
1.139     misha      35: struct Json_options {
                     36:        Request* r;
                     37:        HashStringValue* methods;
                     38:        Value* params;
                     39:        bool skip_unknown;
1.140   ! moko       40:        char* indent;
1.139     misha      41:        enum Date { D_SQL, D_GMT, D_TIMESTAMP } date;
                     42:        enum Table { T_ARRAY, T_OBJECT } table;
                     43:        enum File { F_BODYLESS, F_BASE64, F_TEXT } file;
                     44: 
                     45:        Json_options(Request* arequest): 
                     46:                r(arequest),
                     47:                methods(NULL),
                     48:                params(NULL),
                     49:                skip_unknown(false),
1.140   ! moko       50:                indent(NULL),
1.139     misha      51:                date(D_SQL),
                     52:                table(T_OBJECT),
                     53:                file(F_BODYLESS)
                     54:        {}
                     55: 
                     56:        bool set_date_format(const String &value){
                     57:                if(value == "gmt-string") date = D_GMT;
                     58:                else if (value == "sql-string") date = D_SQL;
                     59:                else if (value == "unix-timestamp") date = D_TIMESTAMP;
                     60:                else return false;
                     61:                return true;
                     62:        }
                     63: 
                     64:        bool set_table_format(const String &value){
                     65:                if(value == "array") table = T_ARRAY;
                     66:                else if (value == "object") table = T_OBJECT;
                     67:                else return false;
                     68:                return true;
                     69:        }
                     70: 
                     71:        bool set_file_format(const String &value){
                     72:                if(value == "base64") file = F_BASE64;
                     73:                else if (value == "text") file = F_TEXT;
                     74:                else return false;
                     75:                return true;
                     76:        }
                     77: };
                     78: 
1.31      paf        79: ///    grandfather of all @a values in @b Parser
1.110     paf        80: class Value: public PA_Object {
1.1       paf        81: public: // Value
                     82: 
1.106     paf        83:        /// value type, used for error reporting and 'is' expression operator
1.110     paf        84:        virtual const char* type() const =0;
1.89      paf        85: 
                     86:        /**
1.95      paf        87:                all except VObject/VClass: this if @atype eq type()
                     88:                VObject/VClass: can locate parent class by it's type
1.89      paf        89:        */
1.134     misha      90:        virtual Value* as(const char* atype) {
1.96      paf        91:                return atype && strcmp(type(), atype)==0?this:0;
1.89      paf        92:        }
1.97      paf        93:        /// type checking helper, uses Value::as
1.134     misha      94:        bool is(const char* atype) { return as(atype)!=0; }
1.59      parser     95:        
1.106     paf        96:        /// is this value defined?
1.38      paf        97:        virtual bool is_defined() const { return true; }
1.59      parser     98:        
1.106     paf        99:        /// is this value string?
1.38      paf       100:        virtual bool is_string() const { return false; }
1.124     paf       101: 
                    102:        /// is this value void?
                    103:        virtual bool is_void() const { return false; }
                    104: 
1.128     misha     105:        /// is this value bool?
                    106:        virtual bool is_bool() const { return false; }
                    107: 
1.124     paf       108:        /// is this value number?
1.126     paf       109:        virtual bool is_evaluated_expr() const { return false; }
1.124     paf       110: 
1.106     paf       111:        /// what's the meaning of this value in context of expression?
1.110     paf       112:        virtual Value& as_expr_result(bool /*return_string_as_is*/=false) {
1.112     paf       113:                return *bark("is '%s', can not be used in expression"); 
1.34      paf       114:        }
1.59      parser    115:        
1.110     paf       116:        
                    117: 
                    118:        /** extract HashStringValue if any
                    119:                WARNING: FOR LOCAL USE ONLY, THIS POINTER IS NOT TO PASS TO ANYBODY!
                    120:        */
                    121:        virtual HashStringValue* get_hash() { return 0; }
1.59      parser    122:        
1.106     paf       123:        /// extract const String
1.110     paf       124:        virtual const String* get_string() { return 0; }
1.136     misha     125: 
1.139     misha     126:        /// extract json-string
                    127:        virtual const String* get_json_string(Json_options* options=0);
                    128: 
1.136     misha     129:        virtual HashStringValue* get_fields() { return 0; }
1.59      parser    130:        
1.106     paf       131:        /// extract double
1.110     paf       132:        virtual double as_double() const { bark("is '%s', it does not have numerical (double) value"); return 0; }
1.59      parser    133:        
1.106     paf       134:        /// extract integer
1.110     paf       135:        virtual int as_int () const { bark("is '%s', it does not have numerical (int) value"); return 0; }
1.59      parser    136: 
1.106     paf       137:        /// extract bool
1.110     paf       138:        virtual bool as_bool() const { bark("is '%s', it does not have logical value"); return 0; }
1.59      parser    139:        
1.106     paf       140:        /// extract file
1.132     misha     141:        virtual VFile* as_vfile(String::Language lang, const Request_charsets* charsets=0);
1.59      parser    142:        
1.106     paf       143:        /// extract Junction
1.110     paf       144:        virtual Junction* get_junction();
1.59      parser    145:        
1.120     paf       146:        /// @return Value element; can return Junction for methods; Code-Junction for code; Getter-Junction for property
1.134     misha     147:        virtual Value* get_element(const String& /*aname*/);
1.92      paf       148: 
1.122     paf       149:        /// indicator value meaning that put_element overwritten something
1.123     paf       150:        #define PUT_ELEMENT_REPLACED_ELEMENT reinterpret_cast<const VJunction*>(1)
1.134     misha     151: 
1.106     paf       152:        /// store Value element under @a name
1.122     paf       153:        /// @returns putter method junction, or it can just report[PUT_ELEMENT_REPLACED_ELEMENT] 
                    154:        /// that it replaced something in base fields 
1.134     misha     155:        virtual const VJunction* put_element(const String& aname, Value* /*avalue*/, bool /*areplace*/) { 
1.65      parser    156:                // to prevent modification of system classes,
                    157:                // created at system startup, and not having exception
                    158:                // handler installed, we neet to bark using request.pool
1.112     paf       159:                bark("element can not be stored to %s", &aname); 
1.120     paf       160:                return 0;
1.65      parser    161:        }
1.137     moko      162: 
                    163:        /// VObject default getter & setter support
                    164:        virtual void enable_default_getter(){ }
                    165:        virtual void enable_default_setter(){ }
                    166:        virtual void disable_default_getter(){ }
                    167:        virtual void disable_default_setter(){ }
                    168:        virtual bool is_enabled_default_getter(){ return true; }
1.59      parser    169:        
1.106     paf       170:        /// extract VStateless_class
1.135     misha     171:        virtual VStateless_class* get_class()=0;
1.107     paf       172: 
1.135     misha     173:        /// extract base class, if any
                    174:        virtual VStateless_class* base() { return 0; }
1.32      paf       175: 
1.106     paf       176:        /// extract VTable
1.110     paf       177:        virtual Table* get_table() { return 0; }
1.1       paf       178: 
                    179: public: // usage
                    180: 
1.26      paf       181:        /// @return sure String. if it doesn't have string value barks
1.1       paf       182:        const String& as_string() {
1.110     paf       183:                const String* result=get_string(); 
1.1       paf       184:                if(!result)
1.110     paf       185:                        bark("is '%s', it has no string representation");
1.6       paf       186: 
1.1       paf       187:                return *result;
                    188:        }
                    189: 
1.22      paf       190:        /// throws exception specifying bark-reason and name() type() of problematic value
1.112     paf       191:        Value* bark(const char *reason, const String *problem_source=0) const {
1.127     paf       192:                //if(this) // removing warnings on unreachable code
1.129     misha     193:                        throw Exception(PARSER_RUNTIME,
1.117     paf       194:                                problem_source, 
                    195:                                reason, type());
                    196: 
1.127     paf       197:                //return 0;
1.1       paf       198:        }
                    199: 
1.17      paf       200: };
1.110     paf       201:        
1.48      paf       202: 
                    203: /**
1.110     paf       204:        $content-type[text/html] -> 
                    205:                content-type: text/html
1.130     misha     206:        $content-type[$.value[text/html] $.charset[windows-1251]] -> 
1.110     paf       207:                content-type: text/html; charset=windows-1251
1.48      paf       208: */
1.110     paf       209: const String& attributed_meaning_to_string(Value& meaning, 
                    210:                                           String::Language lang, 
1.128     misha     211:                                           bool forced=false,
                    212:                                           bool allow_bool=false);
1.110     paf       213: 
                    214: // defines
                    215: 
                    216: ///@{common field names
                    217: #define CHARSET_NAME "charset"
                    218: #define VALUE_NAME "value"
1.114     paf       219: #define EXPIRES_NAME "expires"
1.110     paf       220: #define CONTENT_TYPE_NAME "content-type"
1.119     paf       221: #define NAME_NAME "name"
1.110     paf       222: //@}
                    223: 
                    224: ///@{common field names
                    225: extern const String value_name;
1.114     paf       226: extern const String expires_name;
1.110     paf       227: extern const String content_type_name;
                    228: extern const String name_name;
                    229: ///@}
1.1       paf       230: 
                    231: #endif

E-mail: