• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "pac_map.h"
17 
18 #include <cstdlib>
19 #include <iomanip>
20 #include <iostream>
21 #include <json/json.h>
22 #include <memory>
23 #include <regex>
24 #include <sstream>
25 #include <string>
26 
27 #include "bool_wrapper.h"
28 #include "byte_wrapper.h"
29 #include "double_wrapper.h"
30 #include "float_wrapper.h"
31 #include "int_wrapper.h"
32 #include "long_wrapper.h"
33 #include "short_wrapper.h"
34 #include "string_wrapper.h"
35 #include "user_object_base.h"
36 #include "user_object_wrapper.h"
37 #include "zchar_wrapper.h"
38 #include "array_wrapper.h"
39 #include "parcel_macro_base.h"
40 #include "string_ex.h"
41 
42 using IUserObject = OHOS::AAFwk::IUserObject;
43 using InterfaceID = OHOS::AAFwk::InterfaceID;
44 using Short = OHOS::AAFwk::Short;
45 using Integer = OHOS::AAFwk::Integer;
46 using Long = OHOS::AAFwk::Long;
47 using Boolean = OHOS::AAFwk::Boolean;
48 using Char = OHOS::AAFwk::Char;
49 using Byte = OHOS::AAFwk::Byte;
50 using Float = OHOS::AAFwk::Float;
51 using Double = OHOS::AAFwk::Double;
52 using String = OHOS::AAFwk::String;
53 
54 namespace OHOS {
55 namespace AppExecFwk {
56 namespace {
57 const int MAX_ARRAY_ALLOW_SIZE = 1024;
58 const int FLOAT_PRECISION = 7;
59 const int DOUBLE_PRECISION = 17;
60 const std::regex NUMBER_REGEX("^[-+]?([0-9]+)([.]([0-9]+))?$");
61 };  // namespace
62 
63 #define PAC_MAP_PUT_VALUE(id, iid, key, value, mapList) \
64     do {                                                \
65         RemoveData(mapList, key);                       \
66         sptr<iid> val = id::Box(value);                 \
67         (mapList).emplace(key, val);                    \
68     } while (0);
69 
70 #define PAC_MAP_GET_VALUE(id, key, value, mapList, defaultValue)      \
71     do {                                                              \
72         auto it = (mapList).find(key);                                \
73         if (it != (mapList).end()) {                                  \
74             if (id::Query(it->second.GetRefPtr()) != nullptr) {       \
75                 sptr<id> idValue = id::Query(it->second.GetRefPtr()); \
76                 value retVal = 0;                                     \
77                 idValue->GetValue(retVal);                            \
78                 return retVal;                                        \
79             }                                                         \
80             return defaultValue;                                      \
81         }                                                             \
82     } while (0);
83 
84 #define PAC_MAP_GET_STRING_VALUE(id, key, value, mapList, defaultValue) \
85     do {                                                                \
86         auto it = (mapList).find(key);                                  \
87         if (it != (mapList).end()) {                                    \
88             if (id::Query((it)->second.GetRefPtr()) != nullptr) {       \
89                 sptr<id> idValue = id::Query(it->second.GetRefPtr());   \
90                 value retVal;                                           \
91                 idValue->GetString(retVal);                             \
92                 return retVal;                                          \
93             }                                                           \
94             return defaultValue;                                        \
95         }                                                               \
96     } while (0);
97 
98 #define PAC_MAP_ADD_ARRAY(id, key, value, mapList)                                           \
99     do {                                                                                     \
100         RemoveData(mapList, key);                                                            \
101         std::size_t size = (value).size();                                                     \
102         sptr<IArray> ao = new Array(size, g_IID_##I##id);                                    \
103         for (std::size_t i = 0; i < size; i++) {                                             \
104             ao->Set(i, id::Box((value)[i]));                                                 \
105         }                                                                                    \
106         (mapList).emplace(key, sptr<IInterface>(static_cast<IInterface *>(ao.GetRefPtr()))); \
107     } while (0);
108 
109 #define GET_PAC_MAP_ARRAY(id, mapList, key, value)                                 \
110     do {                                                                           \
111         auto it = (mapList).find(key);                                             \
112         if (it != (mapList).end()) {                                               \
113             if (IArray::Query(it->second.GetRefPtr()) != nullptr) {                \
114                 if (Array::Is##id##Array(IArray::Query(it->second.GetRefPtr()))) { \
115                     auto func = [ & ](IInterface * object) {                       \
116                         if (I##id::Query(object) != nullptr) {                     \
117                             (value).push_back(id::Unbox(I##id::Query(object)));    \
118                         }                                                          \
119                     };                                                             \
120                     Array::ForEach(IArray::Query(it->second.GetRefPtr()), func);   \
121                 }                                                                  \
122             }                                                                      \
123         }                                                                          \
124     } while (0);
125 
126 template<typename IClassName, typename baseValue>
GetBaseDataValue(OHOS::AAFwk::IInterface * baseObj,Json::Value & json,int type)127 static void GetBaseDataValue(OHOS::AAFwk::IInterface *baseObj, Json::Value &json, int type)
128 {
129     IClassName *data = IClassName::Query(baseObj);
130     baseValue val = 0;
131     data->GetValue(val);
132     json["data"] = val;
133     json["type"] = type;
134 }
135 #define GET_BASE_DATA_VALUE(id, it, value, json, type)        \
136     do {                                                      \
137         I##id *data = I##id::Query((it)->second.GetRefPtr()); \
138         value val = 0;                                        \
139         data->GetValue(val);                                  \
140         (json)["data"] = val;                                 \
141         (json)["type"] = type;                                \
142     } while (0);
143 
144 template<typename RawType>
145 static std::string RawTypeToString(const RawType value, unsigned int precisionAfterPoint);
146 
147 template<typename IClassName, typename ClassName, typename baseValue>
GetBaseFloatDoubleDataValue(OHOS::AAFwk::IInterface * baseObj,Json::Value & json,int type,int precision)148 static void GetBaseFloatDoubleDataValue(OHOS::AAFwk::IInterface *baseObj, Json::Value &json, int type, int precision)
149 {
150     IClassName *data = IClassName::Query(baseObj);
151     if (data != nullptr) {
152         baseValue val = ClassName::Unbox(data);
153         json["data"] = RawTypeToString<baseValue>(val, precision);
154         json["type"] = type;
155     }
156 }
157 #define GET_BASE_FLOAT_DOUBLE_DATA_VALUE(iid, id, it, value, precision, json, type) \
158     do {                                                                            \
159         iid *data = iid::Query((it)->second);                                       \
160         if (data != nullptr) {                                                      \
161             value val = id::Unbox(data);                                            \
162             (json)["data"] = RawTypeToString<value>(val, precision);                \
163             (json)["type"] = type;                                                  \
164         }                                                                           \
165     } while (0);
166 
167 #define GET_BASE_STRING_DATA_VALUE(id, it, value, json, type) \
168     do {                                                      \
169         I##id *data = I##id::Query((it)->second.GetRefPtr()); \
170         value val;                                            \
171         data->GetString(val);                                 \
172         (json)["data"] = val;                                 \
173         (json)["type"] = type;                                \
174     } while (0);
175 
176 #define GET_BASE_LONG_DATA_VALUE(id, it, value, json, type)   \
177     do {                                                      \
178         I##id *data = I##id::Query((it)->second.GetRefPtr()); \
179         value val = 0;                                        \
180         data->GetValue(val);                                  \
181         (json)["data"] = std::to_string(val);                 \
182         (json)["type"] = type;                                \
183     } while (0);
184 
185 template<typename IClassName, typename ClassName, typename valueType>
PacmapGetArrayVal(OHOS::AAFwk::IInterface * ao,std::vector<valueType> & array)186 static void PacmapGetArrayVal(OHOS::AAFwk::IInterface *ao, std::vector<valueType> &array)
187 {
188     if (ao == nullptr) {
189         return;
190     }
191     if (IArray::Query(ao) != nullptr) {
192         auto func = [&array](AAFwk::IInterface *object) {
193             if (object != nullptr) {
194                 IClassName *value = IClassName::Query(object);
195                 if (value != nullptr) {
196                     array.emplace_back(ClassName::Unbox(value));
197                 }
198             }
199         };
200         Array::ForEach(IArray::Query(ao), func);
201     }
202 }
203 #define PAC_MAP_GET_ARRAY_VAL(idInterface, id, ao, array)                  \
204     do {                                                                   \
205         if ((ao) == nullptr) {                                             \
206             return false;                                                  \
207         }                                                                  \
208         if (IArray::Query((it)->second.GetRefPtr()) != nullptr) {          \
209             auto func = [ & ](AAFwk::IInterface * object) {                \
210                 if (object != nullptr) {                                   \
211                     idInterface *value = idInterface::Query(object);       \
212                     if (value != nullptr) {                                \
213                         (array).emplace_back(id::Unbox(value));            \
214                     }                                                      \
215                 }                                                          \
216             };                                                             \
217             Array::ForEach(IArray::Query((it)->second.GetRefPtr()), func); \
218         }                                                                  \
219     } while (0);
220 
221 using namespace OHOS::AAFwk;
222 IINTERFACE_IMPL_1(PacMap, Object, IPacMap);
223 /**
224  * @brief A replication structure with deep copy.
225  */
PacMap(const PacMap & other)226 PacMap::PacMap(const PacMap &other)
227 {
228     std::lock_guard<std::mutex> mLock(mapLock_);
229     dataList_.clear();
230     std::string str = MapListToString(other.dataList_);
231     StringToMapList(str, dataList_);
232 }
233 
~PacMap()234 PacMap::~PacMap()
235 {
236     Clear();
237 }
238 
239 /**
240  * @brief A overload operation with shallow copy.
241  */
operator =(const PacMap & other)242 PacMap &PacMap::operator=(const PacMap &other)
243 {
244     if (&other != this) {
245         dataList_.clear();
246         std::string str = MapListToString(other.dataList_);
247         StringToMapList(str, dataList_);
248     }
249     return *this;
250 }
251 
252 /**
253  * @brief Clear all key-value pairs and free resources.
254  */
Clear(void)255 void PacMap::Clear(void)
256 {
257     std::lock_guard<std::mutex> mLock(mapLock_);
258     dataList_.clear();
259 }
260 
261 /**
262  * @brief Creates and returns a copy of this object with shallow copy.
263  *
264  * @return A clone of this instance.
265  */
Clone(void)266 PacMap PacMap::Clone(void)
267 {
268     std::lock_guard<std::mutex> mLock(mapLock_);
269     PacMap pac_map;
270     std::string currentString = MapListToString(dataList_);
271     StringToMapList(currentString, pac_map.dataList_);
272     return pac_map;
273 }
274 
275 /**
276  * @brief Creates a deep copy of this PacMap object with deep copy.
277  *
278  * @return
279  */
DeepCopy(void)280 PacMap PacMap::DeepCopy(void)
281 {
282     std::lock_guard<std::mutex> mLock(mapLock_);
283     PacMap pac_map;
284     std::string str = MapListToString(dataList_);
285     StringToMapList(str, pac_map.dataList_);
286     return pac_map;
287 }
288 
DeepCopy(PacMap & other)289 void PacMap::DeepCopy(PacMap &other)
290 {
291     std::lock_guard<std::mutex> mLock(mapLock_);
292     dataList_.clear();
293     StringToMapList(MapListToString(other.dataList_), dataList_);
294 }
295 
296 /**
297  * @brief Adds a short value matching a specified key.
298  * @param key A specified key.
299  * @param value The value that matches the specified key.
300  */
PutShortValue(const std::string & key,short value)301 void PacMap::PutShortValue(const std::string &key, short value)
302 {
303     std::lock_guard<std::mutex> mLock(mapLock_);
304     InnerPutShortValue(dataList_, key, value);
305 }
InnerPutShortValue(PacMapList & mapList,const std::string & key,short value)306 void PacMap::InnerPutShortValue(PacMapList &mapList, const std::string &key, short value)
307 {
308     PAC_MAP_PUT_VALUE(Short, IShort, key, value, mapList)
309 }
310 /**
311  * @brief Adds a integer value matching a specified key.
312  * @param key A specified key.
313  * @param value The value that matches the specified key.
314  */
PutIntValue(const std::string & key,int value)315 void PacMap::PutIntValue(const std::string &key, int value)
316 {
317     std::lock_guard<std::mutex> mLock(mapLock_);
318     InnerPutIntValue(dataList_, key, value);
319 }
InnerPutIntValue(PacMapList & mapList,const std::string & key,int value)320 void PacMap::InnerPutIntValue(PacMapList &mapList, const std::string &key, int value)
321 {
322     PAC_MAP_PUT_VALUE(Integer, IInteger, key, value, mapList)
323 }
324 /**
325  * @brief Adds a long value matching a specified key.
326  * @param key A specified key.
327  * @param value The value that matches the specified key.
328  */
PutLongValue(const std::string & key,long value)329 void PacMap::PutLongValue(const std::string &key, long value)
330 {
331     std::lock_guard<std::mutex> mLock(mapLock_);
332     InnerPutLongValue(dataList_, key, value);
333 }
InnerPutLongValue(PacMapList & mapList,const std::string & key,long value)334 void PacMap::InnerPutLongValue(PacMapList &mapList, const std::string &key, long value)
335 {
336     PAC_MAP_PUT_VALUE(Long, ILong, key, value, mapList)
337 }
338 /**
339  * @brief Adds a boolean value matching a specified key.
340  * @param key A specified key.
341  * @param value The value that matches the specified key.
342  */
PutBooleanValue(const std::string & key,bool value)343 void PacMap::PutBooleanValue(const std::string &key, bool value)
344 {
345     std::lock_guard<std::mutex> mLock(mapLock_);
346     InnerPutBooleanValue(dataList_, key, value);
347 }
InnerPutBooleanValue(PacMapList & mapList,const std::string & key,bool value)348 void PacMap::InnerPutBooleanValue(PacMapList &mapList, const std::string &key, bool value)
349 {
350     PAC_MAP_PUT_VALUE(Boolean, IBoolean, key, value, mapList)
351 }
352 /**
353  * @brief Adds a char value matching a specified key.
354  * @param key A specified key.
355  * @param value The value that matches the specified key.
356  */
PutCharValue(const std::string & key,char value)357 void PacMap::PutCharValue(const std::string &key, char value)
358 {
359     std::lock_guard<std::mutex> mLock(mapLock_);
360     InnerPutCharValue(dataList_, key, value);
361 }
InnerPutCharValue(PacMapList & mapList,const std::string & key,char value)362 void PacMap::InnerPutCharValue(PacMapList &mapList, const std::string &key, char value)
363 {
364     PAC_MAP_PUT_VALUE(Char, IChar, key, value, mapList)
365 }
366 
367 /**
368  * @brief Adds a byte value matching a specified key.
369  * @param key A specified key.
370  * @param value The value that matches the specified key.
371  */
PutByteValue(const std::string & key,AAFwk::byte value)372 void PacMap::PutByteValue(const std::string &key, AAFwk::byte value)
373 {
374     std::lock_guard<std::mutex> mLock(mapLock_);
375     InnerPutByteValue(dataList_, key, value);
376 }
InnerPutByteValue(PacMapList & mapList,const std::string & key,AAFwk::byte value)377 void PacMap::InnerPutByteValue(PacMapList &mapList, const std::string &key, AAFwk::byte value)
378 {
379     PAC_MAP_PUT_VALUE(Byte, IByte, key, value, mapList)
380 }
381 /**
382  * @brief Adds a float value matching a specified key.
383  * @param key A specified key.
384  * @param value The value that matches the specified key.
385  */
PutFloatValue(const std::string & key,float value)386 void PacMap::PutFloatValue(const std::string &key, float value)
387 {
388     std::lock_guard<std::mutex> mLock(mapLock_);
389     InnerPutFloatValue(dataList_, key, value);
390 }
InnerPutFloatValue(PacMapList & mapList,const std::string & key,float value)391 void PacMap::InnerPutFloatValue(PacMapList &mapList, const std::string &key, float value)
392 {
393     PAC_MAP_PUT_VALUE(Float, IFloat, key, value, mapList)
394 }
395 
396 /**
397  * @brief Adds a double value matching a specified key.
398  * @param key A specified key.
399  * @param value The value that matches the specified key.
400  */
PutDoubleValue(const std::string & key,double value)401 void PacMap::PutDoubleValue(const std::string &key, double value)
402 {
403     std::lock_guard<std::mutex> mLock(mapLock_);
404     InnerPutDoubleValue(dataList_, key, value);
405 }
InnerPutDoubleValue(PacMapList & mapList,const std::string & key,double value)406 void PacMap::InnerPutDoubleValue(PacMapList &mapList, const std::string &key, double value)
407 {
408     PAC_MAP_PUT_VALUE(Double, IDouble, key, value, mapList)
409 }
410 /**
411  * @brief Adds a string {std::string} value matching a specified key.
412  * @param key A specified key.
413  * @param value The value that matches the specified key.
414  */
PutStringValue(const std::string & key,const std::string & value)415 void PacMap::PutStringValue(const std::string &key, const std::string &value)
416 {
417     std::lock_guard<std::mutex> mLock(mapLock_);
418     InnerPutStringValue(dataList_, key, value);
419 }
InnerPutStringValue(PacMapList & mapList,const std::string & key,const std::string & value)420 void PacMap::InnerPutStringValue(PacMapList &mapList, const std::string &key, const std::string &value)
421 {
422     PAC_MAP_PUT_VALUE(String, IString, key, value, mapList);
423 }
424 
425 /**
426  * @brief Adds an object value matching a specified key. The object must be a subclass of UserObjectBase.
427  * @param key A specified key.
428  * @param value A smart pointer to the object that matches the specified key.
429  */
PutObject(const std::string & key,const std::shared_ptr<UserObjectBase> & value)430 void PacMap::PutObject(const std::string &key, const std::shared_ptr<UserObjectBase> &value)
431 {
432     if (value == nullptr) {
433         return;
434     }
435     std::lock_guard<std::mutex> mLock(mapLock_);
436     InnerPutObject(dataList_, key, value);
437 }
InnerPutObject(PacMapList & mapList,const std::string & key,const std::shared_ptr<UserObjectBase> & value)438 void PacMap::InnerPutObject(PacMapList &mapList, const std::string &key, const std::shared_ptr<UserObjectBase> &value)
439 {
440     RemoveData(mapList, key);
441     sptr<IUserObject> valObject = OHOS::AAFwk::UserObject::Box(value);
442     if (valObject == nullptr) {
443         return;
444     }
445     mapList.emplace(key, valObject);
446 }
447 /**
448  * @brief Adds an PacMap value matching a specified key.
449  * @param key A specified key.
450  * @param value The value that matches the specified key.
451  */
PutPacMap(const std::string & key,const PacMap & value)452 bool PacMap::PutPacMap(const std::string &key, const PacMap &value)
453 {
454     std::lock_guard<std::mutex> mLock(mapLock_);
455     return InnerPutPacMap(dataList_, key, const_cast<PacMap &>(value));
456 }
InnerPutPacMap(PacMapList & mapList,const std::string & key,PacMap & value)457 bool PacMap::InnerPutPacMap(PacMapList &mapList, const std::string &key, PacMap &value)
458 {
459     RemoveData(mapList, key);
460     sptr<IPacMap> pacMap = new (std::nothrow) PacMap(value);
461     if (pacMap != nullptr) {
462         mapList.emplace(key, pacMap);
463         return true;
464     }
465     return false;
466 }
467 /**
468  * @brief Adds some short values matching a specified key.
469  * @param key A specified key.
470  * @param value Store a list of short values.
471  */
PutShortValueArray(const std::string & key,const std::vector<short> & value)472 void PacMap::PutShortValueArray(const std::string &key, const std::vector<short> &value)
473 {
474     std::lock_guard<std::mutex> mLock(mapLock_);
475     InnerPutShortValueArray(dataList_, key, value);
476 }
InnerPutShortValueArray(PacMapList & mapList,const std::string & key,const std::vector<short> & value)477 void PacMap::InnerPutShortValueArray(PacMapList &mapList, const std::string &key, const std::vector<short> &value)
478 {
479     PAC_MAP_ADD_ARRAY(Short, key, value, mapList)
480 }
481 /**
482  * @brief Adds some integer values matching a specified key.
483  * @param key A specified key.
484  * @param value Store a list of integer values.
485  */
PutIntValueArray(const std::string & key,const std::vector<int> & value)486 void PacMap::PutIntValueArray(const std::string &key, const std::vector<int> &value)
487 {
488     std::lock_guard<std::mutex> mLock(mapLock_);
489     InnerPutIntValueArray(dataList_, key, value);
490 }
InnerPutIntValueArray(PacMapList & mapList,const std::string & key,const std::vector<int> & value)491 void PacMap::InnerPutIntValueArray(PacMapList &mapList, const std::string &key, const std::vector<int> &value)
492 {
493     PAC_MAP_ADD_ARRAY(Integer, key, value, mapList)
494 }
495 /**
496  * @brief Adds some long values matching a specified key.
497  * @param key A specified key.
498  * @param value Store a list of long values.
499  */
PutLongValueArray(const std::string & key,const std::vector<long> & value)500 void PacMap::PutLongValueArray(const std::string &key, const std::vector<long> &value)
501 {
502     std::lock_guard<std::mutex> mLock(mapLock_);
503     InnerPutLongValueArray(dataList_, key, value);
504 }
InnerPutLongValueArray(PacMapList & mapList,const std::string & key,const std::vector<long> & value)505 void PacMap::InnerPutLongValueArray(PacMapList &mapList, const std::string &key, const std::vector<long> &value)
506 {
507     PAC_MAP_ADD_ARRAY(Long, key, value, mapList)
508 }
509 /**
510  * @brief Adds some boolean values matching a specified key.
511  * @param key A specified key.
512  * @param value Store a list of boolean values.
513  */
PutBooleanValueArray(const std::string & key,const std::vector<bool> & value)514 void PacMap::PutBooleanValueArray(const std::string &key, const std::vector<bool> &value)
515 {
516     std::lock_guard<std::mutex> mLock(mapLock_);
517     InnerPutBooleanValueArray(dataList_, key, value);
518 }
InnerPutBooleanValueArray(PacMapList & mapList,const std::string & key,const std::vector<bool> & value)519 void PacMap::InnerPutBooleanValueArray(PacMapList &mapList, const std::string &key, const std::vector<bool> &value)
520 {
521     PAC_MAP_ADD_ARRAY(Boolean, key, value, mapList)
522 }
523 /**
524  * @brief Adds some char values matching a specified key.
525  * @param key A specified key.
526  * @param value Store a list of char values.
527  */
PutCharValueArray(const std::string & key,const std::vector<char> & value)528 void PacMap::PutCharValueArray(const std::string &key, const std::vector<char> &value)
529 {
530     std::lock_guard<std::mutex> mLock(mapLock_);
531     InnerPutCharValueArray(dataList_, key, value);
532 }
InnerPutCharValueArray(PacMapList & mapList,const std::string & key,const std::vector<char> & value)533 void PacMap::InnerPutCharValueArray(PacMapList &mapList, const std::string &key, const std::vector<char> &value)
534 {
535     PAC_MAP_ADD_ARRAY(Char, key, value, mapList)
536 }
537 /**
538  * @brief Adds some byte values matching a specified key.
539  * @param key A specified key.
540  * @param value Store a list of byte values.
541  */
PutByteValueArray(const std::string & key,const std::vector<AAFwk::byte> & value)542 void PacMap::PutByteValueArray(const std::string &key, const std::vector<AAFwk::byte> &value)
543 {
544     std::lock_guard<std::mutex> mLock(mapLock_);
545     InnerPutByteValueArray(dataList_, key, value);
546 }
InnerPutByteValueArray(PacMapList & mapList,const std::string & key,const std::vector<AAFwk::byte> & value)547 void PacMap::InnerPutByteValueArray(PacMapList &mapList, const std::string &key, const std::vector<AAFwk::byte> &value)
548 {
549     PAC_MAP_ADD_ARRAY(Byte, key, value, mapList)
550 }
551 /**
552  * @brief Adds some float values matching a specified key.
553  * @param key A specified key.
554  * @param value Store a list of float values.
555  */
PutFloatValueArray(const std::string & key,const std::vector<float> & value)556 void PacMap::PutFloatValueArray(const std::string &key, const std::vector<float> &value)
557 {
558     std::lock_guard<std::mutex> mLock(mapLock_);
559     InnerPutFloatValueArray(dataList_, key, value);
560 }
InnerPutFloatValueArray(PacMapList & mapList,const std::string & key,const std::vector<float> & value)561 void PacMap::InnerPutFloatValueArray(PacMapList &mapList, const std::string &key, const std::vector<float> &value)
562 {
563     PAC_MAP_ADD_ARRAY(Float, key, value, mapList)
564 }
565 /**
566  * @brief Adds some double values matching a specified key.
567  * @param key A specified key.
568  * @param value Store a list of double values.
569  */
PutDoubleValueArray(const std::string & key,const std::vector<double> & value)570 void PacMap::PutDoubleValueArray(const std::string &key, const std::vector<double> &value)
571 {
572     std::lock_guard<std::mutex> mLock(mapLock_);
573     InnerPutDoubleValueArray(dataList_, key, value);
574 }
InnerPutDoubleValueArray(PacMapList & mapList,const std::string & key,const std::vector<double> & value)575 void PacMap::InnerPutDoubleValueArray(PacMapList &mapList, const std::string &key, const std::vector<double> &value)
576 {
577     PAC_MAP_ADD_ARRAY(Double, key, value, mapList)
578 }
579 /**
580  * @brief Adds some string {std::string} values matching a specified key.
581  * @param key A specified key.
582  * @param value Store a list of string values.
583  */
PutStringValueArray(const std::string & key,const std::vector<std::string> & value)584 void PacMap::PutStringValueArray(const std::string &key, const std::vector<std::string> &value)
585 {
586     std::lock_guard<std::mutex> mLock(mapLock_);
587     InnerPutStringValueArray(dataList_, key, value);
588 }
InnerPutStringValueArray(PacMapList & mapList,const std::string & key,const std::vector<std::string> & value)589 void PacMap::InnerPutStringValueArray(
590     PacMapList &mapList, const std::string &key, const std::vector<std::string> &value)
591 {
592     PAC_MAP_ADD_ARRAY(String, key, value, mapList)
593 }
594 /**
595  * @brief Inserts all key-value pairs of a map object into the built-in data object.
596  * Duplicate key values will be replaced.
597  * @param mapData Store a list of key-value pairs.
598  */
PutAll(std::map<std::string,PacMapObject::INTERFACE> & mapData)599 void PacMap::PutAll(std::map<std::string, PacMapObject::INTERFACE> &mapData)
600 {
601     std::lock_guard<std::mutex> mLock(mapLock_);
602     dataList_.clear();
603     StringToMapList(MapListToString(mapData), dataList_);
604 }
605 
606 /**
607  * @brief Saves the data in a PacMap object to the current object. Duplicate key values will be replaced.
608  * @param pacMap Store the date of PacMap.
609  */
PutAll(PacMap & pacMap)610 void PacMap::PutAll(PacMap &pacMap)
611 {
612     std::lock_guard<std::mutex> mLock(mapLock_);
613     dataList_.clear();
614     StringToMapList(MapListToString(pacMap.dataList_), dataList_);
615 }
616 
617 /**
618  * @brief Obtains the int value matching a specified key.
619  * @param key A specified key.
620  * @param defaultValue The return value when the function fails.
621  * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
622  */
GetIntValue(const std::string & key,int defaultValue)623 int PacMap::GetIntValue(const std::string &key, int defaultValue)
624 {
625     std::lock_guard<std::mutex> mLock(mapLock_);
626     PAC_MAP_GET_VALUE(IInteger, key, int, dataList_, defaultValue)
627     return defaultValue;
628 }
629 
630 /**
631  * @brief Obtains the short value matching a specified key.
632  * @param key A specified key.
633  * @param defaultValue The return value when the function fails.
634  * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
635  */
GetShortValue(const std::string & key,short defaultValue)636 short PacMap::GetShortValue(const std::string &key, short defaultValue)
637 {
638     std::lock_guard<std::mutex> mLock(mapLock_);
639     PAC_MAP_GET_VALUE(IShort, key, short, dataList_, defaultValue)
640     return defaultValue;
641 }
642 
643 /**
644  * @brief Obtains the boolean value matching a specified key.
645  * @param key A specified key.
646  * @param defaultValue The return value when the function fails.
647  * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
648  */
GetBooleanValue(const std::string & key,bool defaultValue)649 bool PacMap::GetBooleanValue(const std::string &key, bool defaultValue)
650 {
651     std::lock_guard<std::mutex> mLock(mapLock_);
652     PAC_MAP_GET_VALUE(IBoolean, key, bool, dataList_, defaultValue)
653     return defaultValue;
654 }
655 
656 /**
657  * @brief Obtains the long value matching a specified key.
658  * @param key A specified key.
659  * @param defaultValue The return value when the function fails.
660  * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
661  */
GetLongValue(const std::string & key,long defaultValue)662 long PacMap::GetLongValue(const std::string &key, long defaultValue)
663 {
664     std::lock_guard<std::mutex> mLock(mapLock_);
665     PAC_MAP_GET_VALUE(ILong, key, long, dataList_, defaultValue)
666     return defaultValue;
667 }
668 
669 /**
670  * @brief Obtains the char value matching a specified key.
671  * @param key A specified key.
672  * @param defaultValue The return value when the function fails.
673  * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
674  */
GetCharValue(const std::string & key,char defaultValue)675 char PacMap::GetCharValue(const std::string &key, char defaultValue)
676 {
677     std::lock_guard<std::mutex> mLock(mapLock_);
678     PAC_MAP_GET_VALUE(IChar, key, zchar, dataList_, defaultValue)
679     return defaultValue;
680 }
681 
682 /**
683  * @brief Obtains the byte value matching a specified key.
684  * @param key A specified key.
685  * @param defaultValue The return value when the function fails.
686  * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
687  */
GetByteValue(const std::string & key,AAFwk::byte defaultValue)688 AAFwk::byte PacMap::GetByteValue(const std::string &key, AAFwk::byte defaultValue)
689 {
690     std::lock_guard<std::mutex> mLock(mapLock_);
691     PAC_MAP_GET_VALUE(IByte, key, byte, dataList_, defaultValue)
692     return defaultValue;
693 }
694 
695 /**
696  * @brief Obtains the float value matching a specified key.
697  * @param key A specified key.
698  * @param defaultValue The return value when the function fails.
699  * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
700  */
GetFloatValue(const std::string & key,float defaultValue)701 float PacMap::GetFloatValue(const std::string &key, float defaultValue)
702 {
703     std::lock_guard<std::mutex> mLock(mapLock_);
704     PAC_MAP_GET_VALUE(IFloat, key, float, dataList_, defaultValue)
705     return defaultValue;
706 }
707 
708 /**
709  * @brief Obtains the double value matching a specified key.
710  * @param key A specified key.
711  * @param defaultValue The return value when the function fails.
712  * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
713  */
GetDoubleValue(const std::string & key,double defaultValue)714 double PacMap::GetDoubleValue(const std::string &key, double defaultValue)
715 {
716     std::lock_guard<std::mutex> mLock(mapLock_);
717     PAC_MAP_GET_VALUE(IDouble, key, double, dataList_, defaultValue)
718     return defaultValue;
719 }
720 
721 /**
722  * @brief Obtains the string {std::string} value matching a specified key.
723  * @param key A specified key.
724  * @param defaultValue The return value when the function fails.
725  * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
726  */
GetStringValue(const std::string & key,const std::string & defaultValue)727 std::string PacMap::GetStringValue(const std::string &key, const std::string &defaultValue)
728 {
729     std::lock_guard<std::mutex> mLock(mapLock_);
730     PAC_MAP_GET_STRING_VALUE(IString, key, std::string, dataList_, defaultValue)
731     return defaultValue;
732 }
733 
734 /**
735  * @brief Obtains some int values matching a specified key.
736  * @param key A specified key.
737  * @param value Save the returned int values.
738  */
GetIntValueArray(const std::string & key,std::vector<int> & value)739 void PacMap::GetIntValueArray(const std::string &key, std::vector<int> &value)
740 {
741     std::lock_guard<std::mutex> mLock(mapLock_);
742     GET_PAC_MAP_ARRAY(Integer, dataList_, key, value)
743 }
744 
745 /**
746  * @brief Obtains some short values matching a specified key.
747  * @param key A specified key.
748  * @param value Save the returned short values.
749  */
GetShortValueArray(const std::string & key,std::vector<short> & value)750 void PacMap::GetShortValueArray(const std::string &key, std::vector<short> &value)
751 {
752     std::lock_guard<std::mutex> mLock(mapLock_);
753     GET_PAC_MAP_ARRAY(Short, dataList_, key, value)
754 }
755 /**
756  * @brief Obtains some boolean values matching a specified key.
757  * @param key A specified key.
758  * @param value Save the returned boolean values.
759  */
GetBooleanValueArray(const std::string & key,std::vector<bool> & value)760 void PacMap::GetBooleanValueArray(const std::string &key, std::vector<bool> &value)
761 {
762     std::lock_guard<std::mutex> mLock(mapLock_);
763     GET_PAC_MAP_ARRAY(Boolean, dataList_, key, value)
764 }
765 
766 /**
767  * @brief Obtains some long values matching a specified key.
768  * @param key A specified key.
769  * @param value Save the returned long values.
770  */
GetLongValueArray(const std::string & key,std::vector<long> & value)771 void PacMap::GetLongValueArray(const std::string &key, std::vector<long> &value)
772 {
773     std::lock_guard<std::mutex> mLock(mapLock_);
774     GET_PAC_MAP_ARRAY(Long, dataList_, key, value)
775 }
776 
777 /**
778  * @brief Obtains some char values matching a specified key.
779  * @param key A specified key.
780  * @param value Save the returned char values.
781  */
GetCharValueArray(const std::string & key,std::vector<char> & value)782 void PacMap::GetCharValueArray(const std::string &key, std::vector<char> &value)
783 {
784     std::lock_guard<std::mutex> mLock(mapLock_);
785     GET_PAC_MAP_ARRAY(Char, dataList_, key, value)
786 }
787 
788 /**
789  * @brief Obtains some byte values matching a specified key.
790  * @param key A specified key.
791  * @param value Save the returned byte values.
792  */
GetByteValueArray(const std::string & key,std::vector<AAFwk::byte> & value)793 void PacMap::GetByteValueArray(const std::string &key, std::vector<AAFwk::byte> &value)
794 {
795     std::lock_guard<std::mutex> mLock(mapLock_);
796     GET_PAC_MAP_ARRAY(Byte, dataList_, key, value)
797 }
798 
799 /**
800  * @brief Obtains some float values matching a specified key.
801  * @param key A specified key.
802  * @param value Save the returned float values.
803  */
GetFloatValueArray(const std::string & key,std::vector<float> & value)804 void PacMap::GetFloatValueArray(const std::string &key, std::vector<float> &value)
805 {
806     std::lock_guard<std::mutex> mLock(mapLock_);
807     GET_PAC_MAP_ARRAY(Float, dataList_, key, value)
808 }
809 
810 /**
811  * @brief Obtains some double values matching a specified key.
812  * @param key A specified key.
813  * @param value Save the returned double values.
814  */
GetDoubleValueArray(const std::string & key,std::vector<double> & value)815 void PacMap::GetDoubleValueArray(const std::string &key, std::vector<double> &value)
816 {
817     std::lock_guard<std::mutex> mLock(mapLock_);
818     GET_PAC_MAP_ARRAY(Double, dataList_, key, value)
819 }
820 
821 /**
822  * @brief Obtains some string {std::string} values matching a specified key.
823  * @param key A specified key.
824  * @param value Save the returned string {std::string} values.
825  */
GetStringValueArray(const std::string & key,std::vector<std::string> & value)826 void PacMap::GetStringValueArray(const std::string &key, std::vector<std::string> &value)
827 {
828     std::lock_guard<std::mutex> mLock(mapLock_);
829     GET_PAC_MAP_ARRAY(String, dataList_, key, value)
830 }
831 
832 /**
833  * @brief Obtains the object matching a specified key.
834  * @param key A specified key.
835  * @return Returns the smart pointer to object that matches the key.
836  */
GetObject(const std::string & key)837 std::shared_ptr<UserObjectBase> PacMap::GetObject(const std::string &key)
838 {
839     std::lock_guard<std::mutex> mLock(mapLock_);
840     auto it = dataList_.find(key);
841     if (it == dataList_.end()) {
842         return nullptr;
843     }
844 
845     if (it->second != nullptr) {
846         if (IUserObject::Query(it->second.GetRefPtr()) != nullptr) {
847             return UserObject::Unbox(static_cast<IUserObject *>(it->second.GetRefPtr()));
848         }
849     }
850 
851     return nullptr;
852 }
853 
854 /**
855  * @brief Obtains the PacMap matching a specified key.
856  * @param key A specified key.
857  * @return Returns PacMap that matches the key.
858  */
GetPacMap(const std::string & key)859 PacMap PacMap::GetPacMap(const std::string &key)
860 {
861     std::lock_guard<std::mutex> mLock(mapLock_);
862     PacMap pacmap;
863     auto it = dataList_.find(key);
864     if (it != dataList_.end()) {
865         if (IPacMap::Query(it->second.GetRefPtr()) != nullptr) {
866             pacmap.DeepCopy(*static_cast<PacMap *>(IPacMap::Query(it->second.GetRefPtr())));
867         }
868     }
869     return pacmap;
870 }
871 
872 /**
873  * @brief Obtains all the data that has been stored with shallow copy.
874  * @return Returns all data in current PacMap. There is no dependency between the returned data and
875  * the original data.
876  */
GetAll(void)877 std::map<std::string, PacMapObject::INTERFACE> PacMap::GetAll(void)
878 {
879     std::lock_guard<std::mutex> mLock(mapLock_);
880 
881     PacMapList tmpMapList;
882     StringToMapList(MapListToString(dataList_), tmpMapList);
883 
884     return tmpMapList;
885 }
886 
ShallowCopyData(PacMapList & desPacMap,const PacMapList & srcPacMap)887 void PacMap::ShallowCopyData(PacMapList &desPacMap, const PacMapList &srcPacMap)
888 {
889     desPacMap.clear();
890     for (auto it = srcPacMap.begin(); it != srcPacMap.end(); it++) {
891         desPacMap.emplace(it->first, it->second);
892     }
893 }
894 
RemoveData(PacMapList & pacMapList,const std::string & key)895 void PacMap::RemoveData(PacMapList &pacMapList, const std::string &key)
896 {
897     auto it = pacMapList.find(key);
898     if (it != pacMapList.end()) {
899         pacMapList.erase(it);
900     }
901 }
902 
EqualPacMapData(const PacMapList & leftPacMapList,const PacMapList & rightPacMapList)903 bool PacMap::EqualPacMapData(const PacMapList &leftPacMapList, const PacMapList &rightPacMapList)
904 {
905     if (leftPacMapList.size() != rightPacMapList.size()) {
906         return false;
907     }
908 
909     for (auto right = rightPacMapList.begin(); right != rightPacMapList.end(); right++) {
910         auto left = leftPacMapList.find(right->first);
911         if (left == leftPacMapList.end()) {
912             return false;
913         }
914         if (left->second.GetRefPtr() == right->second.GetRefPtr()) {
915             continue;
916         }
917 
918         // PacMap Object
919         if (IPacMap::Query(right->second.GetRefPtr()) != nullptr) {
920             auto leftMapIt = leftPacMapList.find(right->first);
921             if (leftMapIt == leftPacMapList.end()) {
922                 return false;
923             }
924             if (IPacMap::Query(leftMapIt->second.GetRefPtr()) == nullptr) {
925                 return false;
926             }
927 
928             PacMap *rightMap = static_cast<PacMap *>(IPacMap::Query(right->second.GetRefPtr()));
929             PacMap *leftMap = static_cast<PacMap *>(IPacMap::Query(leftMapIt->second.GetRefPtr()));
930             if (rightMap == nullptr || leftMap == nullptr ||
931                 !EqualPacMapData(leftMap->dataList_, rightMap->dataList_)) {
932                 return false;
933             }
934             continue;
935         }
936 
937         if (!Object::Equals(*(right->second.GetRefPtr()), *(left->second.GetRefPtr()))) {
938             return false;
939         }
940     }
941     return true;
942 }
943 
944 template<typename iid, typename id, typename value>
GetArrayData(AAFwk::IInterface * interface,std::vector<value> & array,std::function<bool (IArray *)> IsArrayfunc)945 static void GetArrayData(
946     AAFwk::IInterface *interface, std::vector<value> &array, std::function<bool(IArray *)> IsArrayfunc)
947 {
948     if (interface == nullptr) {
949         return;
950     }
951     if (IsArrayfunc(IArray::Query(interface))) {
952         auto func = [&array](IInterface *object) { array.push_back(id::Unbox(iid::Query(object))); };
953         Array::ForEach(IArray::Query(interface), func);
954     }
955 }
956 
957 template<typename iid, typename id, typename value>
CompareTwoArrayData(AAFwk::IInterface * one_interface,AAFwk::IInterface * two_interface,std::function<bool (IArray *)> IsArrayfunc)958 static bool CompareTwoArrayData(
959     AAFwk::IInterface *one_interface, AAFwk::IInterface *two_interface, std::function<bool(IArray *)> IsArrayfunc)
960 {
961     typename std::vector<value> array;
962     GetArrayData<iid, id, value>(IArray::Query(one_interface), array, IsArrayfunc);
963 
964     if (!IsArrayfunc(IArray::Query(two_interface))) {
965         return false;
966     }
967     typename std::vector<value> otherArray;
968     GetArrayData<iid, id, value>(IArray::Query(two_interface), otherArray, IsArrayfunc);
969     if (array.size() != 0 && otherArray.size() != 0 && array.size() != otherArray.size()) {
970         return false;
971     }
972     for (std::size_t i = 0; i < array.size(); i++) {
973         auto it = std::find(otherArray.begin(), otherArray.end(), array[i]);
974         if (it == array.end()) {
975             return false;
976         }
977     }
978     return true;
979 }
CompareArrayData(AAFwk::IInterface * one_interface,AAFwk::IInterface * two_interface)980 bool PacMap::CompareArrayData(AAFwk::IInterface *one_interface, AAFwk::IInterface *two_interface)
981 {
982     if (IArray::Query(one_interface) != nullptr && IArray::Query(two_interface) != nullptr) {
983         IArray *array_one = IArray::Query(one_interface);
984         IArray *array_two = IArray::Query(two_interface);
985         long size1 = 0;
986         array_one->GetLength(size1);
987         long size2 = 0;
988         array_two->GetLength(size2);
989 
990         if (size1 != 0 && size2 != 0 && size1 != size2) {
991             return false;
992         }
993         if (AAFwk::Array::IsBooleanArray(IArray::Query(one_interface))) {
994             if (!CompareTwoArrayData<AAFwk::IBoolean, AAFwk::Boolean, bool>(
995                 one_interface, two_interface, AAFwk::Array::IsBooleanArray)) {
996                 return false;
997             }
998         } else if (AAFwk::Array::IsCharArray(AAFwk::IArray::Query(one_interface))) {
999             return false;
1000         } else if (AAFwk::Array::IsByteArray(IArray::Query(one_interface))) {
1001             if (!CompareTwoArrayData<AAFwk::IByte, AAFwk::Byte, byte>(
1002                 one_interface, two_interface, AAFwk::Array::IsByteArray)) {
1003                 return false;
1004             }
1005         } else if (AAFwk::Array::IsShortArray(IArray::Query(one_interface))) {
1006             if (!CompareTwoArrayData<AAFwk::IShort, AAFwk::Short, short>(
1007                 one_interface, two_interface, AAFwk::Array::IsShortArray)) {
1008                 return false;
1009             }
1010         } else if (AAFwk::Array::IsIntegerArray(IArray::Query(one_interface))) {
1011             if (!CompareTwoArrayData<AAFwk::IInteger, AAFwk::Integer, int>(
1012                 one_interface, two_interface, AAFwk::Array::IsIntegerArray)) {
1013                 return false;
1014             }
1015         } else if (AAFwk::Array::IsLongArray(IArray::Query(one_interface))) {
1016             if (!CompareTwoArrayData<AAFwk::ILong, AAFwk::Long, long>(
1017                 one_interface, two_interface, AAFwk::Array::IsLongArray)) {
1018                 return false;
1019             }
1020         } else if (AAFwk::Array::IsFloatArray(IArray::Query(one_interface))) {
1021             if (!CompareTwoArrayData<AAFwk::IFloat, AAFwk::Float, float>(
1022                 one_interface, two_interface, AAFwk::Array::IsFloatArray)) {
1023                 return false;
1024             }
1025         } else if (AAFwk::Array::IsDoubleArray(IArray::Query(one_interface))) {
1026             if (!CompareTwoArrayData<AAFwk::IDouble, AAFwk::Double, double>(
1027                 one_interface, two_interface, AAFwk::Array::IsDoubleArray)) {
1028                 return false;
1029             }
1030         } else if (AAFwk::Array::IsStringArray(IArray::Query(one_interface))) {
1031             if (!CompareTwoArrayData<AAFwk::IString, AAFwk::String, std::string>(
1032                 one_interface, two_interface, AAFwk::Array::IsStringArray)) {
1033                 return false;
1034             }
1035         } else {
1036             return false;
1037         }
1038     }
1039     return true;
1040 }
1041 /**
1042  * @brief Indicates whether some other object is "equal to" this one.
1043  * @param pacMap The object with which to compare.
1044  * @return Returns true if this object is the same as the pacMap argument; false otherwise.
1045  */
Equals(const PacMap * pacMap)1046 bool PacMap::Equals(const PacMap *pacMap)
1047 {
1048     std::lock_guard<std::mutex> mLock(mapLock_);
1049     if (pacMap == nullptr) {
1050         return false;
1051     }
1052 
1053     if (this == pacMap) {
1054         return true;
1055     }
1056 
1057     if (dataList_.size() != pacMap->dataList_.size()) {
1058         return false;
1059     }
1060 
1061     if (!EqualPacMapData(dataList_, pacMap->dataList_)) {
1062         return false;
1063     }
1064 
1065     return true;
1066 }
1067 
Equals(const PacMap & pacMap)1068 bool PacMap::Equals(const PacMap &pacMap)
1069 {
1070     return Equals(&pacMap);
1071 }
1072 
1073 /**
1074  * @brief Checks whether the current object is empty.
1075  * @return If there is no data, return true, otherwise return false.
1076  */
IsEmpty(void) const1077 bool PacMap::IsEmpty(void) const
1078 {
1079     return dataList_.empty();
1080 }
1081 
1082 /**
1083  * @brief Obtains the number of key-value pairs stored in the current object.
1084  * @return Returns the number of key-value pairs.
1085  */
GetSize(void) const1086 int PacMap::GetSize(void) const
1087 {
1088     return dataList_.size();
1089 }
1090 
1091 /**
1092  * @brief Obtains all the keys of the current object.
1093  */
GetKeys(void)1094 const std::set<std::string> PacMap::GetKeys(void)
1095 {
1096     std::lock_guard<std::mutex> mLock(mapLock_);
1097     std::set<std::string> keys;
1098 
1099     for (auto it = dataList_.begin(); it != dataList_.end(); it++) {
1100         keys.emplace(it->first);
1101     }
1102     return keys;
1103 }
1104 
1105 /**
1106  * @brief Checks whether a specified key is contained.
1107  * @param key Indicates the key in String
1108  * @return Returns true if the key is contained; returns false otherwise.
1109  */
HasKey(const std::string & key)1110 bool PacMap::HasKey(const std::string &key)
1111 {
1112     std::lock_guard<std::mutex> mLock(mapLock_);
1113     return (dataList_.find(key) != dataList_.end());
1114 }
1115 
1116 /**
1117  * @brief Deletes a key-value pair with a specified key.
1118  * @param key Specifies the key of the deleted data.
1119  */
Remove(const std::string & key)1120 void PacMap::Remove(const std::string &key)
1121 {
1122     std::lock_guard<std::mutex> mLock(mapLock_);
1123     auto it = dataList_.find(key);
1124     if (it != dataList_.end()) {
1125         dataList_.erase(it);
1126     }
1127 }
1128 
ReadFromParcel(Parcel & parcel)1129 bool PacMap::ReadFromParcel(Parcel &parcel)
1130 {
1131     std::string value = parcel.ReadString();
1132     if (!value.empty()) {
1133         return StringToMapList(value, dataList_);
1134     } else {
1135         return true;
1136     }
1137 }
1138 
1139 /**
1140  * @brief Marshals this Sequenceable object to a Parcel.
1141  * @param parcel Indicates the Parcel object into which the Sequenceable object has been marshaled.
1142  * @return Marshals success returns true, otherwise returns false.
1143  */
Marshalling(Parcel & parcel) const1144 bool PacMap::Marshalling(Parcel &parcel) const
1145 {
1146     if (!parcel.WriteString("PACMAP")) {
1147         return false;
1148     }
1149     std::string str = MapListToString(dataList_);
1150     return parcel.WriteString(str);
1151 }
1152 
1153 /**
1154  * @brief Unmarshals this S`nceable object from a Parcel.
1155  * @param parcel Indicates the Parcel object into which the Sequenceable object has been marshaled.
1156  * @return Unmarshals success returns a smart pointer to PacMap, otherwise returns nullptr.
1157  */
Unmarshalling(Parcel & parcel)1158 PacMap *PacMap::Unmarshalling(Parcel &parcel)
1159 {
1160     std::string value = parcel.ReadString();
1161     if (value != "PACMAP") {
1162         return nullptr;
1163     }
1164     PacMap *pPacMap = new (std::nothrow) PacMap();
1165     if (pPacMap != nullptr && !pPacMap->ReadFromParcel(parcel)) {
1166         delete pPacMap;
1167         return nullptr;
1168     }
1169     return pPacMap;
1170 }
1171 
1172 /**
1173  * @brief Object serialization to string.
1174  * @return Returns the serialized string.
1175  */
ToString()1176 std::string PacMap::ToString()
1177 {
1178     std::lock_guard<std::mutex> mLock(mapLock_);
1179     return MapListToString(dataList_);
1180 }
1181 
MapListToString(const PacMapList & mapList) const1182 std::string PacMap::MapListToString(const PacMapList &mapList) const
1183 {
1184     Json::Value root;
1185     Json::Value dataObject;
1186 
1187     ToJson(mapList, dataObject);
1188     root["pacmap"] = dataObject;
1189 
1190     std::ostringstream os;
1191     Json::StreamWriterBuilder builder;
1192     builder.settings_["indentation"] = "";
1193     std::unique_ptr<Json::StreamWriter> jsonWriter(builder.newStreamWriter());
1194     if (jsonWriter != nullptr) {
1195         jsonWriter->write(root, &os);
1196         return os.str();
1197     } else {
1198         return std::string("");
1199     }
1200 }
1201 
ToJson(const PacMapList & mapList,Json::Value & dataObject) const1202 bool PacMap::ToJson(const PacMapList &mapList, Json::Value &dataObject) const
1203 {
1204     for (auto it = mapList.begin(); it != mapList.end(); it++) {
1205         Json::Value item;
1206         bool isOK = false;
1207         if (IPacMap::Query(it->second.GetRefPtr()) != nullptr) {
1208             PacMap *pacmap = static_cast<PacMap *>(IPacMap::Query(it->second.GetRefPtr()));
1209             if (pacmap == nullptr) {
1210                 continue;
1211             }
1212             Json::Value item2;
1213             isOK = pacmap->ToJson(pacmap->dataList_, item2);
1214             if (isOK) {
1215                 item["type"] = PACMAP_DATA_PACMAP;
1216                 item["data"] = item2;
1217             }
1218         } else {
1219             isOK = GetBaseJsonValue(it, item);
1220         }
1221         if (isOK) {
1222             dataObject[it->first] = item;
1223         } else {
1224             return false;
1225         }
1226     }
1227     return true;
1228 }
1229 
1230 template<typename RawType>
RawTypeToString(const RawType value,unsigned int precisionAfterPoint)1231 static std::string RawTypeToString(const RawType value, unsigned int precisionAfterPoint)
1232 {
1233     std::ostringstream out("RawTypeToString");
1234     out << std::setw(0) << std::setprecision(precisionAfterPoint) << value;
1235 
1236     std::string res = out.str();
1237     auto pos = res.find('.');
1238     if (pos == std::string::npos) {
1239         return res;
1240     }
1241 
1242     auto splitLen = pos + 1 + precisionAfterPoint;
1243     if (res.size() <= splitLen) {
1244         return res;
1245     }
1246 
1247     return res.substr(0, splitLen);
1248 }
1249 
GetBaseJsonValue(PacMapList::const_iterator & it,Json::Value & json) const1250 bool PacMap::GetBaseJsonValue(PacMapList::const_iterator &it, Json::Value &json) const
1251 {
1252     // base data  : short
1253     if (IShort::Query(it->second.GetRefPtr()) != nullptr) {
1254         GetBaseDataValue<IShort, short>(it->second.GetRefPtr(), json, PACMAP_DATA_SHORT);
1255     } else if (IInteger::Query(it->second.GetRefPtr()) != nullptr) {
1256         GetBaseDataValue<IInteger, int>(it->second.GetRefPtr(), json, PACMAP_DATA_INTEGER);
1257     } else if (ILong::Query(it->second.GetRefPtr()) != nullptr) {
1258         // long:string
1259         GET_BASE_LONG_DATA_VALUE(Long, it, long, json, PACMAP_DATA_LONG)
1260     } else if (IChar::Query(it->second.GetRefPtr()) != nullptr) {
1261         GetBaseDataValue<IChar, zchar>(it->second.GetRefPtr(), json, PACMAP_DATA_CHAR);
1262     } else if (IByte::Query(it->second.GetRefPtr()) != nullptr) {
1263         GetBaseDataValue<IByte, byte>(it->second.GetRefPtr(), json, PACMAP_DATA_BYTE);
1264     } else if (IBoolean::Query(it->second.GetRefPtr()) != nullptr) {
1265         GetBaseDataValue<IBoolean, bool>(it->second.GetRefPtr(), json, PACMAP_DATA_BOOLEAN);
1266     } else if (IFloat::Query(it->second.GetRefPtr()) != nullptr) {
1267         // base long:string
1268         GetBaseFloatDoubleDataValue<IFloat, Float, float>(
1269             it->second.GetRefPtr(), json, PACMAP_DATA_FLOAT, FLOAT_PRECISION);
1270     } else if (IDouble::Query(it->second.GetRefPtr()) != nullptr) {
1271         // base :double to string
1272         GetBaseFloatDoubleDataValue<IDouble, Double, double>(
1273             it->second.GetRefPtr(), json, PACMAP_DATA_DOUBLE, DOUBLE_PRECISION);
1274     } else if (IString::Query(it->second.GetRefPtr()) != nullptr) {
1275         GET_BASE_STRING_DATA_VALUE(String, it, std::string, json, PACMAP_DATA_STRING)
1276     } else if (IArray::Query(it->second.GetRefPtr()) != nullptr) {
1277         // array data
1278         return GetArrayJsonValue(it, json);
1279     } else if (IUserObject::Query(it->second.GetRefPtr()) != nullptr) {
1280         // Object data [UserObject--data:UserObjectBase]
1281         return GetUserObjectJsonValue(it, json);
1282     } else {
1283         return false;
1284     }
1285 
1286     return true;
1287 }
1288 
1289 // Base data: short
ToJsonArrayShort(std::vector<short> & array,Json::Value & item,int type) const1290 bool PacMap::ToJsonArrayShort(std::vector<short> &array, Json::Value &item, int type) const
1291 {
1292     if (array.size() > 0) {
1293         for (size_t i = 0; i < array.size(); i++) {
1294             item["data"].append(array[i]);
1295         }
1296         item["type"] = type;
1297         return true;
1298     }
1299     return false;
1300 }
1301 // Base data: Integer
ToJsonArrayInt(std::vector<int> & array,Json::Value & item,int type) const1302 bool PacMap::ToJsonArrayInt(std::vector<int> &array, Json::Value &item, int type) const
1303 {
1304     if (array.size() > 0) {
1305         for (size_t i = 0; i < array.size(); i++) {
1306             item["data"].append(array[i]);
1307         }
1308         item["type"] = type;
1309         return true;
1310     }
1311     return false;
1312 }
1313 // Base data: long:sting
ToJsonArrayLong(std::vector<long> & array,Json::Value & item,int type) const1314 bool PacMap::ToJsonArrayLong(std::vector<long> &array, Json::Value &item, int type) const
1315 {
1316     if (array.size() > 0) {
1317         for (size_t i = 0; i < array.size(); i++) {
1318             item["data"].append(std::to_string(array[i]));
1319         }
1320         item["type"] = type;
1321         return true;
1322     }
1323     return false;
1324 }
1325 
1326 // Base data: byte
ToJsonArrayByte(std::vector<byte> & array,Json::Value & item,int type) const1327 bool PacMap::ToJsonArrayByte(std::vector<byte> &array, Json::Value &item, int type) const
1328 {
1329     if (array.size() > 0) {
1330         for (size_t i = 0; i < array.size(); i++) {
1331             item["data"].append(array[i]);
1332         }
1333         item["type"] = type;
1334         return true;
1335     }
1336     return false;
1337 }
1338 // Base data: bool
ToJsonArrayBoolean(std::vector<bool> & array,Json::Value & item,int type) const1339 bool PacMap::ToJsonArrayBoolean(std::vector<bool> &array, Json::Value &item, int type) const
1340 {
1341     if (array.size() > 0) {
1342         for (size_t i = 0; i < array.size(); i++) {
1343             item["data"].append((int)array[i]);
1344         }
1345         item["type"] = type;
1346         return true;
1347     }
1348     return false;
1349 }
1350 // Base data: Float to string
ToJsonArrayFloat(std::vector<float> & array,Json::Value & item,int type) const1351 bool PacMap::ToJsonArrayFloat(std::vector<float> &array, Json::Value &item, int type) const
1352 {
1353     if (array.size() > 0) {
1354         for (size_t i = 0; i < array.size(); i++) {
1355             item["data"].append(RawTypeToString<float>(array[i], FLOAT_PRECISION));
1356         }
1357         item["type"] = type;
1358         return true;
1359     }
1360     return false;
1361 }
1362 // Base data: Double to string
ToJsonArrayDouble(std::vector<double> & array,Json::Value & item,int type) const1363 bool PacMap::ToJsonArrayDouble(std::vector<double> &array, Json::Value &item, int type) const
1364 {
1365     if (array.size() > 0) {
1366         for (size_t i = 0; i < array.size(); i++) {
1367             item["data"].append(RawTypeToString<double>(array[i], DOUBLE_PRECISION));
1368         }
1369         item["type"] = type;
1370         return true;
1371     }
1372     return false;
1373 }
1374 // Base data: string
ToJsonArrayString(std::vector<std::string> & array,Json::Value & item,int type) const1375 bool PacMap::ToJsonArrayString(std::vector<std::string> &array, Json::Value &item, int type) const
1376 {
1377     if (array.size() > 0) {
1378         for (size_t i = 0; i < array.size(); i++) {
1379             item["data"].append(array[i]);
1380         }
1381         item["type"] = type;
1382         return true;
1383     }
1384     return false;
1385 }
1386 
GetArrayJsonValue(PacMapList::const_iterator & it,Json::Value & json) const1387 bool PacMap::GetArrayJsonValue(PacMapList::const_iterator &it, Json::Value &json) const
1388 {
1389     if (IArray::Query(it->second.GetRefPtr()) == nullptr) {
1390         return false;
1391     }
1392     IArray *array = static_cast<IArray *>(it->second.GetRefPtr());
1393     if (array == nullptr) {
1394         return false;
1395     }
1396     if (Array::IsShortArray(array)) {
1397         std::vector<short> arrayData;
1398         PacmapGetArrayVal<AAFwk::IShort, AAFwk::Short, short>(it->second.GetRefPtr(), arrayData);
1399         return ToJsonArrayShort(arrayData, json, PACMAP_DATA_ARRAY_SHORT);
1400     } else if (Array::IsIntegerArray(array)) {
1401         std::vector<int> arrayData;
1402         PacmapGetArrayVal<AAFwk::IInteger, AAFwk::Integer, int>(it->second.GetRefPtr(), arrayData);
1403         return ToJsonArrayInt(arrayData, json, PACMAP_DATA_ARRAY_INTEGER);
1404     } else if (Array::IsLongArray(array)) {
1405         std::vector<long> arrayData;
1406         PacmapGetArrayVal<AAFwk::ILong, AAFwk::Long, long>(it->second.GetRefPtr(), arrayData);
1407         return ToJsonArrayLong(arrayData, json, PACMAP_DATA_ARRAY_LONG);
1408     } else if (Array::IsCharArray(array)) {
1409         return false;
1410     } else if (Array::IsByteArray(array)) {
1411         std::vector<byte> arrayData;
1412         PacmapGetArrayVal<AAFwk::IByte, AAFwk::Byte, byte>(it->second.GetRefPtr(), arrayData);
1413         return ToJsonArrayByte(arrayData, json, PACMAP_DATA_ARRAY_BYTE);
1414     } else if (Array::IsBooleanArray(array)) {
1415         std::vector<bool> arrayData;
1416         PacmapGetArrayVal<AAFwk::IBoolean, AAFwk::Boolean, bool>(it->second.GetRefPtr(), arrayData);
1417         return ToJsonArrayBoolean(arrayData, json, PACMAP_DATA_ARRAY_BOOLEAN);
1418     } else if (Array::IsFloatArray(array)) {
1419         std::vector<float> arrayData;
1420         PacmapGetArrayVal<AAFwk::IFloat, AAFwk::Float, float>(it->second.GetRefPtr(), arrayData);
1421         return ToJsonArrayFloat(arrayData, json, PACMAP_DATA_ARRAY_FLOAT);
1422     } else if (Array::IsDoubleArray(array)) {
1423         std::vector<double> arrayData;
1424         PacmapGetArrayVal<AAFwk::IDouble, AAFwk::Double, double>(it->second.GetRefPtr(), arrayData);
1425         return ToJsonArrayDouble(arrayData, json, PACMAP_DATA_ARRAY_DOUBLE);
1426     } else if (Array::IsStringArray(array)) {
1427         std::vector<std::string> arrayData;
1428         PacmapGetArrayVal<AAFwk::IString, AAFwk::String, std::string>(it->second.GetRefPtr(), arrayData);
1429         return ToJsonArrayString(arrayData, json, PACMAP_DATA_ARRAY_STRING);
1430     } else {
1431         return false;
1432     }
1433     return true;
1434 }
GetUserObjectJsonValue(PacMapList::const_iterator & it,Json::Value & json) const1435 bool PacMap::GetUserObjectJsonValue(PacMapList::const_iterator &it, Json::Value &json) const
1436 {
1437     std::shared_ptr<UserObjectBase> myObjectBase = UserObject::Unbox(IUserObject::Query(it->second.GetRefPtr()));
1438     if (myObjectBase == nullptr) {
1439         return false;
1440     }
1441 
1442     std::string userObjectString = myObjectBase->ToString();
1443     Json::Value objectData;
1444 
1445     json["type"] = PACMAP_DATA_USEROBJECT;
1446     json["class"] = myObjectBase->GetClassName();
1447     json["data"] = userObjectString;
1448     return true;
1449 }
1450 
1451 /**
1452  * @brief Restore pacmap from the string.
1453  * @return Return true if successful, otherwise false.
1454  */
FromString(const std::string & str)1455 bool PacMap::FromString(const std::string &str)
1456 {
1457     std::lock_guard<std::mutex> mLock(mapLock_);
1458     dataList_.clear();
1459     return StringToMapList(str, dataList_);
1460 }
1461 
StringToMapList(const std::string & str,PacMapList & mapList)1462 bool PacMap::StringToMapList(const std::string &str, PacMapList &mapList)
1463 {
1464     if (str.empty()) {
1465         return false;
1466     }
1467 
1468     JSONCPP_STRING err;
1469     Json::Value root;
1470 
1471     const int rawJsonLength = static_cast<int>(str.length());
1472     Json::CharReaderBuilder builder;
1473     std::unique_ptr<Json::CharReader> jsonReader(builder.newCharReader());
1474     if (!jsonReader->parse(str.c_str(), str.c_str() + rawJsonLength, &root, &err)) {
1475         jsonReader.reset();
1476         return false;
1477     }
1478 
1479     if (!root.isMember("pacmap")) {
1480         return false;
1481     }
1482 
1483     Json::Value dataObject = root["pacmap"];
1484     if (dataObject.isNull()) {
1485         return true;
1486     }
1487     return ParseJson(dataObject, mapList);
1488 }
1489 
ParseJson(Json::Value & data,PacMapList & mapList)1490 bool PacMap::ParseJson(Json::Value &data, PacMapList &mapList)
1491 {
1492     Json::Value::Members keyList = data.getMemberNames();
1493     if (keyList.size() == 0) {
1494         return false;
1495     }
1496     Json::Value item;
1497     for (size_t i = 0; i < keyList.size(); i++) {
1498         item = data[keyList[i]];
1499         if (!item.isNull()) {
1500             ParseJsonItem(mapList, keyList[i], item);
1501         }
1502     }
1503     return true;
1504 }
1505 
ParseJsonItem(PacMapList & mapList,const std::string & key,Json::Value & item)1506 bool PacMap::ParseJsonItem(PacMapList &mapList, const std::string &key, Json::Value &item)
1507 {
1508     // base data, object data, arry data
1509     switch (item["type"].asInt()) {
1510         case PACMAP_DATA_SHORT:
1511             InnerPutShortValue(mapList, key, item["data"].asInt());
1512             break;
1513         case PACMAP_DATA_INTEGER:
1514             InnerPutIntValue(mapList, key, item["data"].asInt());
1515             break;
1516         case PACMAP_DATA_LONG:
1517             InnerPutLongValue(mapList, key, std::atol(item["data"].asString().c_str()));
1518             break;
1519         case PACMAP_DATA_CHAR:
1520             InnerPutCharValue(mapList, key, item["data"].asInt());
1521             break;
1522         case PACMAP_DATA_BYTE:
1523             InnerPutByteValue(mapList, key, item["data"].asInt());
1524             break;
1525         case PACMAP_DATA_BOOLEAN:
1526             InnerPutBooleanValue(mapList, key, item["data"].asBool());
1527             break;
1528         case PACMAP_DATA_FLOAT:
1529             InnerPutFloatValue(mapList, key, std::atof(item["data"].asString().c_str()));
1530             break;
1531         case PACMAP_DATA_DOUBLE:
1532             InnerPutDoubleValue(mapList, key, std::atof(item["data"].asString().c_str()));
1533             break;
1534         case PACMAP_DATA_STRING:
1535             InnerPutStringValue(mapList, key, item["data"].asString());
1536             break;
1537         case PACMAP_DATA_USEROBJECT:
1538             InnerPutObjectValue(mapList, key, item);
1539             break;
1540         case PACMAP_DATA_PACMAP:
1541             InnerPutPacMapValue(mapList, key, item);
1542             break;
1543         default:
1544             return ParseJsonItemArray(mapList, key, item);
1545     }
1546     return true;
1547 }
1548 
ParseJsonItemArray(PacMapList & mapList,const std::string & key,Json::Value & item)1549 bool PacMap::ParseJsonItemArray(PacMapList &mapList, const std::string &key, Json::Value &item)
1550 {
1551     switch (item["type"].asInt()) {
1552         case PACMAP_DATA_ARRAY_SHORT:
1553             return ParseJsonItemArrayShort(mapList, key, item);
1554         case PACMAP_DATA_ARRAY_INTEGER:
1555             return ParseJsonItemArrayInteger(mapList, key, item);
1556         case PACMAP_DATA_ARRAY_LONG:
1557             return ParseJsonItemArrayLong(mapList, key, item);
1558         case PACMAP_DATA_ARRAY_CHAR:
1559             return ParseJsonItemArrayChar(mapList, key, item);
1560         case PACMAP_DATA_ARRAY_BYTE:
1561             return ParseJsonItemArrayByte(mapList, key, item);
1562         case PACMAP_DATA_ARRAY_BOOLEAN:
1563             return ParseJsonItemArrayBoolean(mapList, key, item);
1564         case PACMAP_DATA_ARRAY_FLOAT:
1565             return ParseJsonItemArrayFloat(mapList, key, item);
1566         case PACMAP_DATA_ARRAY_DOUBLE:
1567             return ParseJsonItemArrayDouble(mapList, key, item);
1568         case PACMAP_DATA_ARRAY_STRING:
1569             return ParseJsonItemArrayString(mapList, key, item);
1570         default:
1571             return false;
1572     }
1573 }
1574 
ParseJsonItemArrayShort(PacMapList & mapList,const std::string & key,Json::Value & item)1575 bool PacMap::ParseJsonItemArrayShort(PacMapList &mapList, const std::string &key, Json::Value &item)
1576 {
1577     Json::Value arrayValue = item["data"];
1578     if (arrayValue.isNull()) {
1579         return true;
1580     }
1581 
1582     if (arrayValue.size() < 0 || arrayValue.size() > MAX_ARRAY_ALLOW_SIZE) {
1583         return false;
1584     }
1585 
1586     std::vector<short> shortList;
1587     for (Json::ArrayIndex i = 0; i < arrayValue.size(); i++) {
1588         shortList.push_back(arrayValue[i].asInt());
1589     }
1590     InnerPutShortValueArray(mapList, key, shortList);
1591     return true;
1592 }
1593 
ParseJsonItemArrayInteger(PacMapList & mapList,const std::string & key,Json::Value & item)1594 bool PacMap::ParseJsonItemArrayInteger(PacMapList &mapList, const std::string &key, Json::Value &item)
1595 {
1596     Json::Value arrayValue = item["data"];
1597     if (arrayValue.isNull()) {
1598         return true;
1599     }
1600 
1601     if (arrayValue.size() < 0 || arrayValue.size() > MAX_ARRAY_ALLOW_SIZE) {
1602         return false;
1603     }
1604 
1605     std::vector<int> intList;
1606     for (Json::ArrayIndex i = 0; i < arrayValue.size(); i++) {
1607         intList.push_back(arrayValue[i].asInt());
1608     }
1609     InnerPutIntValueArray(mapList, key, intList);
1610     return true;
1611 }
1612 /**
1613  * @brief Determine whether the string content is a numeric string
1614  * @param str indicates stirng.
1615  * @return bool
1616  */
IsNumber(const std::string & str)1617 bool PacMap::IsNumber(const std::string &str)
1618 {
1619     return std::regex_match(str, NUMBER_REGEX);
1620 }
1621 
ParseJsonItemArrayLong(PacMapList & mapList,const std::string & key,Json::Value & item)1622 bool PacMap::ParseJsonItemArrayLong(PacMapList &mapList, const std::string &key, Json::Value &item)
1623 {
1624     Json::Value arrayValue = item["data"];
1625 
1626     if (arrayValue.isNull()) {
1627         return true;
1628     }
1629 
1630     if (arrayValue.size() < 0 || arrayValue.size() > MAX_ARRAY_ALLOW_SIZE) {
1631         return false;
1632     }
1633 
1634     std::vector<long> longList;
1635     for (Json::ArrayIndex i = 0; i < arrayValue.size(); i++) {
1636         if (!IsNumber(arrayValue[i].asString())) {
1637             return false;
1638         }
1639         long longVal = std::atol(arrayValue[i].asString().c_str());
1640         longList.push_back(longVal);
1641     }
1642     InnerPutLongValueArray(mapList, key, longList);
1643     return true;
1644 }
1645 
ParseJsonItemArrayChar(PacMapList & mapList,const std::string & key,Json::Value & item)1646 bool PacMap::ParseJsonItemArrayChar(PacMapList &mapList, const std::string &key, Json::Value &item)
1647 {
1648     Json::Value arrayValue = item["data"];
1649     if (arrayValue.isNull()) {
1650         return true;
1651     }
1652 
1653     if (arrayValue.size() < 0 || arrayValue.size() > MAX_ARRAY_ALLOW_SIZE) {
1654         return false;
1655     }
1656 
1657     std::vector<char> charList;
1658     for (Json::ArrayIndex i = 0; i < arrayValue.size(); i++) {
1659         charList.push_back(arrayValue[i].asInt());
1660     }
1661     InnerPutCharValueArray(mapList, key, charList);
1662     return true;
1663 }
1664 
ParseJsonItemArrayByte(PacMapList & mapList,const std::string & key,Json::Value & item)1665 bool PacMap::ParseJsonItemArrayByte(PacMapList &mapList, const std::string &key, Json::Value &item)
1666 {
1667     Json::Value arrayValue = item["data"];
1668     if (arrayValue.isNull()) {
1669         return true;
1670     }
1671 
1672     if (arrayValue.size() < 0 || arrayValue.size() > MAX_ARRAY_ALLOW_SIZE) {
1673         return false;
1674     }
1675 
1676     std::vector<AAFwk::byte> byteList;
1677     for (Json::ArrayIndex i = 0; i < arrayValue.size(); i++) {
1678         byteList.push_back(arrayValue[i].asInt());
1679     }
1680     InnerPutByteValueArray(mapList, key, byteList);
1681     return true;
1682 }
1683 
ParseJsonItemArrayBoolean(PacMapList & mapList,const std::string & key,Json::Value & item)1684 bool PacMap::ParseJsonItemArrayBoolean(PacMapList &mapList, const std::string &key, Json::Value &item)
1685 {
1686     Json::Value arrayValue = item["data"];
1687     if (arrayValue.isNull()) {
1688         return true;
1689     }
1690 
1691     if (arrayValue.size() < 0 || arrayValue.size() > MAX_ARRAY_ALLOW_SIZE) {
1692         return false;
1693     }
1694 
1695     std::vector<bool> boolList;
1696     for (Json::ArrayIndex i = 0; i < arrayValue.size(); i++) {
1697         boolList.push_back(arrayValue[i].asBool());
1698     }
1699     InnerPutBooleanValueArray(mapList, key, boolList);
1700     return true;
1701 }
1702 
ParseJsonItemArrayFloat(PacMapList & mapList,const std::string & key,Json::Value & item)1703 bool PacMap::ParseJsonItemArrayFloat(PacMapList &mapList, const std::string &key, Json::Value &item)
1704 {
1705     Json::Value arrayValue = item["data"];
1706     if (arrayValue.isNull()) {
1707         return true;
1708     }
1709 
1710     if (arrayValue.size() < 0 || arrayValue.size() > MAX_ARRAY_ALLOW_SIZE) {
1711         return false;
1712     }
1713 
1714     std::vector<float> floatList;
1715     for (Json::ArrayIndex i = 0; i < arrayValue.size(); i++) {
1716         floatList.push_back(std::atof(arrayValue[i].asString().c_str()));
1717     }
1718     InnerPutFloatValueArray(mapList, key, floatList);
1719     return true;
1720 }
1721 
ParseJsonItemArrayDouble(PacMapList & mapList,const std::string & key,Json::Value & item)1722 bool PacMap::ParseJsonItemArrayDouble(PacMapList &mapList, const std::string &key, Json::Value &item)
1723 {
1724     Json::Value arrayValue = item["data"];
1725     if (arrayValue.isNull()) {
1726         return true;
1727     }
1728 
1729     if (arrayValue.size() < 0 || arrayValue.size() > MAX_ARRAY_ALLOW_SIZE) {
1730         return false;
1731     }
1732 
1733     std::vector<double> doubleList;
1734     for (Json::ArrayIndex i = 0; i < arrayValue.size(); i++) {
1735         doubleList.push_back(std::atof(arrayValue[i].asString().c_str()));
1736     }
1737     InnerPutDoubleValueArray(mapList, key, doubleList);
1738     return true;
1739 }
1740 
ParseJsonItemArrayString(PacMapList & mapList,const std::string & key,Json::Value & item)1741 bool PacMap::ParseJsonItemArrayString(PacMapList &mapList, const std::string &key, Json::Value &item)
1742 {
1743     Json::Value arrayValue = item["data"];
1744     if (arrayValue.isNull()) {
1745         return true;
1746     }
1747 
1748     if (arrayValue.size() < 0 || arrayValue.size() > MAX_ARRAY_ALLOW_SIZE) {
1749         return false;
1750     }
1751 
1752     std::vector<std::string> stringList;
1753     for (Json::ArrayIndex i = 0; i < arrayValue.size(); i++) {
1754         stringList.push_back(arrayValue[i].asString());
1755     }
1756     InnerPutStringValueArray(mapList, key, stringList);
1757     return true;
1758 }
1759 
InnerPutObjectValue(PacMapList & mapList,const std::string & key,Json::Value & item)1760 bool PacMap::InnerPutObjectValue(PacMapList &mapList, const std::string &key, Json::Value &item)
1761 {
1762     std::string className = item["class"].asString();
1763     if (className.empty()) {
1764         return false;
1765     }
1766 
1767     UserObjectBase *userObjectIns = UserObjectBaseLoader::GetInstance().GetUserObjectByName(className);
1768     if (userObjectIns == nullptr) {
1769         return false;
1770     }
1771 
1772     std::string userObjectString = item["data"].asString();
1773     if (!userObjectString.empty()) {
1774         userObjectIns->Parse(userObjectString);
1775     }
1776 
1777     std::shared_ptr<UserObjectBase> userObject(userObjectIns);
1778     InnerPutObject(mapList, key, userObject);
1779     return true;
1780 }
1781 
InnerPutPacMapValue(PacMapList & mapList,const std::string & key,Json::Value & item)1782 bool PacMap::InnerPutPacMapValue(PacMapList &mapList, const std::string &key, Json::Value &item)
1783 {
1784     Json::Value value = item["data"];
1785 
1786     if (value.isNull()) {
1787         return false;
1788     }
1789     PacMap *p = new (std::nothrow) PacMap();
1790     if (p == nullptr) {
1791         return false;
1792     }
1793     sptr<IPacMap> sp = p;
1794     if (p->ParseJson(value, p->dataList_)) {
1795         mapList.emplace(key, sp);
1796         return true;
1797     }
1798 
1799     return false;
1800 }
1801 
Equals(IObject & other)1802 bool PacMap::Equals(IObject &other)
1803 {
1804     PacMap *otherObj = static_cast<PacMap *>(IPacMap::Query(&other));
1805     if (otherObj == nullptr) {
1806         return false;
1807     }
1808 
1809     return Equals(otherObj);
1810 }
1811 
Parse(const std::string & str)1812 sptr<IPacMap> PacMap::Parse(const std::string &str)
1813 {
1814     PacMap *pacmap = new (std::nothrow) PacMap();
1815     if (pacmap != nullptr) {
1816         pacmap->StringToMapList(str, pacmap->dataList_);
1817     }
1818     sptr<IPacMap> ret = pacmap;
1819 
1820     return ret;
1821 }
1822 }  // namespace AppExecFwk
1823 }  // namespace OHOS
1824