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 ERROR, 42 STRINGARRAY, 43 BOOLEANARRAY, 44 DOUBLEARRAY, 45 INT64ARRAY 46 }; 47 NWebValue(Type type)48 explicit NWebValue(Type type) : type_(type) {} 49 50 ~NWebValue() = default; 51 GetBoolean()52 bool GetBoolean() { return data_.b; } 53 SetBoolean(bool b)54 void SetBoolean(bool b) { data_.b = b; } 55 SetString(std::string str)56 void SetString(std::string str) { str_ = str; } 57 GetString()58 std::string GetString() { return str_; } 59 SetDouble(double dou)60 void SetDouble(double dou) { data_.f = dou; } 61 GetDouble()62 double GetDouble() { return data_.f; } 63 SetInt(int num)64 void SetInt(int num) { data_.n = num; } 65 GetInt()66 int GetInt() { return data_.n; } 67 SetJsonString(std::string json_string)68 void SetJsonString(std::string json_string) { str_json_ = json_string; } 69 GetJsonString()70 std::string GetJsonString() { return str_json_; } 71 GetType()72 Type GetType() { return type_; } 73 SetType(Type type)74 void SetType(Type type) { type_ = type; } 75 76 int error_ = 0; 77 78 private: 79 Type type_ = Type::NONE; 80 data_union data_; 81 std::string str_; 82 std::string str_json_; 83 }; 84 } // namespace OHOS::NWeb 85 86 #endif // NWEB_VALUE_H 87