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 DATASHARE_VALUE_OBJECT_H 17 #define DATASHARE_VALUE_OBJECT_H 18 19 #include <variant> 20 #include <string> 21 #include <vector> 22 namespace OHOS { 23 namespace DataShare { 24 constexpr int INVALID_TYPE = -1; 25 constexpr int DATA_SHARE_NO_ERROR = 0; 26 enum DataShareValueObjectType : int32_t { 27 TYPE_NULL = 0, 28 TYPE_INT, 29 TYPE_INT64, 30 TYPE_DOUBLE, 31 TYPE_STRING, 32 TYPE_BOOL, 33 TYPE_BLOB, 34 }; 35 36 class DataShareValueObject { 37 public: DataShareValueObject()38 DataShareValueObject() : type(TYPE_NULL) {}; 39 ~DataShareValueObject() = default; DataShareValueObject(DataShareValueObject && object)40 DataShareValueObject(DataShareValueObject &&object) noexcept : type(object.type), value(std::move(object.value)) 41 { 42 object.type = DataShareValueObjectType::TYPE_NULL; 43 }; DataShareValueObject(const DataShareValueObject & object)44 DataShareValueObject(const DataShareValueObject &object) : type(object.type), value(object.value) {}; DataShareValueObject(int val)45 DataShareValueObject(int val) : type(TYPE_INT), value(static_cast<int64_t>(val)) {}; DataShareValueObject(int64_t val)46 DataShareValueObject(int64_t val) : type(TYPE_INT64), value(val) {}; DataShareValueObject(double val)47 DataShareValueObject(double val) : type(TYPE_DOUBLE), value(val) {}; DataShareValueObject(bool val)48 DataShareValueObject(bool val) : type(TYPE_BOOL), value(val) {}; DataShareValueObject(std::string val)49 DataShareValueObject(std::string val) : type(TYPE_STRING), value(std::move(val)) {}; DataShareValueObject(const char * val)50 DataShareValueObject(const char *val) : DataShareValueObject(std::string(val)) {}; DataShareValueObject(std::vector<uint8_t> blob)51 DataShareValueObject(std::vector<uint8_t> blob) : type(TYPE_BLOB), value(std::move(blob)) {}; 52 DataShareValueObject &operator=(DataShareValueObject &&object) noexcept 53 { 54 if (this == &object) { 55 return *this; 56 } 57 type = object.type; 58 value = std::move(object.value); 59 object.type = TYPE_NULL; 60 return *this; 61 }; 62 DataShareValueObject &operator=(const DataShareValueObject &object) 63 { 64 if (this == &object) { 65 return *this; 66 } 67 type = object.type; 68 value = object.value; 69 return *this; 70 } 71 GetType()72 DataShareValueObjectType GetType() const 73 { 74 return type; 75 } 76 77 operator int () const 78 { 79 if (std::get_if<int64_t>(&value) != nullptr) { 80 return static_cast<int>(std::get<int64_t>(value)); 81 } else { 82 return {}; 83 } 84 } int64_t()85 operator int64_t () const 86 { 87 if (std::get_if<int64_t>(&value) != nullptr) { 88 return std::get<int64_t>(value); 89 } else { 90 return {}; 91 } 92 } 93 operator double () const 94 { 95 if (std::get_if<double>(&value) != nullptr) { 96 return std::get<double>(value); 97 } else { 98 return {}; 99 } 100 } 101 operator bool () const 102 { 103 if (std::get_if<bool>(&value) != nullptr) { 104 return std::get<bool>(value); 105 } else { 106 return {}; 107 } 108 } string()109 operator std::string () const 110 { 111 if (std::get_if<std::string>(&value) != nullptr) { 112 return std::get<std::string>(value); 113 } else { 114 return {}; 115 } 116 } 117 operator std::vector<uint8_t> () const 118 { 119 if (std::get_if<std::vector<uint8_t>>(&value) != nullptr) { 120 return std::get<std::vector<uint8_t>>(value); 121 } else { 122 return {}; 123 } 124 } 125 DataShareValueObjectType type; 126 std::variant<std::monostate, int, int64_t, double, std::string, bool, std::vector<uint8_t>> value; 127 }; 128 } // namespace DataShare 129 } // namespace OHOS 130 #endif 131