CCValue.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "base/CCValue.h"
  22. #include "base/ccUtils.h"
  23. #include <cmath>
  24. #include <sstream>
  25. #include <iomanip>
  26. #include <float.h>
  27. #include <stdlib.h>
  28. #include <memory.h>
  29. NS_CC_BEGIN
  30. const ValueVector ValueVectorNull;
  31. const ValueMap ValueMapNull;
  32. const ValueMapIntKey ValueMapIntKeyNull;
  33. const Value Value::Null;
  34. Value::Value()
  35. : _type(Type::NONE)
  36. {
  37. memset(&_field, 0, sizeof(_field));
  38. }
  39. Value::Value(unsigned char v)
  40. : _type(Type::BYTE)
  41. {
  42. _field.byteVal = v;
  43. }
  44. Value::Value(int v)
  45. : _type(Type::INTEGER)
  46. {
  47. _field.intVal = v;
  48. }
  49. Value::Value(unsigned int v)
  50. : _type(Type::UNSIGNED)
  51. {
  52. _field.unsignedVal = v;
  53. }
  54. Value::Value(float v)
  55. : _type(Type::FLOAT)
  56. {
  57. _field.floatVal = v;
  58. }
  59. Value::Value(double v)
  60. : _type(Type::DOUBLE)
  61. {
  62. _field.doubleVal = v;
  63. }
  64. Value::Value(bool v)
  65. : _type(Type::BOOLEAN)
  66. {
  67. _field.boolVal = v;
  68. }
  69. Value::Value(const char* v)
  70. : _type(Type::STRING)
  71. {
  72. _field.strVal = new (std::nothrow) std::string();
  73. if (v)
  74. {
  75. *_field.strVal = v;
  76. }
  77. }
  78. Value::Value(const std::string& v)
  79. : _type(Type::STRING)
  80. {
  81. _field.strVal = new (std::nothrow) std::string();
  82. *_field.strVal = v;
  83. }
  84. Value::Value(const ValueVector& v)
  85. : _type(Type::VECTOR)
  86. {
  87. _field.vectorVal = new (std::nothrow) ValueVector();
  88. *_field.vectorVal = v;
  89. }
  90. Value::Value(ValueVector&& v)
  91. : _type(Type::VECTOR)
  92. {
  93. _field.vectorVal = new (std::nothrow) ValueVector();
  94. *_field.vectorVal = std::move(v);
  95. }
  96. Value::Value(const ValueMap& v)
  97. : _type(Type::MAP)
  98. {
  99. _field.mapVal = new (std::nothrow) ValueMap();
  100. *_field.mapVal = v;
  101. }
  102. Value::Value(ValueMap&& v)
  103. : _type(Type::MAP)
  104. {
  105. _field.mapVal = new (std::nothrow) ValueMap();
  106. *_field.mapVal = std::move(v);
  107. }
  108. Value::Value(const ValueMapIntKey& v)
  109. : _type(Type::INT_KEY_MAP)
  110. {
  111. _field.intKeyMapVal = new (std::nothrow) ValueMapIntKey();
  112. *_field.intKeyMapVal = v;
  113. }
  114. Value::Value(ValueMapIntKey&& v)
  115. : _type(Type::INT_KEY_MAP)
  116. {
  117. _field.intKeyMapVal = new (std::nothrow) ValueMapIntKey();
  118. *_field.intKeyMapVal = std::move(v);
  119. }
  120. Value::Value(const Value& other)
  121. : _type(Type::NONE)
  122. {
  123. *this = other;
  124. }
  125. Value::Value(Value&& other)
  126. : _type(Type::NONE)
  127. {
  128. *this = std::move(other);
  129. }
  130. Value::~Value()
  131. {
  132. clear();
  133. }
  134. Value& Value::operator= (const Value& other)
  135. {
  136. if (this != &other) {
  137. reset(other._type);
  138. switch (other._type) {
  139. case Type::BYTE:
  140. _field.byteVal = other._field.byteVal;
  141. break;
  142. case Type::INTEGER:
  143. _field.intVal = other._field.intVal;
  144. break;
  145. case Type::UNSIGNED:
  146. _field.unsignedVal = other._field.unsignedVal;
  147. break;
  148. case Type::FLOAT:
  149. _field.floatVal = other._field.floatVal;
  150. break;
  151. case Type::DOUBLE:
  152. _field.doubleVal = other._field.doubleVal;
  153. break;
  154. case Type::BOOLEAN:
  155. _field.boolVal = other._field.boolVal;
  156. break;
  157. case Type::STRING:
  158. if (_field.strVal == nullptr)
  159. {
  160. _field.strVal = new std::string();
  161. }
  162. *_field.strVal = *other._field.strVal;
  163. break;
  164. case Type::VECTOR:
  165. if (_field.vectorVal == nullptr)
  166. {
  167. _field.vectorVal = new (std::nothrow) ValueVector();
  168. }
  169. *_field.vectorVal = *other._field.vectorVal;
  170. break;
  171. case Type::MAP:
  172. if (_field.mapVal == nullptr)
  173. {
  174. _field.mapVal = new (std::nothrow) ValueMap();
  175. }
  176. *_field.mapVal = *other._field.mapVal;
  177. break;
  178. case Type::INT_KEY_MAP:
  179. if (_field.intKeyMapVal == nullptr)
  180. {
  181. _field.intKeyMapVal = new (std::nothrow) ValueMapIntKey();
  182. }
  183. *_field.intKeyMapVal = *other._field.intKeyMapVal;
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. return *this;
  190. }
  191. Value& Value::operator= (Value&& other)
  192. {
  193. if (this != &other)
  194. {
  195. clear();
  196. switch (other._type)
  197. {
  198. case Type::BYTE:
  199. _field.byteVal = other._field.byteVal;
  200. break;
  201. case Type::INTEGER:
  202. _field.intVal = other._field.intVal;
  203. break;
  204. case Type::UNSIGNED:
  205. _field.unsignedVal = other._field.unsignedVal;
  206. break;
  207. case Type::FLOAT:
  208. _field.floatVal = other._field.floatVal;
  209. break;
  210. case Type::DOUBLE:
  211. _field.doubleVal = other._field.doubleVal;
  212. break;
  213. case Type::BOOLEAN:
  214. _field.boolVal = other._field.boolVal;
  215. break;
  216. case Type::STRING:
  217. _field.strVal = other._field.strVal;
  218. break;
  219. case Type::VECTOR:
  220. _field.vectorVal = other._field.vectorVal;
  221. break;
  222. case Type::MAP:
  223. _field.mapVal = other._field.mapVal;
  224. break;
  225. case Type::INT_KEY_MAP:
  226. _field.intKeyMapVal = other._field.intKeyMapVal;
  227. break;
  228. default:
  229. break;
  230. }
  231. _type = other._type;
  232. memset(&other._field, 0, sizeof(other._field));
  233. other._type = Type::NONE;
  234. }
  235. return *this;
  236. }
  237. Value& Value::operator= (unsigned char v)
  238. {
  239. reset(Type::BYTE);
  240. _field.byteVal = v;
  241. return *this;
  242. }
  243. Value& Value::operator= (int v)
  244. {
  245. reset(Type::INTEGER);
  246. _field.intVal = v;
  247. return *this;
  248. }
  249. Value& Value::operator= (unsigned int v)
  250. {
  251. reset(Type::UNSIGNED);
  252. _field.unsignedVal = v;
  253. return *this;
  254. }
  255. Value& Value::operator= (float v)
  256. {
  257. reset(Type::FLOAT);
  258. _field.floatVal = v;
  259. return *this;
  260. }
  261. Value& Value::operator= (double v)
  262. {
  263. reset(Type::DOUBLE);
  264. _field.doubleVal = v;
  265. return *this;
  266. }
  267. Value& Value::operator= (bool v)
  268. {
  269. reset(Type::BOOLEAN);
  270. _field.boolVal = v;
  271. return *this;
  272. }
  273. Value& Value::operator= (const char* v)
  274. {
  275. reset(Type::STRING);
  276. *_field.strVal = v ? v : "";
  277. return *this;
  278. }
  279. Value& Value::operator= (const std::string& v)
  280. {
  281. reset(Type::STRING);
  282. *_field.strVal = v;
  283. return *this;
  284. }
  285. Value& Value::operator= (const ValueVector& v)
  286. {
  287. reset(Type::VECTOR);
  288. *_field.vectorVal = v;
  289. return *this;
  290. }
  291. Value& Value::operator= (ValueVector&& v)
  292. {
  293. reset(Type::VECTOR);
  294. *_field.vectorVal = std::move(v);
  295. return *this;
  296. }
  297. Value& Value::operator= (const ValueMap& v)
  298. {
  299. reset(Type::MAP);
  300. *_field.mapVal = v;
  301. return *this;
  302. }
  303. Value& Value::operator= (ValueMap&& v)
  304. {
  305. reset(Type::MAP);
  306. *_field.mapVal = std::move(v);
  307. return *this;
  308. }
  309. Value& Value::operator= (const ValueMapIntKey& v)
  310. {
  311. reset(Type::INT_KEY_MAP);
  312. *_field.intKeyMapVal = v;
  313. return *this;
  314. }
  315. Value& Value::operator= (ValueMapIntKey&& v)
  316. {
  317. reset(Type::INT_KEY_MAP);
  318. *_field.intKeyMapVal = std::move(v);
  319. return *this;
  320. }
  321. bool Value::operator!= (const Value& v)
  322. {
  323. return !(*this == v);
  324. }
  325. bool Value::operator!= (const Value& v) const
  326. {
  327. return !(*this == v);
  328. }
  329. bool Value::operator== (const Value& v)
  330. {
  331. const auto &t = *this;
  332. return t == v;
  333. }
  334. bool Value::operator== (const Value& v) const
  335. {
  336. if (this == &v) return true;
  337. if (v._type != this->_type) return false;
  338. if (this->isNull()) return true;
  339. switch (_type)
  340. {
  341. case Type::BYTE: return v._field.byteVal == this->_field.byteVal;
  342. case Type::INTEGER: return v._field.intVal == this->_field.intVal;
  343. case Type::UNSIGNED:return v._field.unsignedVal == this->_field.unsignedVal;
  344. case Type::BOOLEAN: return v._field.boolVal == this->_field.boolVal;
  345. case Type::STRING: return *v._field.strVal == *this->_field.strVal;
  346. case Type::FLOAT: return std::abs(v._field.floatVal - this->_field.floatVal) <= FLT_EPSILON;
  347. case Type::DOUBLE: return std::abs(v._field.doubleVal - this->_field.doubleVal) <= DBL_EPSILON;
  348. case Type::VECTOR:
  349. {
  350. const auto &v1 = *(this->_field.vectorVal);
  351. const auto &v2 = *(v._field.vectorVal);
  352. const auto size = v1.size();
  353. if (size == v2.size())
  354. {
  355. for (size_t i = 0; i < size; i++)
  356. {
  357. if (v1[i] != v2[i]) return false;
  358. }
  359. return true;
  360. }
  361. return false;
  362. }
  363. case Type::MAP:
  364. {
  365. const auto &map1 = *(this->_field.mapVal);
  366. const auto &map2 = *(v._field.mapVal);
  367. for (const auto &kvp : map1)
  368. {
  369. auto it = map2.find(kvp.first);
  370. if (it == map2.end() || it->second != kvp.second)
  371. {
  372. return false;
  373. }
  374. }
  375. return true;
  376. }
  377. case Type::INT_KEY_MAP:
  378. {
  379. const auto &map1 = *(this->_field.intKeyMapVal);
  380. const auto &map2 = *(v._field.intKeyMapVal);
  381. for (const auto &kvp : map1)
  382. {
  383. auto it = map2.find(kvp.first);
  384. if (it == map2.end() || it->second != kvp.second)
  385. {
  386. return false;
  387. }
  388. }
  389. return true;
  390. }
  391. default:
  392. break;
  393. };
  394. return false;
  395. }
  396. /// Convert value to a specified type
  397. unsigned char Value::asByte() const
  398. {
  399. CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
  400. if (_type == Type::BYTE)
  401. {
  402. return _field.byteVal;
  403. }
  404. if (_type == Type::INTEGER)
  405. {
  406. return static_cast<unsigned char>(_field.intVal);
  407. }
  408. if (_type == Type::UNSIGNED)
  409. {
  410. return static_cast<unsigned char>(_field.unsignedVal);
  411. }
  412. if (_type == Type::STRING)
  413. {
  414. return static_cast<unsigned char>(atoi(_field.strVal->c_str()));
  415. }
  416. if (_type == Type::FLOAT)
  417. {
  418. return static_cast<unsigned char>(_field.floatVal);
  419. }
  420. if (_type == Type::DOUBLE)
  421. {
  422. return static_cast<unsigned char>(_field.doubleVal);
  423. }
  424. if (_type == Type::BOOLEAN)
  425. {
  426. return _field.boolVal ? 1 : 0;
  427. }
  428. return 0;
  429. }
  430. int Value::asInt() const
  431. {
  432. CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
  433. if (_type == Type::INTEGER)
  434. {
  435. return _field.intVal;
  436. }
  437. if (_type == Type::UNSIGNED)
  438. {
  439. CCASSERT(_field.unsignedVal < INT_MAX, "Can only convert values < INT_MAX");
  440. return (int)_field.unsignedVal;
  441. }
  442. if (_type == Type::BYTE)
  443. {
  444. return _field.byteVal;
  445. }
  446. if (_type == Type::STRING)
  447. {
  448. return atoi(_field.strVal->c_str());
  449. }
  450. if (_type == Type::FLOAT)
  451. {
  452. return static_cast<int>(_field.floatVal);
  453. }
  454. if (_type == Type::DOUBLE)
  455. {
  456. return static_cast<int>(_field.doubleVal);
  457. }
  458. if (_type == Type::BOOLEAN)
  459. {
  460. return _field.boolVal ? 1 : 0;
  461. }
  462. return 0;
  463. }
  464. unsigned int Value::asUnsignedInt() const
  465. {
  466. CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
  467. if (_type == Type::UNSIGNED)
  468. {
  469. return _field.unsignedVal;
  470. }
  471. if (_type == Type::INTEGER)
  472. {
  473. CCASSERT(_field.intVal >= 0, "Only values >= 0 can be converted to unsigned");
  474. return static_cast<unsigned int>(_field.intVal);
  475. }
  476. if (_type == Type::BYTE)
  477. {
  478. return static_cast<unsigned int>(_field.byteVal);
  479. }
  480. if (_type == Type::STRING)
  481. {
  482. // NOTE: strtoul is required (need to augment on unsupported platforms)
  483. return static_cast<unsigned int>(strtoul(_field.strVal->c_str(), nullptr, 10));
  484. }
  485. if (_type == Type::FLOAT)
  486. {
  487. return static_cast<unsigned int>(_field.floatVal);
  488. }
  489. if (_type == Type::DOUBLE)
  490. {
  491. return static_cast<unsigned int>(_field.doubleVal);
  492. }
  493. if (_type == Type::BOOLEAN)
  494. {
  495. return _field.boolVal ? 1u : 0u;
  496. }
  497. return 0u;
  498. }
  499. float Value::asFloat() const
  500. {
  501. CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
  502. if (_type == Type::FLOAT)
  503. {
  504. return _field.floatVal;
  505. }
  506. if (_type == Type::BYTE)
  507. {
  508. return static_cast<float>(_field.byteVal);
  509. }
  510. if (_type == Type::STRING)
  511. {
  512. return utils::atof(_field.strVal->c_str());
  513. }
  514. if (_type == Type::INTEGER)
  515. {
  516. return static_cast<float>(_field.intVal);
  517. }
  518. if (_type == Type::UNSIGNED)
  519. {
  520. return static_cast<float>(_field.unsignedVal);
  521. }
  522. if (_type == Type::DOUBLE)
  523. {
  524. return static_cast<float>(_field.doubleVal);
  525. }
  526. if (_type == Type::BOOLEAN)
  527. {
  528. return _field.boolVal ? 1.0f : 0.0f;
  529. }
  530. return 0.0f;
  531. }
  532. double Value::asDouble() const
  533. {
  534. CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
  535. if (_type == Type::DOUBLE)
  536. {
  537. return _field.doubleVal;
  538. }
  539. if (_type == Type::BYTE)
  540. {
  541. return static_cast<double>(_field.byteVal);
  542. }
  543. if (_type == Type::STRING)
  544. {
  545. return static_cast<double>(utils::atof(_field.strVal->c_str()));
  546. }
  547. if (_type == Type::INTEGER)
  548. {
  549. return static_cast<double>(_field.intVal);
  550. }
  551. if (_type == Type::UNSIGNED)
  552. {
  553. return static_cast<double>(_field.unsignedVal);
  554. }
  555. if (_type == Type::FLOAT)
  556. {
  557. return static_cast<double>(_field.floatVal);
  558. }
  559. if (_type == Type::BOOLEAN)
  560. {
  561. return _field.boolVal ? 1.0 : 0.0;
  562. }
  563. return 0.0;
  564. }
  565. bool Value::asBool() const
  566. {
  567. CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
  568. if (_type == Type::BOOLEAN)
  569. {
  570. return _field.boolVal;
  571. }
  572. if (_type == Type::BYTE)
  573. {
  574. return _field.byteVal == 0 ? false : true;
  575. }
  576. if (_type == Type::STRING)
  577. {
  578. return (*_field.strVal == "0" || *_field.strVal == "false") ? false : true;
  579. }
  580. if (_type == Type::INTEGER)
  581. {
  582. return _field.intVal == 0 ? false : true;
  583. }
  584. if (_type == Type::UNSIGNED)
  585. {
  586. return _field.unsignedVal == 0 ? false : true;
  587. }
  588. if (_type == Type::FLOAT)
  589. {
  590. return _field.floatVal == 0.0f ? false : true;
  591. }
  592. if (_type == Type::DOUBLE)
  593. {
  594. return _field.doubleVal == 0.0 ? false : true;
  595. }
  596. return false;
  597. }
  598. std::string Value::asString() const
  599. {
  600. CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
  601. if (_type == Type::STRING)
  602. {
  603. return *_field.strVal;
  604. }
  605. std::stringstream ret;
  606. switch (_type)
  607. {
  608. case Type::BYTE:
  609. ret << _field.byteVal;
  610. break;
  611. case Type::INTEGER:
  612. ret << _field.intVal;
  613. break;
  614. case Type::UNSIGNED:
  615. ret << _field.unsignedVal;
  616. break;
  617. case Type::FLOAT:
  618. ret << std::fixed << std::setprecision( 7 )<< _field.floatVal;
  619. break;
  620. case Type::DOUBLE:
  621. ret << std::fixed << std::setprecision( 16 ) << _field.doubleVal;
  622. break;
  623. case Type::BOOLEAN:
  624. ret << (_field.boolVal ? "true" : "false");
  625. break;
  626. default:
  627. break;
  628. }
  629. return ret.str();
  630. }
  631. ValueVector& Value::asValueVector()
  632. {
  633. CCASSERT(_type == Type::VECTOR, "The value type isn't Type::VECTOR");
  634. return *_field.vectorVal;
  635. }
  636. const ValueVector& Value::asValueVector() const
  637. {
  638. CCASSERT(_type == Type::VECTOR, "The value type isn't Type::VECTOR");
  639. return *_field.vectorVal;
  640. }
  641. ValueMap& Value::asValueMap()
  642. {
  643. CCASSERT(_type == Type::MAP, "The value type isn't Type::MAP");
  644. return *_field.mapVal;
  645. }
  646. const ValueMap& Value::asValueMap() const
  647. {
  648. CCASSERT(_type == Type::MAP, "The value type isn't Type::MAP");
  649. return *_field.mapVal;
  650. }
  651. ValueMapIntKey& Value::asIntKeyMap()
  652. {
  653. CCASSERT(_type == Type::INT_KEY_MAP, "The value type isn't Type::INT_KEY_MAP");
  654. return *_field.intKeyMapVal;
  655. }
  656. const ValueMapIntKey& Value::asIntKeyMap() const
  657. {
  658. CCASSERT(_type == Type::INT_KEY_MAP, "The value type isn't Type::INT_KEY_MAP");
  659. return *_field.intKeyMapVal;
  660. }
  661. static std::string getTabs(int depth)
  662. {
  663. std::string tabWidth;
  664. for (int i = 0; i < depth; ++i)
  665. {
  666. tabWidth += "\t";
  667. }
  668. return tabWidth;
  669. }
  670. static std::string visit(const Value& v, int depth);
  671. static std::string visitVector(const ValueVector& v, int depth)
  672. {
  673. std::stringstream ret;
  674. if (depth > 0)
  675. ret << "\n";
  676. ret << getTabs(depth) << "[\n";
  677. int i = 0;
  678. for (const auto& child : v)
  679. {
  680. ret << getTabs(depth+1) << i << ": " << visit(child, depth + 1);
  681. ++i;
  682. }
  683. ret << getTabs(depth) << "]\n";
  684. return ret.str();
  685. }
  686. template <class T>
  687. static std::string visitMap(const T& v, int depth)
  688. {
  689. std::stringstream ret;
  690. if (depth > 0)
  691. ret << "\n";
  692. ret << getTabs(depth) << "{\n";
  693. for (auto iter = v.begin(); iter != v.end(); ++iter)
  694. {
  695. ret << getTabs(depth + 1) << iter->first << ": ";
  696. ret << visit(iter->second, depth + 1);
  697. }
  698. ret << getTabs(depth) << "}\n";
  699. return ret.str();
  700. }
  701. static std::string visit(const Value& v, int depth)
  702. {
  703. std::stringstream ret;
  704. switch (v.getType())
  705. {
  706. case Value::Type::NONE:
  707. case Value::Type::BYTE:
  708. case Value::Type::INTEGER:
  709. case Value::Type::UNSIGNED:
  710. case Value::Type::FLOAT:
  711. case Value::Type::DOUBLE:
  712. case Value::Type::BOOLEAN:
  713. case Value::Type::STRING:
  714. ret << v.asString() << "\n";
  715. break;
  716. case Value::Type::VECTOR:
  717. ret << visitVector(v.asValueVector(), depth);
  718. break;
  719. case Value::Type::MAP:
  720. ret << visitMap(v.asValueMap(), depth);
  721. break;
  722. case Value::Type::INT_KEY_MAP:
  723. ret << visitMap(v.asIntKeyMap(), depth);
  724. break;
  725. default:
  726. CCASSERT(false, "Invalid type!");
  727. break;
  728. }
  729. return ret.str();
  730. }
  731. std::string Value::getDescription() const
  732. {
  733. std::string ret("\n");
  734. ret += visit(*this, 0);
  735. return ret;
  736. }
  737. void Value::clear()
  738. {
  739. // Free memory the old value allocated
  740. switch (_type)
  741. {
  742. case Type::BYTE:
  743. _field.byteVal = 0;
  744. break;
  745. case Type::INTEGER:
  746. _field.intVal = 0;
  747. break;
  748. case Type::UNSIGNED:
  749. _field.unsignedVal = 0u;
  750. break;
  751. case Type::FLOAT:
  752. _field.floatVal = 0.0f;
  753. break;
  754. case Type::DOUBLE:
  755. _field.doubleVal = 0.0;
  756. break;
  757. case Type::BOOLEAN:
  758. _field.boolVal = false;
  759. break;
  760. case Type::STRING:
  761. CC_SAFE_DELETE(_field.strVal);
  762. break;
  763. case Type::VECTOR:
  764. CC_SAFE_DELETE(_field.vectorVal);
  765. break;
  766. case Type::MAP:
  767. CC_SAFE_DELETE(_field.mapVal);
  768. break;
  769. case Type::INT_KEY_MAP:
  770. CC_SAFE_DELETE(_field.intKeyMapVal);
  771. break;
  772. default:
  773. break;
  774. }
  775. _type = Type::NONE;
  776. }
  777. void Value::reset(Type type)
  778. {
  779. if (_type == type)
  780. return;
  781. clear();
  782. // Allocate memory for the new value
  783. switch (type)
  784. {
  785. case Type::STRING:
  786. _field.strVal = new (std::nothrow) std::string();
  787. break;
  788. case Type::VECTOR:
  789. _field.vectorVal = new (std::nothrow) ValueVector();
  790. break;
  791. case Type::MAP:
  792. _field.mapVal = new (std::nothrow) ValueMap();
  793. break;
  794. case Type::INT_KEY_MAP:
  795. _field.intKeyMapVal = new (std::nothrow) ValueMapIntKey();
  796. break;
  797. default:
  798. break;
  799. }
  800. _type = type;
  801. }
  802. NS_CC_END