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