• 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 Move assignment operator overloaded function.
139      */
140     PREF_API_EXPORT PreferencesValue &operator=(PreferencesValue &&preferencesValue) noexcept;
141 
142     /**
143      * @brief Copy assignment operator overloaded function.
144      */
145     PreferencesValue &operator=(const PreferencesValue &preferencesValue);
146 
147     /**
148      * @brief Determines whether the int type PreferencesValue is currently used.
149      *
150      * @return Returning true means it is, false means it isn't.
151      */
152     PREF_API_EXPORT bool IsInt() const;
153 
154     /**
155      * @brief Determines whether the long type PreferencesValue is currently used.
156      *
157      * @return Returning true means it is, false means it isn't.
158      */
159     PREF_API_EXPORT bool IsLong() const;
160 
161     /**
162      * @brief Determines whether the float type PreferencesValue is currently used.
163      *
164      * @return Returning true means it is, false means it isn't.
165      */
166     PREF_API_EXPORT bool IsFloat() const;
167 
168     /**
169      * @brief Determines whether the double type PreferencesValue is currently used.
170      *
171      * @return Returning true means it is, false means it isn't.
172      */
173     PREF_API_EXPORT bool IsDouble() const;
174 
175     /**
176      * @brief Determines whether the bool type PreferencesValue is currently used.
177      *
178      * @return Returning true means it is, false means it isn't.
179      */
180     PREF_API_EXPORT bool IsBool() const;
181 
182     /**
183      * @brief Determines whether the string type PreferencesValue is currently used.
184      *
185      * @return Returning true means it is, false means it isn't.
186      */
187     PREF_API_EXPORT bool IsString() const;
188 
189     /**
190      * @brief Determines whether the string array type PreferencesValue is currently used.
191      *
192      * @return Returning true means it is, false means it isn't.
193      */
194     PREF_API_EXPORT bool IsStringArray() const;
195 
196     /**
197      * @brief Determines whether the bool array type PreferencesValue is currently used.
198      *
199      * @return Returning true means it is, false means it isn't.
200      */
201     PREF_API_EXPORT bool IsBoolArray() const;
202 
203     /**
204      * @brief Determines whether the double array type PreferencesValue is currently used.
205      *
206      * @return Returning true means it is, false means it isn't.
207      */
208     PREF_API_EXPORT bool IsDoubleArray() const;
209 
210     /**
211      * @brief Type conversion function.
212      *
213      * @return The int type PreferencesValue.
214      */
215     PREF_API_EXPORT operator int() const;
216 
217     /**
218      * @brief Type conversion function.
219      *
220      * @return Returns float type PreferencesValue.
221      */
222     PREF_API_EXPORT operator float() const;
223 
224     /**
225      * @brief Type conversion function.
226      *
227      * @return Returns double type PreferencesValue.
228      */
229     PREF_API_EXPORT operator double() const;
230 
231     /**
232      * @brief Type conversion function.
233      *
234      * @return Returns bool type PreferencesValue.
235      */
236     PREF_API_EXPORT operator bool() const;
237 
238     /**
239      * @brief Type conversion function.
240      *
241      * @return Returns int64_t type PreferencesValue.
242      */
243     PREF_API_EXPORT operator int64_t() const;
244 
245     /**
246      * @brief Type conversion function.
247      *
248      * @return Returns string type PreferencesValue.
249      */
250     PREF_API_EXPORT operator std::string() const;
251 
252     /**
253      * @brief Type conversion function.
254      *
255      * @return Returns vector<double> type PreferencesValue.
256      */
257     PREF_API_EXPORT operator std::vector<double>() const;
258 
259     /**
260      * @brief Type conversion function.
261      *
262      * @return Returns vector<bool> type PreferencesValue.
263      */
264     PREF_API_EXPORT operator std::vector<bool>() const;
265 
266     /**
267      * @brief Type conversion function.
268      *
269      * @return Returns vector<string> type PreferencesValue.
270      */
271     PREF_API_EXPORT operator std::vector<std::string>() const;
272 
273     /**
274      * @brief Overloaded operator "==".
275      *
276      * This function is used to determine whether the input value is equal to the current PreferencesValue.
277      *
278      * @param value Indicates a PreferencesValue.
279      *
280      * @return Returning true means the input value is equal to the current PreferencesValue, false means it isn't.
281      */
282     PREF_API_EXPORT bool operator==(const PreferencesValue &value);
283 
284     std::variant<int, int64_t, float, double, bool, std::string, std::vector<std::string>, std::vector<bool>,
285         std::vector<double>>
286         value_;
287 };
288 } // End of namespace NativePreferences
289 } // End of namespace OHOS
290 #endif // End of #ifndef PREFERENCES_VALUE_H
291