OLD | NEW |
| (Empty) |
1 #pragma once | |
2 #include <deque> | |
3 #include <string> | |
4 #include <map> | |
5 #include "..\base\debug_blob.h" | |
6 | |
7 namespace json { | |
8 | |
9 class Visitor; | |
10 | |
11 class Value { | |
12 public: | |
13 virtual ~Value() {} | |
14 virtual Value* Clone() const = 0; | |
15 virtual void Accept(Visitor* vis) {} | |
16 }; | |
17 | |
18 class Null : public Value { | |
19 public: | |
20 Null(); | |
21 virtual Value* Clone() const; | |
22 virtual void Accept(Visitor* vis); | |
23 }; | |
24 | |
25 // Number does not support floating point numbers. | |
26 class Number : public Value { | |
27 public: | |
28 Number(); | |
29 Number(long long n); | |
30 Number(const Number& n); | |
31 Number(const debug::Blob& blob); | |
32 | |
33 virtual Value* Clone() const; | |
34 virtual void Accept(Visitor* vis); | |
35 void SetInteger(const void* ptr, size_t int_size, bool sign); | |
36 std::string AsHexString() const; | |
37 std::string AsDecString() const; | |
38 int AsInt() const; | |
39 | |
40 // "h" for hex representation, "d" for decimal, "b" for binary, | |
41 // <className>::<memberName> for enums. | |
42 // For example, hint == "h DEBUG_EVENT::dwDebugEventCode", JSON: | |
43 // "dwDebugEventCode" : "6", //0x6:LOAD_DLL_DEBUG_EVENT ih | |
44 void SetPrintHint(const std::string& hint); | |
45 void set_value(long long x) {value_ = x;} | |
46 long long value() const {return value_;} | |
47 | |
48 protected: | |
49 long long value_; | |
50 // std::deque<char> value_; | |
51 bool signed_; | |
52 size_t size_; | |
53 // std::string print_hint_; | |
54 }; | |
55 | |
56 class String : public Value { | |
57 public: | |
58 String(); | |
59 String(const std::string& str); | |
60 | |
61 virtual Value* Clone() const; | |
62 virtual void Accept(Visitor* vis); | |
63 | |
64 void SetStr(const std::string& str); | |
65 const std::string& GetStr() const; | |
66 std::string& GetStr(); | |
67 std::string value() const {return value_;} | |
68 | |
69 protected: | |
70 std::string value_; | |
71 }; | |
72 | |
73 class Boolean : public Value { | |
74 public: | |
75 Boolean(); | |
76 Boolean(bool value); | |
77 | |
78 virtual Value* Clone() const; | |
79 virtual void Accept(Visitor* vis); | |
80 bool value() const {return value_;} | |
81 | |
82 protected: | |
83 bool value_; | |
84 }; | |
85 | |
86 // JSON: | |
87 // "memory" : "00A5A567C6C688", //m | |
88 | |
89 class Blob : public Value { | |
90 public: | |
91 Blob(); | |
92 Blob(const Blob& other); | |
93 Blob(debug::Blob& blob); | |
94 ~Blob(); | |
95 | |
96 virtual Value* Clone() const; | |
97 virtual void Accept(Visitor* vis); | |
98 | |
99 size_t Size() const; | |
100 void Append(const void* data, size_t data_sz); | |
101 void Clear(); | |
102 debug::Blob value() const {return value_;} | |
103 | |
104 protected: | |
105 debug::Blob value_; | |
106 }; | |
107 | |
108 class Array : public Value { | |
109 public: | |
110 Array(); | |
111 ~Array(); | |
112 | |
113 virtual Value* Clone() const; | |
114 virtual void Accept(Visitor* vis); | |
115 | |
116 size_t Size() const; | |
117 const Value* GetAt(size_t i) const; | |
118 Value* GetAt(size_t i); | |
119 void SetAt(size_t i, Value* value); | |
120 void Append(Value* value); | |
121 void Clear(); | |
122 | |
123 protected: | |
124 std::deque<Value*> value_; | |
125 }; | |
126 | |
127 class Object : public Value { | |
128 public: | |
129 Object(); | |
130 ~Object(); | |
131 | |
132 virtual Value* Clone() const; | |
133 virtual void Accept(Visitor* vis); | |
134 | |
135 const Value* GetProperty(const std::string& name) const; | |
136 Value* GetProperty(const std::string& name); | |
137 int GetIntProperty(const std::string& name) const; | |
138 void SetProperty(const std::string& name, Value* value); | |
139 void SetProperty(const std::string& name, const std::string& value); | |
140 void DeleteProperty(const std::string& name); | |
141 void Clear(); | |
142 void GetPropertyNames(std::deque<std::string>* names) const; | |
143 | |
144 protected: | |
145 typedef std::deque<std::pair<std::string, Value*>> TypeOf_value_; | |
146 TypeOf_value_ value_; | |
147 }; | |
148 | |
149 class Visitor { | |
150 public: | |
151 virtual ~Visitor() {} | |
152 virtual void Visit(Null& element) {} | |
153 virtual void Visit(Number& element) {} | |
154 virtual void Visit(Boolean& element) {} | |
155 virtual void Visit(String& element) {} | |
156 virtual void Visit(Blob& element) {} | |
157 virtual void Visit(Array& element) {} | |
158 virtual void Visit(Object& element) {} | |
159 }; | |
160 | |
161 template <class T> | |
162 T* dynamic_value_cast(Value* value); | |
163 | |
164 // Implementation. | |
165 | |
166 class CastValueVisitor : public Visitor { | |
167 public: | |
168 CastValueVisitor() : type_(0) {} | |
169 virtual void Visit(Null& element) {type_ = 1;} | |
170 virtual void Visit(Number& element) {type_ = 2;} | |
171 virtual void Visit(Boolean& element) {type_ = 3;} | |
172 virtual void Visit(String& element) {type_ = 4;} | |
173 virtual void Visit(Blob& element) {type_ = 5;} | |
174 virtual void Visit(Array& element) {type_ = 6;} | |
175 virtual void Visit(Object& element) {type_ = 7;} | |
176 int type_; | |
177 }; | |
178 | |
179 template <class T> | |
180 T* dynamic_value_cast(Value* value) { | |
181 if (NULL == value) | |
182 return NULL; | |
183 CastValueVisitor vis1; | |
184 value->Accept(&vis1); | |
185 | |
186 T tmp; | |
187 CastValueVisitor vis2; | |
188 tmp.Accept(&vis2); | |
189 if (vis2.type_ != vis1.type_) | |
190 return NULL; | |
191 return (T*)value; | |
192 } | |
193 | |
194 template <class T> | |
195 const T* dynamic_value_cast(const Value* value) { | |
196 if (NULL == value) | |
197 return NULL; | |
198 CastValueVisitor vis1; | |
199 const_cast<Value*>(value)->Accept((&vis1)); | |
200 | |
201 T tmp; | |
202 CastValueVisitor vis2; | |
203 tmp.Accept(&vis2); | |
204 if (vis2.type_ != vis1.type_) | |
205 return NULL; | |
206 return (T*)value; | |
207 } | |
208 } // namespace json | |
OLD | NEW |