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 PREFERENCES_VALUE_H
17 #define PREFERENCES_VALUE_H
18 
19 #include <string>
20 #include <variant>
21 #include <vector>
22 
23 #include "preferences_visibility.h"
24 
25 namespace OHOS {
26 namespace NativePreferences {
27 struct Object {
28     std::string valueStr;
29     Object() = default;
ObjectObject30     Object(const std::string &str) : valueStr(str) {};
31     bool operator==(const Object &other) const
32     {
33         return valueStr == other.valueStr;
34     }
35 };
36 
37 struct BigInt {
38 public:
39     BigInt() = default;
BigIntBigInt40     BigInt(const std::vector<uint64_t> &words, int sign) : words_(std::move(words)), sign_(sign)
41     {
42     }
43     ~BigInt() = default;
44     bool operator==(const BigInt &value) const
45     {
46         return sign_ == value.sign_ && words_ == value.words_;
47     }
48     std::vector<uint64_t> words_;
49     int sign_;
50 };
51 
52 /**
53  * The PreferencesValue class of the preference. Various operations on PreferencesValue are provided in this class.
54  */
55 class PREF_API_EXPORT PreferencesValue {
56 public:
PreferencesValue()57     PREF_API_EXPORT PreferencesValue()
58     {
59         value_ = std::monostate();
60     }
61 
~PreferencesValue()62     PREF_API_EXPORT ~PreferencesValue()
63     {
64     }
65 
66     /**
67      * @brief Move constructor.
68      */
69     PREF_API_EXPORT PreferencesValue(PreferencesValue &&preferencesValue) noexcept;
70 
71     /**
72      * @brief Copy constructor.
73      */
74     PREF_API_EXPORT PreferencesValue(const PreferencesValue &preferencesValue);
75 
76     /**
77      * @brief Constructor.
78      *
79      * This constructor is used to convert the int input parameter to a value of type PreferencesValue.
80      *
81      * @param value Indicates an int input parameter.
82      */
83     PREF_API_EXPORT PreferencesValue(int value);
84 
85     /**
86      * @brief Constructor.
87      *
88      * This constructor is used to convert the int64_t input parameter to a value of type PreferencesValue.
89      *
90      * @param value Indicates a int64_t input parameter.
91      */
92     PREF_API_EXPORT PreferencesValue(int64_t value);
93 
94     /**
95      * @brief Constructor.
96      *
97      * This constructor is used to convert the int64_t input parameter to a value of type PreferencesValue.
98      *
99      * @param value Indicates a int64_t input parameter.
100      */
101     PREF_API_EXPORT PreferencesValue(float value);
102 
103     /**
104      * @brief Constructor.
105      *
106      * This constructor is used to convert the double input parameter to a value of type PreferencesValue.
107      *
108      * @param value Indicates a double input parameter.
109      */
110     PREF_API_EXPORT PreferencesValue(double value);
111 
112     /**
113      * @brief Constructor.
114      *
115      * This constructor is used to convert the bool input parameter to a value of type PreferencesValue.
116      *
117      * @param value Indicates a bool input parameter.
118      */
119     PREF_API_EXPORT PreferencesValue(bool value);
120 
121     /**
122      * @brief Constructor.
123      *
124      * This constructor is used to convert the string input parameter to a value of type PreferencesValue.
125      *
126      * @param value Indicates string input parameter.
127      */
128     PREF_API_EXPORT PreferencesValue(std::string value);
129 
130     /**
131      * @brief Constructor.
132      *
133      * This constructor is used to convert the char input parameter to a value of type PreferencesValue.
134      *
135      * @param value Indicates a char input parameter.
136      */
137     PREF_API_EXPORT PreferencesValue(const char *value);
138 
139     /**
140      * @brief Constructor.
141      *
142      * This constructor is used to convert the vector<double> input parameter to a value of type PreferencesValue.
143      *
144      * @param value Indicates a vector<double> input parameter.
145      */
146     PREF_API_EXPORT PreferencesValue(std::vector<double> value);
147 
148     /**
149      * @brief Constructor.
150      *
151      * This constructor is used to convert the vector<std::string> input parameter to a value of type PreferencesValue.
152      *
153      * @param value Indicates a vector<std::string> input parameter.
154      */
155     PREF_API_EXPORT PreferencesValue(std::vector<std::string> value);
156 
157     /**
158      * @brief Constructor.
159      *
160      * This constructor is used to convert the vector<bool> input parameter to a value of type PreferencesValue.
161      *
162      * @param value Indicates a vector<bool> input parameter.
163      */
164     PREF_API_EXPORT PreferencesValue(std::vector<bool> value);
165 
166     /**
167      * @brief Constructor.
168      *
169      * This constructor is used to convert the vector<uint8_t> input parameter to a value of type PreferencesValue.
170      *
171      * @param value Indicates a vector<uint8_t> input parameter.
172      */
173     PREF_API_EXPORT PreferencesValue(std::vector<uint8_t> value);
174 
175     PREF_API_EXPORT PreferencesValue(Object value);
176 
177     /**
178      * @brief Constructor.
179      *
180      * This constructor is used to convert the BigInt input parameter to a value of type PreferencesValue.
181      *
182      * @param value Indicates a vector<uint8_t> input parameter.
183      */
184     PREF_API_EXPORT PreferencesValue(BigInt value);
185 
186     /**
187      * @brief Move assignment operator overloaded function.
188      */
189     PREF_API_EXPORT PreferencesValue &operator=(PreferencesValue &&preferencesValue) noexcept;
190 
191     /**
192      * @brief Copy assignment operator overloaded function.
193      */
194     PreferencesValue &operator=(const PreferencesValue &preferencesValue);
195 
196     /**
197      * @brief Determines whether the int type PreferencesValue is currently used.
198      *
199      * @return Returning true means it is, false means it isn't.
200      */
201     PREF_API_EXPORT bool IsInt() const;
202 
203     /**
204      * @brief Determines whether the long type PreferencesValue is currently used.
205      *
206      * @return Returning true means it is, false means it isn't.
207      */
208     PREF_API_EXPORT bool IsLong() const;
209 
210     /**
211      * @brief Determines whether the float type PreferencesValue is currently used.
212      *
213      * @return Returning true means it is, false means it isn't.
214      */
215     PREF_API_EXPORT bool IsFloat() const;
216 
217     /**
218      * @brief Determines whether the double type PreferencesValue is currently used.
219      *
220      * @return Returning true means it is, false means it isn't.
221      */
222     PREF_API_EXPORT bool IsDouble() const;
223 
224     /**
225      * @brief Determines whether the bool type PreferencesValue is currently used.
226      *
227      * @return Returning true means it is, false means it isn't.
228      */
229     PREF_API_EXPORT bool IsBool() const;
230 
231     /**
232      * @brief Determines whether the string type PreferencesValue is currently used.
233      *
234      * @return Returning true means it is, false means it isn't.
235      */
236     PREF_API_EXPORT bool IsString() const;
237 
238     /**
239      * @brief Determines whether the string array type PreferencesValue is currently used.
240      *
241      * @return Returning true means it is, false means it isn't.
242      */
243     PREF_API_EXPORT bool IsStringArray() const;
244 
245     /**
246      * @brief Determines whether the bool array type PreferencesValue is currently used.
247      *
248      * @return Returning true means it is, false means it isn't.
249      */
250     PREF_API_EXPORT bool IsBoolArray() const;
251 
252     /**
253      * @brief Determines whether the double array type PreferencesValue is currently used.
254      *
255      * @return Returning true means it is, false means it isn't.
256      */
257     PREF_API_EXPORT bool IsDoubleArray() const;
258 
259     /**
260      * @brief Determines whether the uint8 array type PreferencesValue is currently used.
261      *
262      * @return Returning true means it is, false means it isn't.
263      */
264     PREF_API_EXPORT bool IsUint8Array() const;
265 
266     PREF_API_EXPORT bool IsObject() const;
267 
268     /**
269      * @brief Determines whether the BigInt type PreferencesValue is currently used.
270      *
271      * @return Returning true means it is, false means it isn't.
272      */
273     PREF_API_EXPORT bool IsBigInt() const;
274 
275     /**
276      * @brief Type conversion function.
277      *
278      * @return The int type PreferencesValue.
279      */
280     PREF_API_EXPORT operator int() const;
281 
282     /**
283      * @brief Type conversion function.
284      *
285      * @return Returns float type PreferencesValue.
286      */
287     PREF_API_EXPORT operator float() const;
288 
289     /**
290      * @brief Type conversion function.
291      *
292      * @return Returns double type PreferencesValue.
293      */
294     PREF_API_EXPORT operator double() const;
295 
296     /**
297      * @brief Type conversion function.
298      *
299      * @return Returns bool type PreferencesValue.
300      */
301     PREF_API_EXPORT operator bool() const;
302 
303     /**
304      * @brief Type conversion function.
305      *
306      * @return Returns int64_t type PreferencesValue.
307      */
308     PREF_API_EXPORT operator int64_t() const;
309 
310     /**
311      * @brief Type conversion function.
312      *
313      * @return Returns string type PreferencesValue.
314      */
315     PREF_API_EXPORT operator std::string() const;
316 
317     /**
318      * @brief Type conversion function.
319      *
320      * @return Returns vector<double> type PreferencesValue.
321      */
322     PREF_API_EXPORT operator std::vector<double>() const;
323 
324     /**
325      * @brief Type conversion function.
326      *
327      * @return Returns vector<bool> type PreferencesValue.
328      */
329     PREF_API_EXPORT operator std::vector<bool>() const;
330 
331     /**
332      * @brief Type conversion function.
333      *
334      * @return Returns vector<string> type PreferencesValue.
335      */
336     PREF_API_EXPORT operator std::vector<std::string>() const;
337 
338     /**
339      * @brief Type conversion function.
340      *
341      * @return Returns vector<uint8_t> type PreferencesValue.
342      */
343     PREF_API_EXPORT operator std::vector<uint8_t>() const;
344 
345     PREF_API_EXPORT operator Object() const;
346 
347     /**
348      * @brief Type conversion function.
349      *
350      * @return Returns BigInt type PreferencesValue.
351      */
352     PREF_API_EXPORT operator BigInt() const;
353 
354     /**
355      * @brief Overloaded operator "==".
356      *
357      * This function is used to determine whether the input value is equal to the current PreferencesValue.
358      *
359      * @param value Indicates a PreferencesValue.
360      *
361      * @return Returning true means the input value is equal to the current PreferencesValue, false means it isn't.
362      */
363     PREF_API_EXPORT bool operator==(const PreferencesValue &value);
364 
365     std::variant<std::monostate, int, int64_t, float, double, bool, std::string, std::vector<std::string>,
366         std::vector<bool>, std::vector<double>, std::vector<uint8_t>, Object, BigInt> value_;
367 };
368 } // End of namespace NativePreferences
369 } // End of namespace OHOS
370 #endif // End of #ifndef PREFERENCES_VALUE_H
371