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