Variant.cpp 16 KB

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