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