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