Variant.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  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 "Precompiled.h"
  23. #include "StringUtils.h"
  24. #include "Variant.h"
  25. #include <cstring>
  26. namespace Urho3D
  27. {
  28. const Variant Variant::EMPTY;
  29. const PODVector<unsigned char> Variant::emptyBuffer;
  30. const ResourceRef Variant::emptyResourceRef;
  31. const ResourceRefList Variant::emptyResourceRefList;
  32. const VariantMap Variant::emptyVariantMap;
  33. const VariantVector Variant::emptyVariantVector;
  34. static const char* typeNames[] =
  35. {
  36. "None",
  37. "Int",
  38. "Bool",
  39. "Float",
  40. "Vector2",
  41. "Vector3",
  42. "Vector4",
  43. "Quaternion",
  44. "Color",
  45. "String",
  46. "Buffer",
  47. "Pointer",
  48. "ResourceRef",
  49. "ResourceRefList",
  50. "VariantVector",
  51. "VariantMap",
  52. "IntRect",
  53. "IntVector2",
  54. 0
  55. };
  56. Variant& Variant::operator = (const Variant& rhs)
  57. {
  58. SetType(rhs.GetType());
  59. switch (type_)
  60. {
  61. case VAR_STRING:
  62. *(reinterpret_cast<String*>(&value_)) = *(reinterpret_cast<const String*>(&rhs.value_));
  63. break;
  64. case VAR_BUFFER:
  65. *(reinterpret_cast<PODVector<unsigned char>*>(&value_)) = *(reinterpret_cast<const PODVector<unsigned char>*>(&rhs.value_));
  66. break;
  67. case VAR_RESOURCEREF:
  68. *(reinterpret_cast<ResourceRef*>(&value_)) = *(reinterpret_cast<const ResourceRef*>(&rhs.value_));
  69. break;
  70. case VAR_RESOURCEREFLIST:
  71. *(reinterpret_cast<ResourceRefList*>(&value_)) = *(reinterpret_cast<const ResourceRefList*>(&rhs.value_));
  72. break;
  73. case VAR_VARIANTVECTOR:
  74. *(reinterpret_cast<VariantVector*>(&value_)) = *(reinterpret_cast<const VariantVector*>(&rhs.value_));
  75. break;
  76. case VAR_VARIANTMAP:
  77. *(reinterpret_cast<VariantMap*>(&value_)) = *(reinterpret_cast<const VariantMap*>(&rhs.value_));
  78. break;
  79. default:
  80. value_ = rhs.value_;
  81. break;
  82. }
  83. return *this;
  84. }
  85. bool Variant::operator == (const Variant& rhs) const
  86. {
  87. if (type_ != rhs.type_)
  88. return false;
  89. switch (type_)
  90. {
  91. case VAR_INT:
  92. return value_.int_ == rhs.value_.int_;
  93. case VAR_BOOL:
  94. return value_.bool_ == rhs.value_.bool_;
  95. case VAR_FLOAT:
  96. return value_.float_ == rhs.value_.float_;
  97. case VAR_VECTOR2:
  98. return *(reinterpret_cast<const Vector2*>(&value_)) == *(reinterpret_cast<const Vector2*>(&rhs.value_));
  99. case VAR_VECTOR3:
  100. return *(reinterpret_cast<const Vector3*>(&value_)) == *(reinterpret_cast<const Vector3*>(&rhs.value_));
  101. case VAR_VECTOR4:
  102. case VAR_QUATERNION:
  103. case VAR_COLOR:
  104. // Hack: use the Vector4 compare for all these classes, as they have the same memory structure
  105. return *(reinterpret_cast<const Vector4*>(&value_)) == *(reinterpret_cast<const Vector4*>(&rhs.value_));
  106. case VAR_STRING:
  107. return *(reinterpret_cast<const String*>(&value_)) == *(reinterpret_cast<const String*>(&rhs.value_));
  108. case VAR_BUFFER:
  109. return *(reinterpret_cast<const PODVector<unsigned char>*>(&value_)) == *(reinterpret_cast<const PODVector<unsigned char>*>(&rhs.value_));
  110. case VAR_PTR:
  111. return value_.ptr_ == rhs.value_.ptr_;
  112. case VAR_RESOURCEREF:
  113. return *(reinterpret_cast<const ResourceRef*>(&value_)) == *(reinterpret_cast<const ResourceRef*>(&rhs.value_));
  114. case VAR_RESOURCEREFLIST:
  115. return *(reinterpret_cast<const ResourceRefList*>(&value_)) == *(reinterpret_cast<const ResourceRefList*>(&rhs.value_));
  116. case VAR_VARIANTVECTOR:
  117. return *(reinterpret_cast<const VariantVector*>(&value_)) == *(reinterpret_cast<const VariantVector*>(&rhs.value_));
  118. case VAR_VARIANTMAP:
  119. return *(reinterpret_cast<const VariantMap*>(&value_)) == *(reinterpret_cast<const VariantMap*>(&rhs.value_));
  120. case VAR_INTRECT:
  121. return *(reinterpret_cast<const IntRect*>(&value_)) == *(reinterpret_cast<const IntRect*>(&rhs.value_));
  122. case VAR_INTVECTOR2:
  123. return *(reinterpret_cast<const IntVector2*>(&value_)) == *(reinterpret_cast<const IntVector2*>(&rhs.value_));
  124. default:
  125. return true;
  126. }
  127. }
  128. void Variant::FromString(const String& type, const String& value)
  129. {
  130. return FromString(GetTypeFromName(type), value.CString());
  131. }
  132. void Variant::FromString(const char* type, const char* value)
  133. {
  134. return FromString(GetTypeFromName(type), value);
  135. }
  136. void Variant::FromString(VariantType type, const String& value)
  137. {
  138. return FromString(type, value.CString());
  139. }
  140. void Variant::FromString(VariantType type, const char* value)
  141. {
  142. switch (type)
  143. {
  144. case VAR_INT:
  145. *this = ToInt(value);
  146. break;
  147. case VAR_BOOL:
  148. *this = ToBool(value);
  149. break;
  150. case VAR_FLOAT:
  151. *this = ToFloat(value);
  152. break;
  153. case VAR_VECTOR2:
  154. *this = ToVector2(value);
  155. break;
  156. case VAR_VECTOR3:
  157. *this = ToVector3(value);
  158. break;
  159. case VAR_VECTOR4:
  160. *this = ToVector4(value);
  161. break;
  162. case VAR_QUATERNION:
  163. *this = ToQuaternion(value);
  164. break;
  165. case VAR_COLOR:
  166. *this = ToColor(value);
  167. break;
  168. case VAR_STRING:
  169. *this = value;
  170. break;
  171. case VAR_BUFFER:
  172. {
  173. SetType(VAR_BUFFER);
  174. PODVector<unsigned char>& buffer = *(reinterpret_cast<PODVector<unsigned char>*>(&value_));
  175. Vector<String> values = String::Split(value, ' ');
  176. buffer.Resize(values.Size());
  177. for (unsigned i = 0; i < values.Size(); ++i)
  178. buffer[i] = ToInt(values[i]);
  179. }
  180. break;
  181. case VAR_PTR:
  182. *this = (void*)0;
  183. break;
  184. case VAR_RESOURCEREF:
  185. {
  186. Vector<String> values = String::Split(value, ';');
  187. if (values.Size() == 2)
  188. {
  189. SetType(VAR_RESOURCEREF);
  190. ResourceRef& ref = *(reinterpret_cast<ResourceRef*>(&value_));
  191. ref.type_ = values[0];
  192. ref.id_ = values[1];
  193. }
  194. }
  195. break;
  196. case VAR_RESOURCEREFLIST:
  197. {
  198. Vector<String> values = String::Split(value, ';');
  199. if (values.Size() >= 1)
  200. {
  201. SetType(VAR_RESOURCEREFLIST);
  202. ResourceRefList& refList = *(reinterpret_cast<ResourceRefList*>(&value_));
  203. refList.type_ = values[0];
  204. refList.ids_.Resize(values.Size() - 1);
  205. for (unsigned i = 1; i < values.Size(); ++i)
  206. refList.ids_[i - 1] = values[i];
  207. }
  208. }
  209. break;
  210. case VAR_INTRECT:
  211. *this = ToIntRect(value);
  212. break;
  213. case VAR_INTVECTOR2:
  214. *this = ToIntVector2(value);
  215. break;
  216. default:
  217. SetType(VAR_NONE);
  218. }
  219. }
  220. void Variant::SetBuffer(const void* data, unsigned size)
  221. {
  222. if (size && !data)
  223. size = 0;
  224. SetType(VAR_BUFFER);
  225. PODVector<unsigned char>& buffer = *(reinterpret_cast<PODVector<unsigned char>*>(&value_));
  226. buffer.Resize(size);
  227. if (size)
  228. memcpy(&buffer[0], data, size);
  229. }
  230. String Variant::GetTypeName() const
  231. {
  232. return typeNames[type_];
  233. }
  234. String Variant::ToString() const
  235. {
  236. switch (type_)
  237. {
  238. case VAR_INT:
  239. return String(value_.int_);
  240. case VAR_BOOL:
  241. return String(value_.bool_);
  242. case VAR_FLOAT:
  243. return String(value_.float_);
  244. case VAR_VECTOR2:
  245. return (reinterpret_cast<const Vector2*>(&value_))->ToString();
  246. case VAR_VECTOR3:
  247. return (reinterpret_cast<const Vector3*>(&value_))->ToString();
  248. case VAR_VECTOR4:
  249. return (reinterpret_cast<const Vector4*>(&value_))->ToString();
  250. case VAR_QUATERNION:
  251. return (reinterpret_cast<const Quaternion*>(&value_))->ToString();
  252. case VAR_COLOR:
  253. return (reinterpret_cast<const Color*>(&value_))->ToString();
  254. case VAR_STRING:
  255. return *(reinterpret_cast<const String*>(&value_));
  256. case VAR_BUFFER:
  257. {
  258. const PODVector<unsigned char>& buffer = *(reinterpret_cast<const PODVector<unsigned char>*>(&value_));
  259. String ret;
  260. for (PODVector<unsigned char>::ConstIterator i = buffer.Begin(); i != buffer.End(); ++i)
  261. {
  262. if (i != buffer.Begin())
  263. ret += " ";
  264. ret += String((unsigned)*i);
  265. }
  266. return ret;
  267. }
  268. case VAR_PTR:
  269. // Pointer serialization not supported (convert to null)
  270. return String(0);
  271. case VAR_INTRECT:
  272. return (reinterpret_cast<const IntRect*>(&value_))->ToString();
  273. case VAR_INTVECTOR2:
  274. return (reinterpret_cast<const IntVector2*>(&value_))->ToString();
  275. default:
  276. // VAR_RESOURCEREF, VAR_RESOURCEREFLIST, VAR_VARIANTVECTOR, VAR_VARIANTMAP
  277. // Reference string serialization requires hash-to-name mapping from the context & subsystems. Can not support here
  278. // Also variant map or vector string serialization is not supported. XML or binary save should be used instead
  279. return String::EMPTY;
  280. }
  281. }
  282. bool Variant::IsZero() const
  283. {
  284. switch (type_)
  285. {
  286. case VAR_INT:
  287. return value_.int_ == 0;
  288. case VAR_BOOL:
  289. return value_.bool_ == false;
  290. case VAR_FLOAT:
  291. return value_.float_ == 0.0f;
  292. case VAR_VECTOR2:
  293. return *reinterpret_cast<const Vector2*>(&value_) == Vector2::ZERO;
  294. case VAR_VECTOR3:
  295. return *reinterpret_cast<const Vector3*>(&value_) == Vector3::ZERO;
  296. case VAR_VECTOR4:
  297. return *reinterpret_cast<const Vector4*>(&value_) == Vector4::ZERO;
  298. case VAR_QUATERNION:
  299. return *reinterpret_cast<const Quaternion*>(&value_) == Quaternion::IDENTITY;
  300. case VAR_COLOR:
  301. // WHITE is considered empty (i.e. default) color in the Color class definition
  302. return *reinterpret_cast<const Color*>(&value_) == Color::WHITE;
  303. case VAR_STRING:
  304. return reinterpret_cast<const String*>(&value_)->Empty();
  305. case VAR_BUFFER:
  306. return reinterpret_cast<const PODVector<unsigned char>*>(&value_)->Empty();
  307. case VAR_PTR:
  308. return value_.ptr_ == 0;
  309. case VAR_RESOURCEREF:
  310. return reinterpret_cast<const ResourceRef*>(&value_)->id_ == StringHash::ZERO;
  311. case VAR_RESOURCEREFLIST:
  312. {
  313. Vector<StringHash> ids = reinterpret_cast<const ResourceRefList*>(&value_)->ids_;
  314. for (Vector<StringHash>::ConstIterator i = ids.Begin(); i != ids.End(); ++i)
  315. {
  316. if (*i != StringHash::ZERO)
  317. return false;
  318. }
  319. return true;
  320. }
  321. case VAR_VARIANTVECTOR:
  322. return reinterpret_cast<const VariantVector*>(&value_)->Empty();
  323. case VAR_VARIANTMAP:
  324. return reinterpret_cast<const VariantMap*>(&value_)->Empty();
  325. case VAR_INTRECT:
  326. return *reinterpret_cast<const IntRect*>(&value_) == IntRect::ZERO;
  327. case VAR_INTVECTOR2:
  328. return *reinterpret_cast<const IntVector2*>(&value_) == IntVector2::ZERO;
  329. default:
  330. return true;
  331. }
  332. }
  333. void Variant::SetType(VariantType newType)
  334. {
  335. if (type_ == newType)
  336. return;
  337. switch (type_)
  338. {
  339. case VAR_STRING:
  340. (reinterpret_cast<String*>(&value_))->~String();
  341. break;
  342. case VAR_BUFFER:
  343. (reinterpret_cast<PODVector<unsigned char>*>(&value_))->~PODVector<unsigned char>();
  344. break;
  345. case VAR_RESOURCEREF:
  346. (reinterpret_cast<ResourceRef*>(&value_))->~ResourceRef();
  347. break;
  348. case VAR_RESOURCEREFLIST:
  349. (reinterpret_cast<ResourceRefList*>(&value_))->~ResourceRefList();
  350. break;
  351. case VAR_VARIANTVECTOR:
  352. (reinterpret_cast<VariantVector*>(&value_))->~VariantVector();
  353. break;
  354. case VAR_VARIANTMAP:
  355. (reinterpret_cast<VariantMap*>(&value_))->~VariantMap();
  356. break;
  357. default:
  358. break;
  359. }
  360. type_ = newType;
  361. switch (type_)
  362. {
  363. case VAR_STRING:
  364. new(reinterpret_cast<String*>(&value_)) String();
  365. break;
  366. case VAR_BUFFER:
  367. new(reinterpret_cast<PODVector<unsigned char>*>(&value_)) PODVector<unsigned char>();
  368. break;
  369. case VAR_RESOURCEREF:
  370. new(reinterpret_cast<ResourceRef*>(&value_)) ResourceRef();
  371. break;
  372. case VAR_RESOURCEREFLIST:
  373. new(reinterpret_cast<ResourceRefList*>(&value_)) ResourceRefList();
  374. break;
  375. case VAR_VARIANTVECTOR:
  376. new(reinterpret_cast<VariantVector*>(&value_)) VariantVector();
  377. break;
  378. case VAR_VARIANTMAP:
  379. new(reinterpret_cast<VariantMap*>(&value_)) VariantMap();
  380. break;
  381. default:
  382. break;
  383. }
  384. }
  385. template<> int Variant::Get<int>() const
  386. {
  387. return GetInt();
  388. }
  389. template<> unsigned Variant::Get<unsigned>() const
  390. {
  391. return GetUInt();
  392. }
  393. template<> StringHash Variant::Get<StringHash>() const
  394. {
  395. return GetStringHash();
  396. }
  397. template<> ShortStringHash Variant::Get<ShortStringHash>() const
  398. {
  399. return GetShortStringHash();
  400. }
  401. template<> bool Variant::Get<bool>() const
  402. {
  403. return GetBool();
  404. }
  405. template<> float Variant::Get<float>() const
  406. {
  407. return GetFloat();
  408. }
  409. template<> const Vector2& Variant::Get<const Vector2&>() const
  410. {
  411. return GetVector2();
  412. }
  413. template<> const Vector3& Variant::Get<const Vector3&>() const
  414. {
  415. return GetVector3();
  416. }
  417. template<> const Vector4& Variant::Get<const Vector4&>() const
  418. {
  419. return GetVector4();
  420. }
  421. template<> const Quaternion& Variant::Get<const Quaternion&>() const
  422. {
  423. return GetQuaternion();
  424. }
  425. template<> const Color& Variant::Get<const Color&>() const
  426. {
  427. return GetColor();
  428. }
  429. template<> const String& Variant::Get<const String&>() const
  430. {
  431. return GetString();
  432. }
  433. template<> const IntRect& Variant::Get<const IntRect&>() const
  434. {
  435. return GetIntRect();
  436. }
  437. template<> const IntVector2& Variant::Get<const IntVector2&>() const
  438. {
  439. return GetIntVector2();
  440. }
  441. template<> const PODVector<unsigned char>& Variant::Get<const PODVector<unsigned char>& >() const
  442. {
  443. return GetBuffer();
  444. }
  445. template<> void* Variant::Get<void*>() const
  446. {
  447. return GetPtr();
  448. }
  449. template<> ResourceRef Variant::Get<ResourceRef>() const
  450. {
  451. return GetResourceRef();
  452. }
  453. template<> ResourceRefList Variant::Get<ResourceRefList>() const
  454. {
  455. return GetResourceRefList();
  456. }
  457. template<> VariantVector Variant::Get<VariantVector>() const
  458. {
  459. return GetVariantVector();
  460. }
  461. template<> VariantMap Variant::Get<VariantMap>() const
  462. {
  463. return GetVariantMap();
  464. }
  465. template<> Vector2 Variant::Get<Vector2>() const
  466. {
  467. return GetVector2();
  468. }
  469. template<> Vector3 Variant::Get<Vector3>() const
  470. {
  471. return GetVector3();
  472. }
  473. template<> Vector4 Variant::Get<Vector4>() const
  474. {
  475. return GetVector4();
  476. }
  477. template<> Quaternion Variant::Get<Quaternion>() const
  478. {
  479. return GetQuaternion();
  480. }
  481. template<> Color Variant::Get<Color>() const
  482. {
  483. return GetColor();
  484. }
  485. template<> String Variant::Get<String>() const
  486. {
  487. return GetString();
  488. }
  489. template<> IntRect Variant::Get<IntRect>() const
  490. {
  491. return GetIntRect();
  492. }
  493. template<> IntVector2 Variant::Get<IntVector2>() const
  494. {
  495. return GetIntVector2();
  496. }
  497. template<> PODVector<unsigned char> Variant::Get<PODVector<unsigned char> >() const
  498. {
  499. return GetBuffer();
  500. }
  501. String Variant::GetTypeName(VariantType type)
  502. {
  503. return typeNames[type];
  504. }
  505. VariantType Variant::GetTypeFromName(const String& typeName)
  506. {
  507. return GetTypeFromName(typeName.CString());
  508. }
  509. VariantType Variant::GetTypeFromName(const char* typeName)
  510. {
  511. return (VariantType)GetStringListIndex(typeName, typeNames, VAR_NONE);
  512. }
  513. }