CCDictionary.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /****************************************************************************
  2. Copyright (c) 2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "deprecated/CCDictionary.h"
  23. #include <type_traits>
  24. #include "base/ccUTF8.h"
  25. #include "platform/CCFileUtils.h"
  26. #include "deprecated/CCString.h"
  27. #include "deprecated/CCBool.h"
  28. #include "deprecated/CCInteger.h"
  29. #include "deprecated/CCFloat.h"
  30. #include "deprecated/CCDouble.h"
  31. #include "deprecated/CCArray.h"
  32. using namespace std;
  33. NS_CC_BEGIN
  34. // -----------------------------------------------------------------------
  35. // DictElement
  36. DictElement::DictElement(const char* pszKey, Ref* pObject)
  37. {
  38. CCASSERT(pszKey && strlen(pszKey) > 0, "Invalid key value.");
  39. _intKey = 0;
  40. const char* pStart = pszKey;
  41. size_t len = strlen(pszKey);
  42. if (len > MAX_KEY_LEN )
  43. {
  44. char* pEnd = (char*)&pszKey[len-1];
  45. pStart = pEnd - (MAX_KEY_LEN-1);
  46. }
  47. strcpy(_strKey, pStart);
  48. _object = pObject;
  49. memset(&hh, 0, sizeof(hh));
  50. }
  51. DictElement::DictElement(intptr_t iKey, Ref* pObject)
  52. {
  53. _strKey[0] = '\0';
  54. _intKey = iKey;
  55. _object = pObject;
  56. memset(&hh, 0, sizeof(hh));
  57. }
  58. DictElement::~DictElement()
  59. {
  60. CCLOGINFO("deallocing DictElement: %p", this);
  61. }
  62. // -----------------------------------------------------------------------
  63. // __Dictionary
  64. __Dictionary::__Dictionary()
  65. : _elements(nullptr)
  66. , _dictType(kDictUnknown)
  67. {
  68. }
  69. __Dictionary::~__Dictionary()
  70. {
  71. CCLOGINFO("deallocing __Dictionary: %p", this);
  72. removeAllObjects();
  73. }
  74. unsigned int __Dictionary::count()
  75. {
  76. return HASH_COUNT(_elements);
  77. }
  78. __Array* __Dictionary::allKeys()
  79. {
  80. int iKeyCount = this->count();
  81. if (iKeyCount <= 0) return nullptr;
  82. __Array* array = __Array::createWithCapacity(iKeyCount);
  83. DictElement *pElement, *tmp;
  84. if (_dictType == kDictStr)
  85. {
  86. HASH_ITER(hh, _elements, pElement, tmp)
  87. {
  88. __String* pOneKey = new (std::nothrow) __String(pElement->_strKey);
  89. array->addObject(pOneKey);
  90. CC_SAFE_RELEASE(pOneKey);
  91. }
  92. }
  93. else if (_dictType == kDictInt)
  94. {
  95. HASH_ITER(hh, _elements, pElement, tmp)
  96. {
  97. __Integer* pOneKey = new (std::nothrow) __Integer(static_cast<int>(pElement->_intKey));
  98. array->addObject(pOneKey);
  99. CC_SAFE_RELEASE(pOneKey);
  100. }
  101. }
  102. return array;
  103. }
  104. __Array* __Dictionary::allKeysForObject(Ref* object)
  105. {
  106. int iKeyCount = this->count();
  107. if (iKeyCount <= 0) return nullptr;
  108. __Array* array = __Array::create();
  109. DictElement *pElement, *tmp;
  110. if (_dictType == kDictStr)
  111. {
  112. HASH_ITER(hh, _elements, pElement, tmp)
  113. {
  114. if (object == pElement->_object)
  115. {
  116. __String* pOneKey = new (std::nothrow) __String(pElement->_strKey);
  117. array->addObject(pOneKey);
  118. CC_SAFE_RELEASE(pOneKey);
  119. }
  120. }
  121. }
  122. else if (_dictType == kDictInt)
  123. {
  124. HASH_ITER(hh, _elements, pElement, tmp)
  125. {
  126. if (object == pElement->_object)
  127. {
  128. __Integer* pOneKey = new (std::nothrow) __Integer(static_cast<int>(pElement->_intKey));
  129. array->addObject(pOneKey);
  130. CC_SAFE_RELEASE(pOneKey);
  131. }
  132. }
  133. }
  134. return array;
  135. }
  136. Ref* __Dictionary::objectForKey(const std::string& key)
  137. {
  138. // if dictionary wasn't initialized, return nullptr directly.
  139. if (_dictType == kDictUnknown) return nullptr;
  140. // __Dictionary only supports one kind of key, string or integer.
  141. // This method uses string as key, therefore we should make sure that the key type of this __Dictionary is string.
  142. CCASSERT(_dictType == kDictStr, "this dictionary does not use string as key.");
  143. Ref* pRetObject = nullptr;
  144. DictElement *pElement = nullptr;
  145. HASH_FIND_STR(_elements, key.c_str(), pElement);
  146. if (pElement != nullptr)
  147. {
  148. pRetObject = pElement->_object;
  149. }
  150. return pRetObject;
  151. }
  152. Ref* __Dictionary::objectForKey(intptr_t key)
  153. {
  154. // if dictionary wasn't initialized, return nullptr directly.
  155. if (_dictType == kDictUnknown) return nullptr;
  156. // __Dictionary only supports one kind of key, string or integer.
  157. // This method uses integer as key, therefore we should make sure that the key type of this __Dictionary is integer.
  158. CCASSERT(_dictType == kDictInt, "this dictionary does not use integer as key.");
  159. Ref* pRetObject = nullptr;
  160. DictElement *pElement = nullptr;
  161. HASH_FIND_PTR(_elements, &key, pElement);
  162. if (pElement != nullptr)
  163. {
  164. pRetObject = pElement->_object;
  165. }
  166. return pRetObject;
  167. }
  168. const __String* __Dictionary::valueForKey(const std::string& key)
  169. {
  170. __String* pStr = dynamic_cast<__String*>(objectForKey(key));
  171. if (pStr == nullptr)
  172. {
  173. pStr = __String::create("");
  174. }
  175. return pStr;
  176. }
  177. const __String* __Dictionary::valueForKey(intptr_t key)
  178. {
  179. __String* pStr = dynamic_cast<__String*>(objectForKey(key));
  180. if (pStr == nullptr)
  181. {
  182. pStr = __String::create("");
  183. }
  184. return pStr;
  185. }
  186. void __Dictionary::setObject(Ref* pObject, const std::string& key)
  187. {
  188. CCASSERT(!key.empty() && pObject != nullptr, "Invalid Argument!");
  189. if (_dictType == kDictUnknown)
  190. {
  191. _dictType = kDictStr;
  192. }
  193. CCASSERT(_dictType == kDictStr, "this dictionary doesn't use string as key.");
  194. DictElement *pElement = nullptr;
  195. HASH_FIND_STR(_elements, key.c_str(), pElement);
  196. if (pElement == nullptr)
  197. {
  198. setObjectUnSafe(pObject, key);
  199. }
  200. else if (pElement->_object != pObject)
  201. {
  202. Ref* pTmpObj = pElement->_object;
  203. pTmpObj->retain();
  204. removeObjectForElememt(pElement);
  205. setObjectUnSafe(pObject, key);
  206. pTmpObj->release();
  207. }
  208. }
  209. void __Dictionary::setObject(Ref* pObject, intptr_t key)
  210. {
  211. CCASSERT(pObject != nullptr, "Invalid Argument!");
  212. if (_dictType == kDictUnknown)
  213. {
  214. _dictType = kDictInt;
  215. }
  216. CCASSERT(_dictType == kDictInt, "this dictionary doesn't use integer as key.");
  217. DictElement *pElement = nullptr;
  218. HASH_FIND_PTR(_elements, &key, pElement);
  219. if (pElement == nullptr)
  220. {
  221. setObjectUnSafe(pObject, key);
  222. }
  223. else if (pElement->_object != pObject)
  224. {
  225. Ref* pTmpObj = pElement->_object;
  226. pTmpObj->retain();
  227. removeObjectForElememt(pElement);
  228. setObjectUnSafe(pObject, key);
  229. pTmpObj->release();
  230. }
  231. }
  232. void __Dictionary::removeObjectForKey(const std::string& key)
  233. {
  234. if (_dictType == kDictUnknown)
  235. {
  236. return;
  237. }
  238. CCASSERT(_dictType == kDictStr, "this dictionary doesn't use string as its key");
  239. CCASSERT(!key.empty(), "Invalid Argument!");
  240. DictElement *pElement = nullptr;
  241. HASH_FIND_STR(_elements, key.c_str(), pElement);
  242. removeObjectForElememt(pElement);
  243. }
  244. void __Dictionary::removeObjectForKey(intptr_t key)
  245. {
  246. if (_dictType == kDictUnknown)
  247. {
  248. return;
  249. }
  250. CCASSERT(_dictType == kDictInt, "this dictionary doesn't use integer as its key");
  251. DictElement *pElement = nullptr;
  252. HASH_FIND_PTR(_elements, &key, pElement);
  253. removeObjectForElememt(pElement);
  254. }
  255. void __Dictionary::setObjectUnSafe(Ref* pObject, const std::string& key)
  256. {
  257. pObject->retain();
  258. DictElement* pElement = new (std::nothrow) DictElement(key.c_str(), pObject);
  259. HASH_ADD_STR(_elements, _strKey, pElement);
  260. }
  261. void __Dictionary::setObjectUnSafe(Ref* pObject, const intptr_t key)
  262. {
  263. pObject->retain();
  264. DictElement* pElement = new (std::nothrow) DictElement(key, pObject);
  265. HASH_ADD_PTR(_elements, _intKey, pElement);
  266. }
  267. void __Dictionary::removeObjectsForKeys(__Array* pKey__Array)
  268. {
  269. Ref* pObj = nullptr;
  270. CCARRAY_FOREACH(pKey__Array, pObj)
  271. {
  272. __String* pStr = static_cast<__String*>(pObj);
  273. removeObjectForKey(pStr->getCString());
  274. }
  275. }
  276. void __Dictionary::removeObjectForElememt(DictElement* pElement)
  277. {
  278. if (pElement != nullptr)
  279. {
  280. HASH_DEL(_elements, pElement);
  281. pElement->_object->release();
  282. CC_SAFE_DELETE(pElement);
  283. }
  284. }
  285. void __Dictionary::removeAllObjects()
  286. {
  287. DictElement *pElement, *tmp;
  288. HASH_ITER(hh, _elements, pElement, tmp)
  289. {
  290. HASH_DEL(_elements, pElement);
  291. pElement->_object->release();
  292. CC_SAFE_DELETE(pElement);
  293. }
  294. }
  295. Ref* __Dictionary::randomObject()
  296. {
  297. if (_dictType == kDictUnknown)
  298. {
  299. return nullptr;
  300. }
  301. Ref* key = allKeys()->getRandomObject();
  302. if (_dictType == kDictInt)
  303. {
  304. return objectForKey( static_cast<__Integer*>(key)->getValue());
  305. }
  306. else if (_dictType == kDictStr)
  307. {
  308. return objectForKey( static_cast<__String*>(key)->getCString());
  309. }
  310. else
  311. {
  312. return nullptr;
  313. }
  314. }
  315. __Dictionary* __Dictionary::create()
  316. {
  317. __Dictionary* ret = new (std::nothrow) __Dictionary();
  318. if (ret && ret->init() )
  319. {
  320. ret->autorelease();
  321. }
  322. return ret;
  323. }
  324. bool __Dictionary::init()
  325. {
  326. return true;
  327. }
  328. __Dictionary* __Dictionary::createWithDictionary(__Dictionary* srcDict)
  329. {
  330. return srcDict->clone();
  331. }
  332. static __Array* visitArray(const ValueVector& array);
  333. static __Dictionary* visitDict(const ValueMap& dict)
  334. {
  335. __Dictionary* ret = new (std::nothrow) __Dictionary();
  336. ret->init();
  337. for (auto iter = dict.begin(); iter != dict.end(); ++iter)
  338. {
  339. if (iter->second.getType() == Value::Type::MAP)
  340. {
  341. const ValueMap& subDict = iter->second.asValueMap();
  342. auto sub = visitDict(subDict);
  343. ret->setObject(sub, iter->first);
  344. sub->release();
  345. }
  346. else if (iter->second.getType() == Value::Type::VECTOR)
  347. {
  348. const ValueVector& arr = iter->second.asValueVector();
  349. auto sub = visitArray(arr);
  350. ret->setObject(sub, iter->first);
  351. sub->release();
  352. }
  353. else
  354. {
  355. auto str = new (std::nothrow) __String(iter->second.asString());
  356. ret->setObject(str, iter->first);
  357. str->release();
  358. }
  359. }
  360. return ret;
  361. }
  362. static __Array* visitArray(const ValueVector& array)
  363. {
  364. __Array* ret = new (std::nothrow) __Array();
  365. ret->init();
  366. for(const auto &value : array) {
  367. if (value.getType() == Value::Type::MAP)
  368. {
  369. const ValueMap& subDict = value.asValueMap();
  370. auto sub = visitDict(subDict);
  371. ret->addObject(sub);
  372. sub->release();
  373. }
  374. else if (value.getType() == Value::Type::VECTOR)
  375. {
  376. const ValueVector& arr = value.asValueVector();
  377. auto sub = visitArray(arr);
  378. ret->addObject(sub);
  379. sub->release();
  380. }
  381. else
  382. {
  383. auto str = new (std::nothrow) __String(value.asString());
  384. ret->addObject(str);
  385. str->release();
  386. }
  387. }
  388. return ret;
  389. }
  390. __Dictionary* __Dictionary::createWithContentsOfFileThreadSafe(const char *pFileName)
  391. {
  392. return visitDict(FileUtils::getInstance()->getValueMapFromFile(pFileName));
  393. }
  394. void __Dictionary::acceptVisitor(DataVisitor &visitor)
  395. {
  396. return visitor.visit(this);
  397. }
  398. __Dictionary* __Dictionary::createWithContentsOfFile(const char *pFileName)
  399. {
  400. auto ret = createWithContentsOfFileThreadSafe(pFileName);
  401. if (ret != nullptr)
  402. {
  403. ret->autorelease();
  404. }
  405. return ret;
  406. }
  407. static ValueMap ccdictionary_to_valuemap(__Dictionary* dict);
  408. static ValueVector ccarray_to_valuevector(__Array* arr)
  409. {
  410. ValueVector ret;
  411. Ref* obj;
  412. CCARRAY_FOREACH(arr, obj)
  413. {
  414. Value arrElement;
  415. __String* strVal = nullptr;
  416. __Dictionary* dictVal = nullptr;
  417. __Array* arrVal = nullptr;
  418. __Double* doubleVal = nullptr;
  419. __Bool* boolVal = nullptr;
  420. __Float* floatVal = nullptr;
  421. __Integer* intVal = nullptr;
  422. if ((strVal = dynamic_cast<__String *>(obj))) {
  423. arrElement = Value(strVal->getCString());
  424. } else if ((dictVal = dynamic_cast<__Dictionary*>(obj))) {
  425. arrElement = ccdictionary_to_valuemap(dictVal);
  426. } else if ((arrVal = dynamic_cast<__Array*>(obj))) {
  427. arrElement = ccarray_to_valuevector(arrVal);
  428. } else if ((doubleVal = dynamic_cast<__Double*>(obj))) {
  429. arrElement = Value(doubleVal->getValue());
  430. } else if ((floatVal = dynamic_cast<__Float*>(obj))) {
  431. arrElement = Value(floatVal->getValue());
  432. } else if ((intVal = dynamic_cast<__Integer*>(obj))) {
  433. arrElement = Value(intVal->getValue());
  434. } else if ((boolVal = dynamic_cast<__Bool*>(obj))) {
  435. arrElement = boolVal->getValue() ? Value(true) : Value(false);
  436. } else {
  437. CCASSERT(false, "the type isn't supported.");
  438. }
  439. ret.push_back(arrElement);
  440. }
  441. return ret;
  442. }
  443. static ValueMap ccdictionary_to_valuemap(__Dictionary* dict)
  444. {
  445. ValueMap ret;
  446. DictElement* pElement = nullptr;
  447. CCDICT_FOREACH(dict, pElement)
  448. {
  449. Ref* obj = pElement->getObject();
  450. __String* strVal = nullptr;
  451. __Dictionary* dictVal = nullptr;
  452. __Array* arrVal = nullptr;
  453. __Double* doubleVal = nullptr;
  454. __Bool* boolVal = nullptr;
  455. __Float* floatVal = nullptr;
  456. __Integer* intVal = nullptr;
  457. Value dictElement;
  458. if ((strVal = dynamic_cast<__String *>(obj))) {
  459. dictElement = Value(strVal->getCString());
  460. } else if ((dictVal = dynamic_cast<__Dictionary*>(obj))) {
  461. dictElement = ccdictionary_to_valuemap(dictVal);
  462. } else if ((arrVal = dynamic_cast<__Array*>(obj))) {
  463. dictElement = ccarray_to_valuevector(arrVal);
  464. } else if ((doubleVal = dynamic_cast<__Double*>(obj))) {
  465. dictElement = Value(doubleVal->getValue());
  466. } else if ((floatVal = dynamic_cast<__Float*>(obj))) {
  467. dictElement = Value(floatVal->getValue());
  468. } else if ((intVal = dynamic_cast<__Integer*>(obj))) {
  469. dictElement = Value(intVal->getValue());
  470. } else if ((boolVal = dynamic_cast<__Bool*>(obj))) {
  471. dictElement = boolVal->getValue() ? Value(true) : Value(false);
  472. } else {
  473. CCASSERT(false, "the type isn't supported.");
  474. }
  475. const char* key = pElement->getStrKey();
  476. if (key && strlen(key) > 0)
  477. {
  478. ret[key] = dictElement;
  479. }
  480. }
  481. return ret;
  482. }
  483. bool __Dictionary::writeToFile(const char *fullPath)
  484. {
  485. ValueMap dict = ccdictionary_to_valuemap(this);
  486. return FileUtils::getInstance()->writeToFile(dict, fullPath);
  487. }
  488. __Dictionary* __Dictionary::clone() const
  489. {
  490. __Dictionary* newDict = __Dictionary::create();
  491. DictElement* element = nullptr;
  492. Ref* tmpObj = nullptr;
  493. Clonable* obj = nullptr;
  494. if (_dictType == kDictInt)
  495. {
  496. DictElement* tmp = nullptr;
  497. HASH_ITER(hh, _elements, element, tmp)
  498. {
  499. obj = dynamic_cast<Clonable*>(element->getObject());
  500. if (obj)
  501. {
  502. tmpObj = dynamic_cast<Ref*>(obj->clone());
  503. if (tmpObj)
  504. {
  505. newDict->setObject(tmpObj, element->getIntKey());
  506. }
  507. }
  508. else
  509. {
  510. CCLOGWARN("%s isn't clonable.", typeid(std::remove_pointer<decltype(element->getObject())>::type).name());
  511. }
  512. }
  513. }
  514. else if (_dictType == kDictStr)
  515. {
  516. DictElement* tmp = nullptr;
  517. HASH_ITER(hh, _elements, element, tmp)
  518. {
  519. obj = dynamic_cast<Clonable*>(element->getObject());
  520. if (obj)
  521. {
  522. tmpObj = dynamic_cast<Ref*>(obj->clone());
  523. if (tmpObj)
  524. {
  525. newDict->setObject(tmpObj, element->getStrKey());
  526. }
  527. }
  528. else
  529. {
  530. CCLOGWARN("%s isn't clonable.", typeid(std::remove_pointer<decltype(element->getObject())>::type).name());
  531. }
  532. }
  533. }
  534. return newDict;
  535. }
  536. NS_CC_END