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