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