• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 NATIVE_RDB_VALUE_OBJECT_H
17 #define NATIVE_RDB_VALUE_OBJECT_H
18 
19 #include <string>
20 #include <variant>
21 #include <vector>
22 
23 #include "asset_value.h"
24 #include "rdb_visibility.h"
25 namespace OHOS {
26 namespace NativeRdb {
27 /**
28  * The ValueObject class of RDB.
29  */
30 class API_EXPORT ValueObject {
31 public:
32     /**
33      * @brief Use Type replace std::variant.
34      */
35     using Nil = std::monostate;
36     using Blob = std::vector<uint8_t>;
37     using Asset = AssetValue;
38     using Assets = std::vector<Asset>;
39     using Type = std::variant<Nil, int64_t, double, std::string, bool, Blob, Asset, Assets>;
40     template<typename Tp, typename... Types>
41     struct index_of : std::integral_constant<size_t, 0> {};
42 
43     template<typename Tp, typename... Types>
44     inline static constexpr size_t index_of_v = index_of<Tp, Types...>::value;
45 
46     template<typename Tp, typename First, typename... Rest>
47     struct index_of<Tp, First, Rest...>
48         : std::integral_constant<size_t, std::is_same_v<Tp, First> ? 0 : index_of_v<Tp, Rest...> + 1> {};
49 
50     template<typename... Types>
51     struct variant_size_of {
52         static constexpr size_t value = sizeof...(Types);
53     };
54 
55     template<typename T, typename... Types>
56     struct variant_index_of {
57         static constexpr size_t value = index_of_v<T, Types...>;
58     };
59 
60     template<typename... Types>
61     static variant_size_of<Types...> variant_size_test(const std::variant<Types...> &);
62 
63     template<typename T, typename... Types>
64     static variant_index_of<T, Types...> variant_index_test(const T &, const std::variant<Types...> &);
65 
66     template<typename T>
67     inline constexpr static int32_t TYPE_INDEX =
68         decltype(variant_index_test(std::declval<T>(), std::declval<Type>()))::value;
69 
70     inline constexpr static int32_t TYPE_MAX = decltype(variant_size_test(std::declval<Type>()))::value;
71 
72     /**
73      * @brief Indicates the ValueObject {@link ValueObject} type.
74      * */
75     enum TypeId : int32_t {
76         /** Indicates the ValueObject type is NULL.*/
77         TYPE_NULL = TYPE_INDEX<Nil>,
78         /** Indicates the ValueObject type is int.*/
79         TYPE_INT = TYPE_INDEX<int64_t>,
80         /** Indicates the ValueObject type is double.*/
81         TYPE_DOUBLE = TYPE_INDEX<double>,
82         /** Indicates the ValueObject type is string.*/
83         TYPE_STRING = TYPE_INDEX<std::string>,
84         /** Indicates the ValueObject type is bool.*/
85         TYPE_BOOL = TYPE_INDEX<bool>,
86         /** Indicates the ValueObject type is blob.*/
87         TYPE_BLOB = TYPE_INDEX<Blob>,
88         /** Indicates the ValueObject type is asset.*/
89         TYPE_ASSET = TYPE_INDEX<Asset>,
90         /** Indicates the ValueObject type is assets.*/
91         TYPE_ASSETS = TYPE_INDEX<Assets>,
92         /** the BUTT.*/
93         TYPE_BUTT = TYPE_MAX
94     };
95     Type value;
96 
97     /**
98      * @brief convert a std::variant input to another std::variant output with different (..._Types)
99      */
100     template<typename T>
101     static inline std::enable_if_t<(TYPE_INDEX<T>) < TYPE_MAX, const char *> DeclType()
102     {
103         return DECLARE_TYPES[TYPE_INDEX<T>];
104     }
105     /**
106      * @brief Constructor.
107      */
108     API_EXPORT ValueObject();
109 
110     /**
111      * @brief Destructor.
112      */
113     API_EXPORT ~ValueObject();
114 
115     /**
116      * @brief Constructor.
117      *
118      * A parameterized constructor used to create a ValueObject instance.
119      */
120     API_EXPORT ValueObject(Type val) noexcept;
121 
122     /**
123      * @brief Move constructor.
124      */
125     API_EXPORT ValueObject(ValueObject &&val) noexcept;
126 
127     /**
128      * @brief Copy constructor.
129      */
130     API_EXPORT ValueObject(const ValueObject &val);
131 
132     /**
133      * @brief Constructor.
134      *
135      * This constructor is used to convert the int input parameter to a value of type ValueObject.
136      *
137      * @param val Indicates an int input parameter.
138      */
139     API_EXPORT ValueObject(int32_t val);
140 
141     /**
142      * @brief Constructor.
143      *
144      * This constructor is used to convert the int64_t input parameter to a value of type ValueObject.
145      *
146      * @param val Indicates an int64_t input parameter.
147      */
148     API_EXPORT ValueObject(int64_t val);
149 
150     /**
151      * @brief Constructor.
152      *
153      * This constructor is used to convert the double input parameter to a value of type ValueObject.
154      *
155      * @param val Indicates an double input parameter.
156      */
157     API_EXPORT ValueObject(double val);
158 
159     /**
160      * @brief Constructor.
161      *
162      * This constructor is used to convert the bool input parameter to a value of type ValueObject.
163      *
164      * @param val Indicates an bool input parameter.
165      */
166     API_EXPORT ValueObject(bool val);
167 
168     /**
169      * @brief Constructor.
170      *
171      * This constructor is used to convert the string input parameter to a value of type ValueObject.
172      *
173      * @param val Indicates an string input parameter.
174      */
175     API_EXPORT ValueObject(std::string val);
176 
177     /**
178      * @brief Constructor.
179      *
180      * This constructor is used to convert the const char * input parameter to a value of type ValueObject.
181      *
182      * @param val Indicates an const char * input parameter.
183      */
184     API_EXPORT ValueObject(const char *val);
185 
186     /**
187      * @brief Constructor.
188      *
189      * This constructor is used to convert the vector<uint8_t> input parameter to a value of type ValueObject.
190      *
191      * @param val Indicates an vector<uint8_t> input parameter.
192      */
193     API_EXPORT ValueObject(const std::vector<uint8_t> &blob);
194 
195     /**
196      * @brief Constructor.
197      *
198      * This constructor is used to convert the Asset input parameter to a value of type ValueObject.
199      *
200      * @param val Indicates an Asset input parameter.
201      */
202     API_EXPORT ValueObject(Asset val);
203 
204     /**
205      * @brief Constructor.
206      *
207      * This constructor is used to convert the Assets input parameter to a value of type ValueObject.
208      *
209      * @param val Indicates an Assets input parameter.
210      */
211     API_EXPORT ValueObject(Assets val);
212 
213     /**
214      * @brief Move assignment operator overloaded function.
215      */
216     API_EXPORT ValueObject &operator=(ValueObject &&valueObject) noexcept;
217 
218     /**
219      * @brief Copy assignment operator overloaded function.
220      */
221     API_EXPORT ValueObject &operator=(const ValueObject &valueObject);
222 
223     /**
224      * @brief Obtains the type in this {@code ValueObject} object.
225      */
226     API_EXPORT TypeId GetType() const;
227 
228     /**
229      * @brief Obtains the int value in this {@code ValueObject} object.
230      */
231     API_EXPORT int GetInt(int &val) const;
232 
233     /**
234      * @brief Obtains the long value in this {@code ValueObject} object.
235      */
236     API_EXPORT int GetLong(int64_t &val) const;
237 
238     /**
239      * @brief Obtains the double value in this {@code ValueObject} object.
240      */
241     API_EXPORT int GetDouble(double &val) const;
242 
243     /**
244      * @brief Obtains the bool value in this {@code ValueObject} object.
245      */
246     API_EXPORT int GetBool(bool &val) const;
247 
248     /**
249      * @brief Obtains the string value in this {@code ValueObject} object.
250      */
251     API_EXPORT int GetString(std::string &val) const;
252 
253     /**
254      * @brief Obtains the vector<uint8_t> value in this {@code ValueObject} object.
255      */
256     API_EXPORT int GetBlob(std::vector<uint8_t> &val) const;
257 
258     /**
259      * @brief Obtains the vector<uint8_t> value in this {@code ValueObject} object.
260      */
261     API_EXPORT int GetAsset(Asset &val) const;
262 
263     /**
264      * @brief Obtains the vector<uint8_t> value in this {@code ValueObject} object.
265      */
266     API_EXPORT int GetAssets(Assets &val) const;
267 
268     /**
269      * @brief Type conversion function.
270      *
271      * @return Returns the int type ValueObject.
272      */
273     operator int() const
274     {
275         return static_cast<int>(std::get<int64_t>(value));
276     }
277 
278     /**
279      * @brief Type conversion function.
280      *
281      * @return Returns the int64_t type ValueObject.
282      */
283     operator int64_t() const
284     {
285         return std::get<int64_t>(value);
286     }
287 
288     /**
289      * @brief Type conversion function.
290      *
291      * @return Returns the double type ValueObject.
292      */
293     operator double() const
294     {
295         return std::get<double>(value);
296     }
297 
298     /**
299      * @brief Type conversion function.
300      *
301      * @return Returns the bool type ValueObject.
302      */
303     operator bool() const
304     {
305         return std::get<bool>(value);
306     }
307 
308     /**
309      * @brief Type conversion function.
310      *
311      * @return Returns the string type ValueObject.
312      */
313     operator std::string() const
314     {
315         return std::get<std::string>(value);
316     }
317 
318     /**
319      * @brief Type conversion function.
320      *
321      * @return Returns the vector<uint8_t> type ValueObject.
322      */
323     operator Blob() const
324     {
325         return std::get<Blob>(value);
326     }
327 
328     /**
329      * @brief Type conversion function.
330      *
331      * @return Returns the vector<uint8_t> type ValueObject.
332      */
333     operator Asset() const
334     {
335         return std::get<Asset>(value);
336     }
337 
338     /**
339     * @brief Type conversion function.
340     *
341     * @return Returns the vector<uint8_t> type ValueObject.
342     */
343     operator Assets() const
344     {
345         return std::get<Assets>(value);
346     }
347 
348     /**
349      * @brief Type conversion function.
350      *
351      * @return Returns the Type type ValueObject.
352      */
353     operator Type() const
354     {
355         return value;
356     }
357 
358 private:
359     template<class T>
360     int Get(T &output) const;
361     static constexpr const char *DECLARE_TYPES[TypeId::TYPE_BUTT] = {
362         /** Indicates the ValueObject type is NULL.*/
363         "",
364         /** Indicates the ValueObject type is int.*/
365         "INT",
366         /** Indicates the ValueObject type is double.*/
367         "REAL",
368         /** Indicates the ValueObject type is string.*/
369         "TEXT",
370         /** Indicates the ValueObject type is bool.*/
371         "INT",
372         /** Indicates the ValueObject type is blob.*/
373         "BLOB",
374         /** Indicates the ValueObject type is asset.*/
375         "ASSET",
376         /** Indicates the ValueObject type is assets.*/
377         "ASSETS"
378     };
379 };
380 using ValueObjectType = ValueObject::TypeId;
381 } // namespace NativeRdb
382 } // namespace OHOS
383 #endif
384