Value.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /****************************************************************************
  2. Copyright (c) 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 "Value.hpp"
  22. #include "Object.hpp"
  23. namespace se {
  24. ValueArray EmptyValueArray;
  25. Value Value::Null = Value(Type::Null);
  26. Value Value::Undefined = Value(Type::Undefined);
  27. Value::Value()
  28. : _type(Type::Undefined)
  29. , _autoRootUnroot(false)
  30. {
  31. memset(&_u, 0, sizeof(_u));
  32. }
  33. Value::Value(Type type)
  34. : _type(Type::Undefined)
  35. , _autoRootUnroot(false)
  36. {
  37. memset(&_u, 0, sizeof(_u));
  38. reset(type);
  39. }
  40. Value::Value(const Value& v)
  41. : _type(Type::Undefined)
  42. , _autoRootUnroot(false)
  43. {
  44. *this = v;
  45. }
  46. Value::Value(Value&& v)
  47. : _type(Type::Undefined)
  48. , _autoRootUnroot(false)
  49. {
  50. *this = std::move(v);
  51. }
  52. Value::Value(bool v)
  53. : _type(Type::Undefined)
  54. , _autoRootUnroot(false)
  55. {
  56. setBoolean(v);
  57. }
  58. Value::Value(int8_t v)
  59. : _type(Type::Undefined)
  60. , _autoRootUnroot(false)
  61. {
  62. setInt8(v);
  63. }
  64. Value::Value(uint8_t v)
  65. : _type(Type::Undefined)
  66. , _autoRootUnroot(false)
  67. {
  68. setUint8(v);
  69. }
  70. Value::Value(int32_t v)
  71. : _type(Type::Undefined)
  72. , _autoRootUnroot(false)
  73. {
  74. setInt32(v);
  75. }
  76. Value::Value(uint32_t v)
  77. : _type(Type::Undefined)
  78. , _autoRootUnroot(false)
  79. {
  80. setUint32(v);
  81. }
  82. Value::Value(int16_t v)
  83. : _type(Type::Undefined)
  84. , _autoRootUnroot(false)
  85. {
  86. setInt16(v);
  87. }
  88. Value::Value(uint16_t v)
  89. : _type(Type::Undefined)
  90. , _autoRootUnroot(false)
  91. {
  92. setUint16(v);
  93. }
  94. Value::Value(long v)
  95. : _type(Type::Undefined)
  96. , _autoRootUnroot(false)
  97. {
  98. setLong(v);
  99. }
  100. Value::Value(unsigned long v)
  101. : _type(Type::Undefined)
  102. , _autoRootUnroot(false)
  103. {
  104. setUlong(v);
  105. }
  106. Value::Value(float v)
  107. : _type(Type::Undefined)
  108. , _autoRootUnroot(false)
  109. {
  110. setFloat(v);
  111. }
  112. Value::Value(double v)
  113. : _type(Type::Undefined)
  114. , _autoRootUnroot(false)
  115. {
  116. setNumber(v);
  117. }
  118. Value::Value(const char* v)
  119. : _type(Type::Undefined)
  120. , _autoRootUnroot(false)
  121. {
  122. setString(v);
  123. }
  124. Value::Value(const std::string& v)
  125. : _type(Type::Undefined)
  126. , _autoRootUnroot(false)
  127. {
  128. setString(v);
  129. }
  130. Value::Value(Object* o, bool autoRootUnroot/* = false*/)
  131. : _type(Type::Undefined)
  132. , _autoRootUnroot(false)
  133. {
  134. setObject(o, autoRootUnroot);
  135. }
  136. Value::Value(const HandleObject& o, bool autoRootUnroot/* = false*/)
  137. : _type(Type::Undefined)
  138. , _autoRootUnroot(false)
  139. {
  140. setObject(o, autoRootUnroot);
  141. }
  142. Value::~Value()
  143. {
  144. reset(Type::Undefined);
  145. }
  146. Value& Value::operator=(const Value& v)
  147. {
  148. if (this != &v)
  149. {
  150. reset(v.getType());
  151. switch (_type) {
  152. case Type::Null:
  153. case Type::Undefined:
  154. {
  155. memset(&_u, 0, sizeof(_u));
  156. break;
  157. }
  158. case Type::Number:
  159. _u._number = v._u._number;
  160. break;
  161. case Type::String:
  162. *_u._string = *v._u._string;
  163. break;
  164. case Type::Boolean:
  165. _u._boolean = v._u._boolean;
  166. break;
  167. case Type::Object:
  168. {
  169. setObject(v._u._object, v._autoRootUnroot);
  170. }
  171. break;
  172. default:
  173. break;
  174. }
  175. }
  176. return *this;
  177. }
  178. Value& Value::operator=(Value&& v)
  179. {
  180. if (this != &v)
  181. {
  182. reset(v.getType());
  183. switch (_type) {
  184. case Type::Null:
  185. case Type::Undefined:
  186. {
  187. memset(&_u, 0, sizeof(_u));
  188. break;
  189. }
  190. case Type::Number:
  191. _u._number = v._u._number;
  192. break;
  193. case Type::String:
  194. *_u._string = std::move(*v._u._string);
  195. break;
  196. case Type::Boolean:
  197. _u._boolean = v._u._boolean;
  198. break;
  199. case Type::Object:
  200. {
  201. if (_u._object != nullptr) // When old value is also an Object, reset will take no effect, therefore, _u._object may not be nullptr.
  202. {
  203. if (_autoRootUnroot)
  204. {
  205. _u._object->unroot();
  206. }
  207. _u._object->decRef();
  208. }
  209. _u._object = v._u._object;
  210. _autoRootUnroot = v._autoRootUnroot;
  211. v._u._object = nullptr; // Reset to nullptr here to avoid 'release' operation in v.reset(Type::Undefined) since it's a move operation here.
  212. v._autoRootUnroot = false;
  213. }
  214. break;
  215. default:
  216. break;
  217. }
  218. v.reset(Type::Undefined);
  219. }
  220. return *this;
  221. }
  222. // Value& Value::operator=(bool v)
  223. // {
  224. // setBoolean(v);
  225. // return *this;
  226. // }
  227. //
  228. // Value& Value::operator=(double v)
  229. // {
  230. // setNumber(v);
  231. // return *this;
  232. // }
  233. //
  234. // Value& Value::operator=(const std::string& v)
  235. // {
  236. // setString(v);
  237. // return *this;
  238. // }
  239. //
  240. // Value& Value::operator=(Object* o)
  241. // {
  242. // setObject(o);
  243. // return *this;
  244. // }
  245. //
  246. // Value& Value::operator=(const HandleObject& o)
  247. // {
  248. // setObject(o);
  249. // return *this;
  250. // }
  251. void Value::setUndefined()
  252. {
  253. reset(Type::Undefined);
  254. }
  255. void Value::setNull()
  256. {
  257. reset(Type::Null);
  258. }
  259. void Value::setBoolean(bool v)
  260. {
  261. reset(Type::Boolean);
  262. _u._boolean = v;
  263. }
  264. void Value::setInt8(int8_t v)
  265. {
  266. reset(Type::Number);
  267. _u._number = (double)v;
  268. }
  269. void Value::setUint8(uint8_t v)
  270. {
  271. reset(Type::Number);
  272. _u._number = (double)v;
  273. }
  274. void Value::setInt32(int32_t v)
  275. {
  276. reset(Type::Number);
  277. _u._number = (double)v;
  278. }
  279. void Value::setUint32(uint32_t v)
  280. {
  281. reset(Type::Number);
  282. _u._number = (double)v;
  283. }
  284. void Value::setInt16(int16_t v)
  285. {
  286. reset(Type::Number);
  287. _u._number = (double)v;
  288. }
  289. void Value::setUint16(uint16_t v)
  290. {
  291. reset(Type::Number);
  292. _u._number = (double)v;
  293. }
  294. void Value::setLong(long v)
  295. {
  296. reset(Type::Number);
  297. _u._number = (double)v;
  298. }
  299. void Value::setUlong(unsigned long v)
  300. {
  301. reset(Type::Number);
  302. _u._number = (double)v;
  303. }
  304. void Value::setFloat(float v)
  305. {
  306. reset(Type::Number);
  307. _u._number = (double)v;
  308. }
  309. void Value::setNumber(double v)
  310. {
  311. reset(Type::Number);
  312. _u._number = v;
  313. }
  314. void Value::setString(const char* v)
  315. {
  316. if (v != nullptr)
  317. {
  318. reset(Type::String);
  319. *_u._string = v;
  320. }
  321. else
  322. {
  323. reset(Type::Null);
  324. }
  325. }
  326. void Value::setString(const std::string& v)
  327. {
  328. reset(Type::String);
  329. *_u._string = v;
  330. }
  331. void Value::setObject(Object* object, bool autoRootUnroot/* = false*/)
  332. {
  333. if (object == nullptr)
  334. {
  335. reset(Type::Null);
  336. return;
  337. }
  338. if (_type != Type::Object)
  339. {
  340. reset(Type::Object);
  341. }
  342. if (_u._object != object)
  343. {
  344. if (object != nullptr)
  345. {
  346. object->incRef();
  347. if (autoRootUnroot)
  348. {
  349. object->root();
  350. }
  351. }
  352. if (_u._object != nullptr) // When old value is also an Object, reset will take no effect, therefore, _u._object may not be nullptr.
  353. {
  354. if (_autoRootUnroot)
  355. {
  356. _u._object->unroot();
  357. }
  358. _u._object->decRef();
  359. }
  360. _u._object = object;
  361. _autoRootUnroot = autoRootUnroot;
  362. }
  363. else
  364. {
  365. _autoRootUnroot = autoRootUnroot;
  366. if (_autoRootUnroot)
  367. {
  368. _u._object->root();
  369. }
  370. }
  371. }
  372. void Value::setObject(const HandleObject& o, bool autoRootUnroot/* = false*/)
  373. {
  374. setObject(o.get(), autoRootUnroot);
  375. }
  376. int8_t Value::toInt8() const
  377. {
  378. return static_cast<int8_t>(toNumber());
  379. }
  380. uint8_t Value::toUint8() const
  381. {
  382. return static_cast<uint8_t>(toNumber());
  383. }
  384. int16_t Value::toInt16() const
  385. {
  386. return static_cast<int16_t>(toNumber());
  387. }
  388. uint16_t Value::toUint16() const
  389. {
  390. return static_cast<uint16_t>(toNumber());
  391. }
  392. int32_t Value::toInt32() const
  393. {
  394. return static_cast<int32_t>(toNumber());
  395. }
  396. uint32_t Value::toUint32() const
  397. {
  398. return static_cast<uint32_t>(toNumber());
  399. }
  400. long Value::toLong() const
  401. {
  402. return static_cast<long>(toNumber());
  403. }
  404. unsigned long Value::toUlong() const
  405. {
  406. return static_cast<unsigned long>(toNumber());
  407. }
  408. float Value::toFloat() const
  409. {
  410. return static_cast<float>(toNumber());
  411. }
  412. double Value::toNumber() const
  413. {
  414. assert(_type == Type::Number || _type == Type::Boolean);
  415. if (_type == Type::Boolean)
  416. {
  417. if (_u._boolean)
  418. return 1.0;
  419. else
  420. return 0.0;
  421. }
  422. return _u._number;
  423. }
  424. bool Value::toBoolean() const
  425. {
  426. assert(_type == Type::Boolean);
  427. return _u._boolean;
  428. }
  429. const std::string& Value::toString() const
  430. {
  431. assert(_type == Type::String);
  432. return *_u._string;
  433. }
  434. std::string Value::toStringForce() const
  435. {
  436. std::string ret;
  437. if (_type == Type::String)
  438. {
  439. ret = *_u._string;
  440. }
  441. else if (_type == Type::Boolean)
  442. {
  443. ret = _u._boolean ? "true" : "false";
  444. }
  445. else if (_type == Type::Number)
  446. {
  447. char tmp[50] = {0};
  448. snprintf(tmp, sizeof(tmp), "%.17g", _u._number);
  449. ret = tmp;
  450. }
  451. else if (_type == Type::Object)
  452. {
  453. ret = toObject()->toString();
  454. }
  455. else if (_type == Type::Null)
  456. {
  457. ret = "null";
  458. }
  459. else if (_type == Type::Undefined)
  460. {
  461. ret = "undefined";
  462. }
  463. else
  464. {
  465. assert(false);
  466. }
  467. return ret;
  468. }
  469. Object* Value::toObject() const
  470. {
  471. assert(isObject());
  472. return _u._object;
  473. }
  474. void Value::reset(Type type)
  475. {
  476. if (_type != type)
  477. {
  478. switch (_type) {
  479. case Type::String:
  480. delete _u._string;
  481. break;
  482. case Type::Object:
  483. {
  484. if (_u._object != nullptr)
  485. {
  486. if (_autoRootUnroot)
  487. {
  488. _u._object->unroot();
  489. }
  490. _u._object->decRef();
  491. _u._object = nullptr;
  492. }
  493. _autoRootUnroot = false;
  494. break;
  495. }
  496. default:
  497. break;
  498. }
  499. memset(&_u, 0, sizeof(_u));
  500. switch (type) {
  501. case Type::String:
  502. _u._string = new std::string();
  503. break;
  504. default:
  505. break;
  506. }
  507. _type = type;
  508. }
  509. }
  510. } // namespace se {