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 #include "preferences_value.h"
17 
18 namespace OHOS {
19 namespace NativePreferences {
PreferencesValue(const PreferencesValue & preferencesValue)20 PreferencesValue::PreferencesValue(const PreferencesValue &preferencesValue)
21 {
22     if (this == &preferencesValue) {
23         return;
24     }
25     value_ = preferencesValue.value_;
26 }
27 
PreferencesValue(PreferencesValue && preferencesValue)28 PreferencesValue::PreferencesValue(PreferencesValue &&preferencesValue) noexcept
29 {
30     if (this == &preferencesValue) {
31         return;
32     }
33     value_ = std::move(preferencesValue.value_);
34 }
35 
PreferencesValue(int value)36 PreferencesValue::PreferencesValue(int value)
37 {
38     value_ = value;
39 }
40 
PreferencesValue(int64_t value)41 PreferencesValue::PreferencesValue(int64_t value)
42 {
43     value_ = value;
44 }
45 
PreferencesValue(float value)46 PreferencesValue::PreferencesValue(float value)
47 {
48     value_ = value;
49 }
50 
PreferencesValue(double value)51 PreferencesValue::PreferencesValue(double value)
52 {
53     value_ = value;
54 }
55 
PreferencesValue(bool value)56 PreferencesValue::PreferencesValue(bool value)
57 {
58     value_ = value;
59 }
60 
PreferencesValue(const char * value)61 PreferencesValue::PreferencesValue(const char *value)
62 {
63     value_ = std::string(value);
64 }
65 
PreferencesValue(std::string value)66 PreferencesValue::PreferencesValue(std::string value)
67 {
68     value_ = value;
69 }
70 
PreferencesValue(std::vector<double> value)71 PreferencesValue::PreferencesValue(std::vector<double> value)
72 {
73     value_ = value;
74 }
75 
PreferencesValue(std::vector<std::string> value)76 PreferencesValue::PreferencesValue(std::vector<std::string> value)
77 {
78     value_ = value;
79 }
80 
PreferencesValue(std::vector<bool> value)81 PreferencesValue::PreferencesValue(std::vector<bool> value)
82 {
83     value_ = value;
84 }
85 
PreferencesValue(std::vector<uint8_t> value)86 PreferencesValue::PreferencesValue(std::vector<uint8_t> value)
87 {
88     value_ = value;
89 }
90 
PreferencesValue(Object value)91 PreferencesValue::PreferencesValue(Object value)
92 {
93     value_ = value;
94 }
95 
PreferencesValue(BigInt value)96 PreferencesValue::PreferencesValue(BigInt value)
97 {
98     value_ = value;
99 }
100 
operator =(PreferencesValue && preferencesValue)101 PreferencesValue &PreferencesValue::operator=(PreferencesValue &&preferencesValue) noexcept
102 {
103     if (this == &preferencesValue) {
104         return *this;
105     }
106     value_ = std::move(preferencesValue.value_);
107     return *this;
108 }
109 
operator =(const PreferencesValue & preferencesValue)110 PreferencesValue &PreferencesValue::operator=(const PreferencesValue &preferencesValue)
111 {
112     if (this == &preferencesValue) {
113         return *this;
114     }
115     value_ = preferencesValue.value_;
116     return *this;
117 }
118 
IsInt() const119 bool PreferencesValue::IsInt() const
120 {
121     return std::holds_alternative<int>(value_);
122 }
123 
IsLong() const124 bool PreferencesValue::IsLong() const
125 {
126     return std::holds_alternative<int64_t>(value_);
127 }
128 
IsFloat() const129 bool PreferencesValue::IsFloat() const
130 {
131     return std::holds_alternative<float>(value_);
132 }
133 
IsDouble() const134 bool PreferencesValue::IsDouble() const
135 {
136     return std::holds_alternative<double>(value_);
137 }
138 
IsBool() const139 bool PreferencesValue::IsBool() const
140 {
141     return std::holds_alternative<bool>(value_);
142 }
143 
IsString() const144 bool PreferencesValue::IsString() const
145 {
146     return std::holds_alternative<std::string>(value_);
147 }
148 
IsDoubleArray() const149 bool PreferencesValue::IsDoubleArray() const
150 {
151     return std::holds_alternative<std::vector<double>>(value_);
152 }
153 
IsUint8Array() const154 bool PreferencesValue::IsUint8Array() const
155 {
156     return std::holds_alternative<std::vector<uint8_t>>(value_);
157 }
158 
IsObject() const159 bool PreferencesValue::IsObject() const
160 {
161     return std::holds_alternative<Object>(value_);
162 }
163 
IsStringArray() const164 bool PreferencesValue::IsStringArray() const
165 {
166     return std::holds_alternative<std::vector<std::string>>(value_);
167 }
168 
IsBoolArray() const169 bool PreferencesValue::IsBoolArray() const
170 {
171     return std::holds_alternative<std::vector<bool>>(value_);
172 }
173 
IsBigInt() const174 bool PreferencesValue::IsBigInt() const
175 {
176     return std::holds_alternative<BigInt>(value_);
177 }
178 
179 PreferencesValue::operator int() const
180 {
181     return std::get<int>(value_);
182 }
183 
184 PreferencesValue::operator int64_t() const
185 {
186     return std::get<int64_t>(value_);
187 }
188 
189 PreferencesValue::operator float() const
190 {
191     return std::get<float>(value_);
192 }
193 
194 PreferencesValue::operator double() const
195 {
196     return std::get<double>(value_);
197 }
198 
199 PreferencesValue::operator bool() const
200 {
201     return std::get<bool>(value_);
202 }
203 
204 PreferencesValue::operator std::string() const
205 {
206     return std::get<std::string>(value_);
207 }
208 
209 PreferencesValue::operator std::vector<double>() const
210 {
211     return std::get<std::vector<double>>(value_);
212 }
213 
214 PreferencesValue::operator std::vector<bool>() const
215 {
216     return std::get<std::vector<bool>>(value_);
217 }
218 
219 PreferencesValue::operator std::vector<std::string>() const
220 {
221     return std::get<std::vector<std::string>>(value_);
222 }
223 
224 PreferencesValue::operator std::vector<uint8_t>() const
225 {
226     return std::get<std::vector<uint8_t>>(value_);
227 }
228 
229 PreferencesValue::operator Object() const
230 {
231     return std::get<Object>(value_);
232 }
233 
234 PreferencesValue::operator BigInt() const
235 {
236     return std::get<BigInt>(value_);
237 }
238 
operator ==(const PreferencesValue & value)239 bool PreferencesValue::operator==(const PreferencesValue &value)
240 {
241     return (this->value_ == value.value_);
242 }
243 } // End of namespace NativePreferences
244 } // End of namespace OHOS
245