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