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

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

E-mail: