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 "ohos/aafwk/base/array_wrapper.h"
17 #include <cstdint>
18 #include "ohos/aafwk/base/bool_wrapper.h"
19 #include "ohos/aafwk/base/zchar_wrapper.h"
20 #include "ohos/aafwk/base/byte_wrapper.h"
21 #include "ohos/aafwk/base/double_wrapper.h"
22 #include "ohos/aafwk/base/float_wrapper.h"
23 #include "ohos/aafwk/base/int_wrapper.h"
24 #include "ohos/aafwk/base/long_wrapper.h"
25 #include "ohos/aafwk/base/short_wrapper.h"
26 #include "ohos/aafwk/base/string_wrapper.h"
27
28 namespace OHOS {
29 namespace AAFwk {
30 IINTERFACE_IMPL_1(Array, Object, IArray);
31
Array(long size,const InterfaceID & id)32 Array::Array(long size, const InterfaceID &id) : size_(size), typeId_(id)
33 {
34 for (long i = 0; i < size; i++) {
35 values_.push_back(nullptr);
36 }
37 }
38
Get(long index,sptr<IInterface> & value)39 ErrCode Array::Get(long index, /* [in] */
40 sptr<IInterface> &value) /* [out] */
41 {
42 if (index < 0 || index >= size_) {
43 value = nullptr;
44 return ERR_INVALID_VALUE;
45 }
46 value = values_[index];
47 return ERR_OK;
48 }
49
GetLength(long & size)50 ErrCode Array::GetLength(long &size) /* [out] */
51 {
52 VALIDATE_NOT_NULL(&size);
53
54 size = size_;
55 return ERR_OK;
56 }
57
GetType(InterfaceID & id)58 ErrCode Array::GetType(InterfaceID &id) /* [out] */
59 {
60 VALIDATE_NOT_NULL(&id);
61
62 id = typeId_;
63 return ERR_OK;
64 }
65
Set(long index,IInterface * value)66 ErrCode Array::Set(long index, /* [in] */
67 IInterface *value) /* [in] */
68 {
69 if (index < 0 || index >= size_) {
70 return ERR_INVALID_VALUE;
71 }
72 values_[index] = value;
73 return ERR_OK;
74 }
75
Equals(IObject & other)76 bool Array::Equals(IObject &other) /* [in] */
77 {
78 Array *otherObj = static_cast<Array *>(IArray::Query(&other));
79 if (otherObj == nullptr) {
80 return false;
81 }
82
83 if (this == otherObj) {
84 return true;
85 }
86
87 if (otherObj->size_ != size_ || otherObj->typeId_ != typeId_) {
88 return false;
89 }
90
91 for (long i = 0; i < size_; i++) {
92 if (!Object::Equals(*(values_[i].GetRefPtr()), *(otherObj->values_[i].GetRefPtr()))) {
93 return false;
94 }
95 }
96 return true;
97 }
98
ToString()99 std::string Array::ToString()
100 {
101 std::string result;
102 if (typeId_ == g_IID_IString) {
103 result += String::SIGNATURE;
104 } else if (typeId_ == g_IID_IBoolean) {
105 result += Boolean::SIGNATURE;
106 } else if (typeId_ == g_IID_IByte) {
107 result += Byte::SIGNATURE;
108 } else if (typeId_ == g_IID_IShort) {
109 result += Short::SIGNATURE;
110 } else if (typeId_ == g_IID_IInteger) {
111 result += Integer::SIGNATURE;
112 } else if (typeId_ == g_IID_ILong) {
113 result += Long::SIGNATURE;
114 } else if (typeId_ == g_IID_IFloat) {
115 result += Float::SIGNATURE;
116 } else if (typeId_ == g_IID_IDouble) {
117 result += Double::SIGNATURE;
118 } else if (typeId_ == g_IID_IArray) {
119 result += Array::SIGNATURE;
120 } else if (typeId_ == g_IID_IChar) {
121 result += Char::SIGNATURE;
122 } else {
123 result += "";
124 }
125
126 result += std::to_string(size_) + "{";
127 for (long i = 0; i < size_; i++) {
128 result += Object::ToString(*(values_[i].GetRefPtr()));
129 if (i < size_ - 1) {
130 result += ",";
131 }
132 }
133 result += "}";
134 return result;
135 }
136
ParseString(const std::string & values,long size)137 sptr<IArray> Array::ParseString(const std::string &values, long size)
138 {
139 sptr<IArray> array = new (std::nothrow) Array(size, g_IID_IString);
140 if (array != nullptr) {
141 auto func = [](const std::string &str) -> sptr<IInterface> { return String::Parse(str); };
142 ParseElement(array, func, values, size);
143 }
144 return array;
145 }
146
ParseBoolean(const std::string & values,long size)147 sptr<IArray> Array::ParseBoolean(const std::string &values, long size)
148 {
149 sptr<IArray> array = new (std::nothrow) Array(size, g_IID_IBoolean);
150 if (array != nullptr) {
151 auto func = [](const std::string &str) -> sptr<IInterface> { return Boolean::Parse(str); };
152 ParseElement(array, func, values, size);
153 }
154 return array;
155 }
156
ParseByte(const std::string & values,long size)157 sptr<IArray> Array::ParseByte(const std::string &values, long size)
158 {
159 sptr<IArray> array = new (std::nothrow) Array(size, g_IID_IByte);
160 if (array != nullptr) {
161 auto func = [](const std::string &str) -> sptr<IInterface> { return Byte::Parse(str); };
162 ParseElement(array, func, values, size);
163 }
164 return array;
165 }
166
ParseShort(const std::string & values,long size)167 sptr<IArray> Array::ParseShort(const std::string &values, long size)
168 {
169 sptr<IArray> array = new (std::nothrow) Array(size, g_IID_IShort);
170 if (array != nullptr) {
171 auto func = [](const std::string &str) -> sptr<IInterface> { return Short::Parse(str); };
172 ParseElement(array, func, values, size);
173 }
174 return array;
175 }
176
ParseInteger(const std::string & values,long size)177 sptr<IArray> Array::ParseInteger(const std::string &values, long size)
178 {
179 sptr<IArray> array = new (std::nothrow) Array(size, g_IID_IInteger);
180 if (array != nullptr) {
181 auto func = [](const std::string &str) -> sptr<IInterface> { return Integer::Parse(str); };
182 ParseElement(array, func, values, size);
183 }
184 return array;
185 }
186
ParseLong(const std::string & values,long size)187 sptr<IArray> Array::ParseLong(const std::string &values, long size)
188 {
189 sptr<IArray> array = new (std::nothrow) Array(size, g_IID_ILong);
190 if (array != nullptr) {
191 auto func = [](const std::string &str) -> sptr<IInterface> { return Long::Parse(str); };
192 ParseElement(array, func, values, size);
193 }
194 return array;
195 }
196
ParseFloat(const std::string & values,long size)197 sptr<IArray> Array::ParseFloat(const std::string &values, long size)
198 {
199 sptr<IArray> array = new (std::nothrow) Array(size, g_IID_IFloat);
200 if (array != nullptr) {
201 auto func = [](const std::string &str) -> sptr<IInterface> { return Float::Parse(str); };
202 ParseElement(array, func, values, size);
203 }
204 return array;
205 }
206
ParseDouble(const std::string & values,long size)207 sptr<IArray> Array::ParseDouble(const std::string &values, long size)
208 {
209 sptr<IArray> array = new (std::nothrow) Array(size, g_IID_IDouble);
210 if (array != nullptr) {
211 auto func = [](const std::string &str) -> sptr<IInterface> { return Double::Parse(str); };
212 ParseElement(array, func, values, size);
213 }
214 return array;
215 }
ParseChar(const std::string & values,long size)216 sptr<IArray> Array::ParseChar(const std::string &values, long size)
217 {
218 sptr<IArray> array = new (std::nothrow) Array(size, g_IID_IChar);
219 if (array != nullptr) {
220 auto func = [](const std::string &str) -> sptr<IInterface> { return Char::Parse(str); };
221 ParseElement(array, func, values, size);
222 }
223 return array;
224 }
ParseArray(const std::string & values,long size)225 sptr<IArray> Array::ParseArray(const std::string &values, long size)
226 {
227 sptr<IArray> array = new (std::nothrow) Array(size, g_IID_IArray);
228 if (array != nullptr) {
229 auto func = [](const std::string &str) -> sptr<IInterface> { return Array::Parse(str); };
230 ParseElement(array, func, values, size);
231 }
232 return array;
233 }
234
Parse(const std::string & arrayStr)235 sptr<IArray> Array::Parse(const std::string &arrayStr) /* [in] */
236 {
237 char signature = arrayStr[0];
238 if (signature != String::SIGNATURE && signature != Boolean::SIGNATURE && signature != Byte::SIGNATURE &&
239 signature != Short::SIGNATURE && signature != Integer::SIGNATURE && signature != Long::SIGNATURE &&
240 signature != Float::SIGNATURE && signature != Double::SIGNATURE && signature != Array::SIGNATURE &&
241 signature != Char::SIGNATURE) {
242 return nullptr;
243 }
244
245 std::size_t idx = arrayStr.find("{");
246 if (idx == std::string::npos) {
247 return nullptr;
248 }
249 if (arrayStr[arrayStr.length() - 1] != '}') {
250 return nullptr;
251 }
252
253 long size = std::stol(arrayStr.substr(1, idx - 1));
254 idx += 1;
255 std::string values = arrayStr.substr(idx, arrayStr.length() - 1 - idx);
256
257 switch (signature) {
258 case Char::SIGNATURE:
259 return ParseChar(values, size);
260 case String::SIGNATURE:
261 return ParseString(values, size);
262 case Boolean::SIGNATURE:
263 return ParseBoolean(values, size);
264 case Byte::SIGNATURE:
265 return ParseByte(values, size);
266 case Short::SIGNATURE:
267 return ParseShort(values, size);
268 case Integer::SIGNATURE:
269 return ParseInteger(values, size);
270 case Long::SIGNATURE:
271 return ParseLong(values, size);
272 case Float::SIGNATURE:
273 return ParseFloat(values, size);
274 case Double::SIGNATURE:
275 return ParseDouble(values, size);
276 case Array::SIGNATURE:
277 return ParseArray(values, size);
278 default:
279 break;
280 }
281 return nullptr;
282 }
283
ParseElement(IArray * array,std::function<sptr<IInterface> (std::string &)> func,const std::string & values,long size)284 void Array::ParseElement(IArray *array, /* [in] */
285 std::function<sptr<IInterface>(std::string &)> func, /* [in] */
286 const std::string &values, /* [in] */
287 long size) /* [in] */
288 {
289 if (array == nullptr) {
290 return;
291 }
292
293 std::size_t beginIdx = 0;
294 std::size_t endIdx;
295 for (long i = 0; i < size; i++) {
296 std::string valueStr;
297 if (i < size - 1) {
298 endIdx = values.find(",", beginIdx);
299 if (endIdx == std::string::npos) {
300 valueStr = values.substr(beginIdx);
301 } else {
302 valueStr = values.substr(beginIdx, endIdx - beginIdx);
303 beginIdx = endIdx + 1;
304 }
305 } else {
306 valueStr = values.substr(beginIdx, values.length() - beginIdx);
307 }
308 array->Set(i, func(valueStr));
309 }
310 }
311
IsBooleanArray(IArray * array)312 bool Array::IsBooleanArray(IArray *array) /* [in] */
313 {
314 InterfaceID typeId;
315 array->GetType(typeId);
316 return typeId == g_IID_IBoolean;
317 }
318
IsCharArray(IArray * array)319 bool Array::IsCharArray(IArray *array) /* [in] */
320 {
321 InterfaceID typeId;
322 array->GetType(typeId);
323 return typeId == g_IID_IChar;
324 }
325
IsByteArray(IArray * array)326 bool Array::IsByteArray(IArray *array) /* [in] */
327 {
328 InterfaceID typeId;
329 array->GetType(typeId);
330 return typeId == g_IID_IByte;
331 }
332
IsShortArray(IArray * array)333 bool Array::IsShortArray(IArray *array) /* [in] */
334 {
335 InterfaceID typeId;
336 array->GetType(typeId);
337 return typeId == g_IID_IShort;
338 }
339
IsIntegerArray(IArray * array)340 bool Array::IsIntegerArray(IArray *array) /* [in] */
341 {
342 InterfaceID typeId;
343 array->GetType(typeId);
344 return typeId == g_IID_IInteger;
345 }
346
IsLongArray(IArray * array)347 bool Array::IsLongArray(IArray *array) /* [in] */
348 {
349 InterfaceID typeId;
350 array->GetType(typeId);
351 return typeId == g_IID_ILong;
352 }
353
IsFloatArray(IArray * array)354 bool Array::IsFloatArray(IArray *array) /* [in] */
355 {
356 InterfaceID typeId;
357 array->GetType(typeId);
358 return typeId == g_IID_IFloat;
359 }
360
IsDoubleArray(IArray * array)361 bool Array::IsDoubleArray(IArray *array) /* [in] */
362 {
363 InterfaceID typeId;
364 array->GetType(typeId);
365 return typeId == g_IID_IDouble;
366 }
367
IsStringArray(IArray * array)368 bool Array::IsStringArray(IArray *array) /* [in] */
369 {
370 InterfaceID typeId;
371 array->GetType(typeId);
372 return typeId == g_IID_IString;
373 }
374
ForEach(IArray * array,std::function<void (IInterface *)> func)375 void Array::ForEach(IArray *array, /* [in] */
376 std::function<void(IInterface *)> func) /* [in] */
377 {
378 long size;
379 array->GetLength(size);
380 for (long i = 0; i < size; i++) {
381 sptr<IInterface> object;
382 array->Get(i, object);
383 func(object);
384 }
385 }
386 } // namespace AAFwk
387 } // namespace OHOS