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 /**
26  * @brief DataShare Predicates Object Type .
27  */
28 enum class DataSharePredicatesObjectType {
29     /** Predicates Object Type is null.*/
30     TYPE_NULL = 0x00,
31     /** Predicates Object Type is int.*/
32     TYPE_INT,
33     /** Predicates Object Type is double.*/
34     TYPE_DOUBLE,
35     /** Predicates Object Type is string.*/
36     TYPE_STRING,
37     /** Predicates Object Type is bool.*/
38     TYPE_BOOL,
39     /** Predicates Object Type is long.*/
40     TYPE_LONG,
41 };
42 
43 /**
44  * @brief Use ObjectType replace DataSharePredicatesObjectType namespace.
45  */
46 using ObjectType = DataSharePredicatesObjectType;
47 
48 /**
49  * The SingleValue class.
50  */
51 class SingleValue {
52 public:
53 
54     /**
55      * @brief Use Type replace variant namespace.
56      */
57     using Type = std::variant<std::monostate, int, int64_t, double, std::string, bool>;
58     Type value;
59 
60     /**
61      * @brief Constructor.
62      */
63     SingleValue() = default;
64 
65     /**
66      * @brief Destructor.
67      */
68     ~SingleValue() = default;
69 
70     /**
71      * @brief Constructor.
72      */
SingleValue(Type val)73     SingleValue(Type val) noexcept : value(std::move(val))
74     {
75     }
76 
77     /**
78      * @brief Move constructor.
79      */
SingleValue(SingleValue && val)80     SingleValue(SingleValue &&val) noexcept :value(std::move(val.value))
81     {
82     }
83 
84     /**
85      * @brief Copy constructor.
86      */
SingleValue(const SingleValue & val)87     SingleValue(const SingleValue &val) : value(val.value) {}
88     SingleValue &operator=(SingleValue &&object) noexcept
89     {
90         if (this == &object) {
91             return *this;
92         }
93         value = std::move(object.value);
94         return *this;
95     }
96 
97     SingleValue &operator=(const SingleValue &object)
98     {
99         if (this == &object) {
100             return *this;
101         }
102         value = object.value;
103         return *this;
104     }
105 
106     /**
107      * @brief constructor.
108      *
109      * @param int Specifies the parameter of the type.
110      */
SingleValue(int val)111     SingleValue(int val) : value(val) {}
112 
113     /**
114      * @brief constructor.
115      *
116      * @param int64_t Specifies the parameter of the type.
117      */
SingleValue(int64_t val)118     SingleValue(int64_t val) : value(val) {}
119 
120     /**
121      * @brief constructor.
122      *
123      * @param idoublent Specifies the parameter of the type.
124      */
SingleValue(double val)125     SingleValue(double val) : value(val) {}
126 
127     /**
128      * @brief constructor.
129      *
130      * @param bool Specifies the parameter of the type.
131      */
SingleValue(bool val)132     SingleValue(bool val) : value(val) {}
133 
134     /**
135      * @brief Copy constructor.
136      *
137      * @param char Specifies the parameter of the type.
138      */
SingleValue(const char * val)139     SingleValue(const char *val) : value(std::string(val)) {}
140 
141     /**
142      * @brief Move constructor.
143      *
144      * @param string Specifies the parameter of the type.
145      */
SingleValue(std::string val)146     SingleValue(std::string val) : value(std::move(val)) {}
147     operator int () const
148     {
149         if (std::get_if<int>(&value) != nullptr) {
150             return std::get<int>(value);
151         } else {
152             return {};
153         }
154     }
int64_t()155     operator int64_t () const
156     {
157         if (std::get_if<int64_t>(&value) != nullptr) {
158             return std::get<int64_t>(value);
159         } else {
160             return {};
161         }
162     }
163     operator double () const
164     {
165         if (std::get_if<double>(&value) != nullptr) {
166             return std::get<double>(value);
167         } else {
168             return {};
169         }
170     }
171     operator bool () const
172     {
173         if (std::get_if<bool>(&value) != nullptr) {
174             return std::get<bool>(value);
175         } else {
176             return {};
177         }
178     }
string()179     operator std::string () const
180     {
181         if (std::get_if<std::string>(&value) != nullptr) {
182             return std::get<std::string>(value);
183         } else {
184             return {};
185         }
186     }
187 };
188 } // namespace DataShare
189 } // namespace OHOS
190 #endif