• 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 <string>
21 #include "nweb_export.h"
22 
23 namespace OHOS::NWeb {
24 union data_union {
25     int n;
26     double f;
27     bool b;
28 };
29 
30 class OHOS_NWEB_EXPORT NWebValue {
31 public:
32     enum class Type : unsigned char {
33         NONE = 0,
34         BOOLEAN,
35         INTEGER,
36         DOUBLE,
37         STRING,
38         BINARY,
39         DICTIONARY,
40         LIST
41     };
42 
NWebValue(Type type)43     explicit NWebValue(Type type) : type_(type) {}
44 
45     ~NWebValue() = default;
46 
GetBoolean()47     bool GetBoolean() { return data_.b; }
48 
SetBoolean(bool b)49     void SetBoolean(bool b) { data_.b = b; }
50 
SetString(std::string str)51     void SetString(std::string str) { str_ = str; }
52 
GetString()53     std::string GetString() { return str_; }
54 
SetDouble(double dou)55     void SetDouble(double dou) { data_.f = dou; }
56 
GetDouble()57     double GetDouble() { return data_.f; }
58 
SetInt(int num)59     void SetInt(int num) { data_.n = num; }
60 
GetInt()61     int GetInt() { return data_.n; }
62 
SetJsonString(std::string json_string)63     void SetJsonString(std::string json_string) { str_json_ = json_string; }
64 
GetJsonString()65     std::string GetJsonString() { return str_json_; }
66 
GetType()67     Type GetType() { return type_; }
68 
SetType(Type type)69     void SetType(Type type) { type_ = type; }
70 
71     int error_ = 0;
72 
73 private:
74     Type type_ = Type::NONE;
75     data_union data_;
76     std::string str_;
77     std::string str_json_;
78 };
79 } // namespace OHOS::NWeb
80 
81 #endif  // NWEB_VALUE_H
82