• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef NWEB_VALUE_H
17 #define NWEB_VALUE_H
18 
19 #include <iostream>
20 #include <map>
21 #include <set>
22 #include <sstream>
23 #include <string>
24 #include <vector>
25 
26 #include "nweb_export.h"
27 
28 namespace OHOS::NWeb {
29 union data_union {
30     int n;
31     double f;
32     bool b;
data_union()33     data_union()
34     {}
data_union(int value)35     data_union(int value) : n(value)
36     {}
data_union(double value)37     data_union(double value) : f(value)
38     {}
data_union(bool value)39     data_union(bool value) : b(value)
40     {}
41 };
42 
43 class OHOS_NWEB_EXPORT NWebValue {
44 public:
45     enum class Type : unsigned char {
46         NONE = 0,
47         BOOLEAN,
48         INTEGER,
49         DOUBLE,
50         STRING,
51         BINARY,
52         DICTIONARY,
53         LIST,
54         ERROR,
55         STRINGARRAY,
56         BOOLEANARRAY,
57         DOUBLEARRAY,
58         INT64ARRAY
59     };
NWebValue()60     NWebValue()
61     {}
NWebValue(Type type)62     explicit NWebValue(Type type) : type_(type)
63     {}
NWebValue(const int & value)64     explicit NWebValue(const int& value) : type_(Type::INTEGER), data_(value)
65     {}
NWebValue(const double & value)66     explicit NWebValue(const double& value) : type_(Type::DOUBLE), data_(value)
67     {}
NWebValue(const bool & value)68     explicit NWebValue(const bool& value) : type_(Type::BOOLEAN), data_(value)
69     {}
NWebValue(const std::string & value)70     explicit NWebValue(const std::string& value) : type_(Type::STRING), str_(value)
71     {}
NWebValue(const char * data,size_t len)72     NWebValue(const char* data, size_t len) : type_(Type::BINARY), str_(data, len)
73     {}
NWebValue(const std::vector<NWebValue> & value)74     explicit NWebValue(const std::vector<NWebValue>& value) : type_(Type::LIST), list_value_(value.begin(), value.end())
75     {}
NWebValue(const std::map<std::string,NWebValue> & value)76     explicit NWebValue(const std::map<std::string, NWebValue>& value)
77         : type_(Type::DICTIONARY), dictionary_value_(value)
78     {}
79 
NWebValue(const NWebValue & value)80     explicit NWebValue(const NWebValue& value) : type_(value.type_)
81     {
82         switch (type_) {
83             case Type::NONE:
84                 break;
85             case Type::BOOLEAN:
86                 data_.b = value.data_.b;
87                 break;
88             case Type::INTEGER:
89                 data_.n = value.data_.n;
90                 break;
91             case Type::DOUBLE:
92                 data_.f = value.data_.f;
93                 break;
94             case Type::STRING:
95                 str_ = value.str_;
96                 break;
97             case Type::BINARY:
98                 str_ = value.str_;
99                 break;
100             case Type::LIST:
101                 list_value_ = value.list_value_;
102                 break;
103             case Type::DICTIONARY:
104                 dictionary_value_ = value.dictionary_value_;
105                 break;
106             default:
107                 break;
108         }
109     }
110 
NWebValue(std::vector<NWebValue> && value)111     NWebValue(std::vector<NWebValue>&& value) : type_(Type::LIST)
112     {
113         std::swap(list_value_, value);
114     }
NWebValue(std::map<std::string,NWebValue> && value)115     NWebValue(std::map<std::string, NWebValue>&& value) : type_(Type::DICTIONARY)
116     {
117         std::swap(dictionary_value_, value);
118     }
NWebValue(NWebValue && value)119     NWebValue(NWebValue&& value)
120     {
121         *this = std::move(value);
122     }
123 
124     ~NWebValue() = default;
125 
126     NWebValue& operator=(const NWebValue& value)
127     {
128         SetType(value.type_);
129         switch (type_) {
130             case Type::NONE:
131                 break;
132             case Type::BOOLEAN:
133                 data_.b = value.data_.b;
134                 break;
135             case Type::INTEGER:
136                 data_.n = value.data_.n;
137                 break;
138             case Type::DOUBLE:
139                 data_.f = value.data_.f;
140                 break;
141             case Type::STRING:
142                 str_ = value.str_;
143                 break;
144             case Type::BINARY:
145                 str_ = value.str_;
146                 break;
147             case Type::LIST:
148                 list_value_ = value.list_value_;
149                 break;
150             case Type::DICTIONARY:
151                 dictionary_value_ = value.dictionary_value_;
152                 break;
153             default:
154                 std::cout << "error: Invalid type" << std::endl;
155                 break;
156         }
157         return *this;
158     }
159 
160     NWebValue& operator=(NWebValue&& value)
161     {
162         std::swap(type_, value.type_);
163         switch (type_) {
164             case Type::NONE:
165                 break;
166             case Type::BOOLEAN:
167                 std::swap(data_.b, value.data_.b);
168                 break;
169             case Type::INTEGER:
170                 std::swap(data_.n, value.data_.n);
171                 break;
172             case Type::DOUBLE:
173                 std::swap(data_.f, value.data_.f);
174                 break;
175             case Type::STRING:
176                 std::swap(str_, value.str_);
177                 break;
178             case Type::BINARY:
179                 std::swap(str_, value.str_);
180                 break;
181             case Type::LIST:
182                 std::swap(list_value_, value.list_value_);
183                 break;
184             case Type::DICTIONARY:
185                 std::swap(dictionary_value_, value.dictionary_value_);
186                 break;
187             default:
188                 std::cout << "error: Invalid type" << std::endl;
189                 break;
190         }
191         return *this;
192     }
193 
194     bool operator==(NWebValue& oVal)
195     {
196         if (type_ != oVal.type_)
197             return false;
198         switch (type_) {
199             case Type::NONE:
200                 return false;
201             case Type::BOOLEAN:
202                 return data_.b == oVal.data_.b;
203             case Type::INTEGER:
204                 return data_.n == oVal.data_.n;
205             case Type::DOUBLE:
206                 return data_.f == oVal.data_.f;
207             case Type::STRING:
208                 return str_ == oVal.str_;
209             case Type::LIST:
210                 if ((*this).list_value_.size() != oVal.list_value_.size())
211                     return false;
212                 for (size_t i = 0; i < list_value_.size(); ++i) {
213                     NWebValue& lVal = oVal.list_value_[i];
214                     NWebValue& rVal = (*this).list_value_[i];
215                     if (!(lVal == rVal)) {
216                         return false;
217                     }
218                 }
219                 return true;
220             case Type::DICTIONARY:
221                 if ((*this).dictionary_value_.size() != oVal.dictionary_value_.size())
222                     return false;
223                 for (auto item : dictionary_value_) {
224                     NWebValue& lVal = oVal.dictionary_value_[item.first];
225                     NWebValue& rVal = (*this).dictionary_value_[item.first];
226                     if (!(lVal == rVal)) {
227                         return false;
228                     }
229                 }
230                 return true;
231             case Type::BINARY:
232                 return str_ == oVal.str_;
233             default:
234                 std::cout << "error: Invalid type" << std::endl;
235                 return false;
236         }
237         return false;
238     }
239 
IsNone()240     bool IsNone()
241     {
242         return GetType() == Type::NONE;
243     }
244 
IsBoolean()245     bool IsBoolean()
246     {
247         return GetType() == Type::BOOLEAN;
248     }
249 
IsString()250     bool IsString()
251     {
252         return GetType() == Type::STRING;
253     }
254 
IsDouble()255     bool IsDouble()
256     {
257         return GetType() == Type::DOUBLE;
258     }
259 
IsINTEGER()260     bool IsINTEGER()
261     {
262         return GetType() == Type::INTEGER;
263     }
264 
IsList()265     bool IsList()
266     {
267         return GetType() == Type::LIST;
268     }
269 
IsDictionary()270     bool IsDictionary()
271     {
272         return GetType() == Type::DICTIONARY;
273     }
274 
IsBinary()275     bool IsBinary()
276     {
277         return GetType() == Type::BINARY;
278     }
279 
GetBoolean()280     bool GetBoolean()
281     {
282         validateType(Type::BOOLEAN);
283         return data_.b;
284     }
285 
SetBoolean(bool b)286     void SetBoolean(bool b)
287     {
288         validateType(Type::BOOLEAN);
289         data_.b = b;
290     }
291 
SetString(std::string str)292     void SetString(std::string str)
293     {
294         validateType(Type::STRING);
295         str_ = str;
296     }
297 
GetString()298     std::string GetString()
299     {
300         validateType(Type::STRING);
301         return str_;
302     }
303 
SetDouble(double dou)304     void SetDouble(double dou)
305     {
306         validateType(Type::DOUBLE);
307         data_.f = dou;
308     }
309 
GetDouble()310     double GetDouble()
311     {
312         validateType(Type::DOUBLE);
313         return data_.f;
314     }
315 
SetInt(int num)316     void SetInt(int num)
317     {
318         validateType(Type::INTEGER);
319         data_.n = num;
320     }
321 
GetInt()322     int GetInt()
323     {
324         validateType(Type::INTEGER);
325         return data_.n;
326     }
327 
GetListValueSize()328     size_t GetListValueSize()
329     {
330         validateType(Type::LIST);
331         return list_value_.size();
332     }
333 
GetListValue()334     std::vector<NWebValue> GetListValue()
335     {
336         validateType(Type::LIST);
337         return list_value_;
338     }
339 
GetListValue(unsigned int index)340     NWebValue& GetListValue(unsigned int index)
341     {
342         validateType(Type::LIST);
343         if (index >= list_value_.size()) {
344             std::cout << "error: index larger than size()" << std::endl;
345         }
346         return list_value_[index];
347     }
348 
AddListValue(const NWebValue & value)349     void AddListValue(const NWebValue& value)
350     {
351         validateType(Type::LIST);
352         SetType(Type::LIST);
353         list_value_.push_back(value);
354     }
355 
deleteListValue()356     void deleteListValue()
357     {
358         validateType(Type::LIST);
359         SetType(Type::LIST);
360         list_value_.pop_back();
361     }
362 
GetDictionaryValueSize()363     size_t GetDictionaryValueSize()
364     {
365         validateType(Type::DICTIONARY);
366         return dictionary_value_.size();
367     }
368 
GetDictionaryValueKeys()369     std::vector<std::string> GetDictionaryValueKeys()
370     {
371         validateType(Type::DICTIONARY);
372         std::vector<std::string> ret;
373         for (auto& item : dictionary_value_) {
374             ret.push_back(item.first);
375         }
376         return ret;
377     }
378 
HasDictionaryValueKey(std::string & key)379     bool HasDictionaryValueKey(std::string& key)
380     {
381         validateType(Type::DICTIONARY);
382         return dictionary_value_.count(key) == 1;
383     }
384 
GetDictionaryValue()385     std::map<std::string, NWebValue> GetDictionaryValue()
386     {
387         validateType(Type::DICTIONARY);
388         return dictionary_value_;
389     }
390 
GetDictionaryValue(std::string & key)391     NWebValue& GetDictionaryValue(std::string& key)
392     {
393         validateType(Type::DICTIONARY);
394         return dictionary_value_[key];
395     }
396 
AddDictionaryValue(std::string key,NWebValue & value)397     void AddDictionaryValue(std::string key, NWebValue& value)
398     {
399         validateType(Type::DICTIONARY);
400         dictionary_value_[key] = value;
401     }
402 
DeleteDictionaryValue(std::string & key)403     void DeleteDictionaryValue(std::string& key)
404     {
405         validateType(Type::DICTIONARY);
406         dictionary_value_.erase(key);
407     }
408 
GetBinaryValueSize()409     size_t GetBinaryValueSize()
410     {
411         validateType(Type::BINARY);
412         return str_.size();
413     }
414 
GetBinaryValue()415     const char* GetBinaryValue()
416     {
417         validateType(Type::BINARY);
418         return (const char*)str_.c_str();
419     }
420 
SetJsonString(std::string json_string)421     void SetJsonString(std::string json_string)
422     {
423         str_json_ = json_string;
424     }
425 
GetJsonString()426     std::string GetJsonString()
427     {
428         return str_json_;
429     }
430 
GetType()431     Type GetType()
432     {
433         return type_;
434     }
435 
SetType(Type type)436     void SetType(Type type)
437     {
438         type_ = type;
439     }
440 
validateType(Type type)441     void validateType(Type type) const
442     {
443         if (type_ != Type::NONE && type_ != type) {
444             std::cout << "error: Invalid type" << std::endl;
445         }
446     }
447 
448     int error_ = 0;
449 
450 private:
451     Type type_ = Type::NONE;
452     data_union data_;
453     std::string str_;
454     std::string str_json_;
455     std::map<std::string, NWebValue> dictionary_value_;
456     std::vector<NWebValue> list_value_;
457 };
458 } // namespace OHOS::NWeb
459 
460 #endif  // NWEB_VALUE_H
461