CCValue.cpp 22 KB

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