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