Variant.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  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. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "StringUtils.h"
  25. #include "Variant.h"
  26. #include <cstring>
  27. const Variant Variant::EMPTY;
  28. const String Variant::emptyString;
  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 String 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. ""
  53. };
  54. Variant& Variant::operator = (const Variant& rhs)
  55. {
  56. SetType(rhs.GetType());
  57. switch (type_)
  58. {
  59. case VAR_STRING:
  60. *(reinterpret_cast<String*>(&value_)) = *(reinterpret_cast<const String*>(&rhs.value_));
  61. break;
  62. case VAR_BUFFER:
  63. *(reinterpret_cast<PODVector<unsigned char>*>(&value_)) = *(reinterpret_cast<const PODVector<unsigned char>*>(&rhs.value_));
  64. break;
  65. case VAR_RESOURCEREF:
  66. *(reinterpret_cast<ResourceRef*>(&value_)) = *(reinterpret_cast<const ResourceRef*>(&rhs.value_));
  67. break;
  68. case VAR_RESOURCEREFLIST:
  69. *(reinterpret_cast<ResourceRefList*>(&value_)) = *(reinterpret_cast<const ResourceRefList*>(&rhs.value_));
  70. break;
  71. case VAR_VARIANTVECTOR:
  72. *(reinterpret_cast<VariantVector*>(&value_)) = *(reinterpret_cast<const VariantVector*>(&rhs.value_));
  73. break;
  74. case VAR_VARIANTMAP:
  75. *(reinterpret_cast<VariantMap*>(&value_)) = *(reinterpret_cast<const VariantMap*>(&rhs.value_));
  76. break;
  77. default:
  78. value_ = rhs.value_;
  79. break;
  80. }
  81. return *this;
  82. }
  83. bool Variant::operator == (const Variant& rhs) const
  84. {
  85. if (type_ != rhs.type_)
  86. return false;
  87. switch (type_)
  88. {
  89. case VAR_INT:
  90. return value_.int_ == rhs.value_.int_;
  91. case VAR_BOOL:
  92. return value_.bool_ == rhs.value_.bool_;
  93. case VAR_FLOAT:
  94. return value_.float_ == rhs.value_.float_;
  95. case VAR_VECTOR2:
  96. return *(reinterpret_cast<const Vector2*>(&value_)) == *(reinterpret_cast<const Vector2*>(&rhs.value_));
  97. case VAR_VECTOR3:
  98. return *(reinterpret_cast<const Vector3*>(&value_)) == *(reinterpret_cast<const Vector3*>(&rhs.value_));
  99. case VAR_VECTOR4:
  100. return *(reinterpret_cast<const Vector4*>(&value_)) == *(reinterpret_cast<const Vector4*>(&rhs.value_));
  101. case VAR_QUATERNION:
  102. return *(reinterpret_cast<const Quaternion*>(&value_)) == *(reinterpret_cast<const Quaternion*>(&rhs.value_));
  103. case VAR_COLOR:
  104. return *(reinterpret_cast<const Color*>(&value_)) == *(reinterpret_cast<const Color*>(&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. default:
  120. return true;
  121. }
  122. }
  123. void Variant::FromString(const String& type, const String& value)
  124. {
  125. return FromString(GetTypeFromName(type), value.CString());
  126. }
  127. void Variant::FromString(const char* type, const char* value)
  128. {
  129. return FromString(GetTypeFromName(type), value);
  130. }
  131. void Variant::FromString(VariantType type, const String& value)
  132. {
  133. return FromString(type, value.CString());
  134. }
  135. void Variant::FromString(VariantType type, const char* value)
  136. {
  137. switch (type)
  138. {
  139. case VAR_INT:
  140. *this = ToInt(value);
  141. break;
  142. case VAR_BOOL:
  143. *this = ToBool(value);
  144. break;
  145. case VAR_FLOAT:
  146. *this = ToFloat(value);
  147. break;
  148. case VAR_VECTOR2:
  149. *this = ToVector2(value);
  150. break;
  151. case VAR_VECTOR3:
  152. *this = ToVector3(value);
  153. break;
  154. case VAR_VECTOR4:
  155. *this = ToVector4(value);
  156. break;
  157. case VAR_QUATERNION:
  158. *this = ToQuaternion(value);
  159. break;
  160. case VAR_COLOR:
  161. *this = ToColor(value);
  162. break;
  163. case VAR_STRING:
  164. *this = value;
  165. break;
  166. case VAR_BUFFER:
  167. {
  168. SetType(VAR_BUFFER);
  169. PODVector<unsigned char>& buffer = *(reinterpret_cast<PODVector<unsigned char>*>(&value_));
  170. Vector<String> values = String::Split(value, ' ');
  171. buffer.Resize(values.Size());
  172. for (unsigned i = 0; i < values.Size(); ++i)
  173. buffer[i] = ToInt(values[i]);
  174. }
  175. break;
  176. case VAR_PTR:
  177. *this = (void*)0;
  178. break;
  179. case VAR_RESOURCEREF:
  180. {
  181. Vector<String> values = String::Split(value, ';');
  182. if (values.Size() == 2)
  183. {
  184. SetType(VAR_RESOURCEREF);
  185. ResourceRef& ref = *(reinterpret_cast<ResourceRef*>(&value_));
  186. ref.type_ = ShortStringHash(values[0]);
  187. ref.id_ = StringHash(values[1]);
  188. }
  189. }
  190. break;
  191. case VAR_RESOURCEREFLIST:
  192. {
  193. Vector<String> values = String::Split(value, ';');
  194. if (values.Size() >= 1)
  195. {
  196. SetType(VAR_RESOURCEREFLIST);
  197. ResourceRefList& refList = *(reinterpret_cast<ResourceRefList*>(&value_));
  198. refList.type_ = ShortStringHash(values[0]);
  199. refList.ids_.Resize(values.Size() - 1);
  200. for (unsigned i = 1; i < values.Size(); ++i)
  201. refList.ids_[i - 1] = StringHash(values[i]);
  202. }
  203. }
  204. break;
  205. default:
  206. SetType(VAR_NONE);
  207. }
  208. }
  209. void Variant::SetBuffer(const void* data, unsigned size)
  210. {
  211. if (size && !data)
  212. size = 0;
  213. SetType(VAR_BUFFER);
  214. PODVector<unsigned char>& buffer = *(reinterpret_cast<PODVector<unsigned char>*>(&value_));
  215. buffer.Resize(size);
  216. if (size)
  217. memcpy(&buffer[0], data, size);
  218. }
  219. const String& Variant::GetTypeName() const
  220. {
  221. return typeNames[type_];
  222. }
  223. String Variant::ToString() const
  224. {
  225. switch (type_)
  226. {
  227. case VAR_INT:
  228. return String(value_.int_);
  229. case VAR_BOOL:
  230. return String(value_.bool_);
  231. case VAR_FLOAT:
  232. return String(value_.float_);
  233. case VAR_VECTOR2:
  234. return (reinterpret_cast<const Vector2*>(&value_))->ToString();
  235. case VAR_VECTOR3:
  236. return (reinterpret_cast<const Vector3*>(&value_))->ToString();
  237. case VAR_VECTOR4:
  238. return (reinterpret_cast<const Vector4*>(&value_))->ToString();
  239. case VAR_QUATERNION:
  240. return (reinterpret_cast<const Quaternion*>(&value_))->ToString();
  241. case VAR_COLOR:
  242. return (reinterpret_cast<const Color*>(&value_))->ToString();
  243. case VAR_STRING:
  244. return *(reinterpret_cast<const String*>(&value_));
  245. case VAR_BUFFER:
  246. {
  247. const PODVector<unsigned char>& buffer = *(reinterpret_cast<const PODVector<unsigned char>*>(&value_));
  248. String ret;
  249. for (PODVector<unsigned char>::ConstIterator i = buffer.Begin(); i != buffer.End(); ++i)
  250. {
  251. if (i != buffer.Begin())
  252. ret += " ";
  253. ret += String((unsigned)*i);
  254. }
  255. return ret;
  256. }
  257. case VAR_PTR:
  258. // Pointer serialization not supported (convert to null)
  259. return String(0);
  260. case VAR_RESOURCEREF:
  261. case VAR_RESOURCEREFLIST:
  262. case VAR_VARIANTVECTOR:
  263. case VAR_VARIANTMAP:
  264. // Reference string serialization requires hash-to-name mapping from the context & subsystems. Can not support here
  265. // Also variant map or vector string serialization is not supported. XML or binary save should be used instead
  266. return String();
  267. default:
  268. return String();
  269. }
  270. }
  271. void Variant::SetType(VariantType newType)
  272. {
  273. if (type_ == newType)
  274. return;
  275. switch (type_)
  276. {
  277. case VAR_STRING:
  278. (reinterpret_cast<String*>(&value_))->~String();
  279. break;
  280. case VAR_BUFFER:
  281. (reinterpret_cast<PODVector<unsigned char>*>(&value_))->~PODVector<unsigned char>();
  282. break;
  283. case VAR_RESOURCEREF:
  284. (reinterpret_cast<ResourceRef*>(&value_))->~ResourceRef();
  285. break;
  286. case VAR_RESOURCEREFLIST:
  287. (reinterpret_cast<ResourceRefList*>(&value_))->~ResourceRefList();
  288. break;
  289. case VAR_VARIANTVECTOR:
  290. (reinterpret_cast<VariantVector*>(&value_))->~VariantVector();
  291. break;
  292. case VAR_VARIANTMAP:
  293. (reinterpret_cast<VariantMap*>(&value_))->~VariantMap();
  294. break;
  295. default:
  296. break;
  297. }
  298. type_ = newType;
  299. switch (type_)
  300. {
  301. case VAR_STRING:
  302. new(reinterpret_cast<String*>(&value_)) String();
  303. break;
  304. case VAR_BUFFER:
  305. new(reinterpret_cast<PODVector<unsigned char>*>(&value_)) PODVector<unsigned char>();
  306. break;
  307. case VAR_RESOURCEREF:
  308. new(reinterpret_cast<ResourceRef*>(&value_)) ResourceRef();
  309. break;
  310. case VAR_RESOURCEREFLIST:
  311. new(reinterpret_cast<ResourceRefList*>(&value_)) ResourceRefList();
  312. break;
  313. case VAR_VARIANTVECTOR:
  314. new(reinterpret_cast<VariantVector*>(&value_)) VariantVector();
  315. break;
  316. case VAR_VARIANTMAP:
  317. new(reinterpret_cast<VariantMap*>(&value_)) VariantMap();
  318. break;
  319. default:
  320. break;
  321. }
  322. }
  323. template<> int Variant::Get<int>() const
  324. {
  325. return GetInt();
  326. }
  327. template<> unsigned Variant::Get<unsigned>() const
  328. {
  329. return GetUInt();
  330. }
  331. template<> StringHash Variant::Get<StringHash>() const
  332. {
  333. return GetStringHash();
  334. }
  335. template<> ShortStringHash Variant::Get<ShortStringHash>() const
  336. {
  337. return GetShortStringHash();
  338. }
  339. template<> bool Variant::Get<bool>() const
  340. {
  341. return GetBool();
  342. }
  343. template<> float Variant::Get<float>() const
  344. {
  345. return GetFloat();
  346. }
  347. template<> const Vector2& Variant::Get<const Vector2&>() const
  348. {
  349. return GetVector2();
  350. }
  351. template<> const Vector3& Variant::Get<const Vector3&>() const
  352. {
  353. return GetVector3();
  354. }
  355. template<> const Vector4& Variant::Get<const Vector4&>() const
  356. {
  357. return GetVector4();
  358. }
  359. template<> const Quaternion& Variant::Get<const Quaternion&>() const
  360. {
  361. return GetQuaternion();
  362. }
  363. template<> const Color& Variant::Get<const Color&>() const
  364. {
  365. return GetColor();
  366. }
  367. template<> const String& Variant::Get<const String&>() const
  368. {
  369. return GetString();
  370. }
  371. template<> const PODVector<unsigned char>& Variant::Get<const PODVector<unsigned char>& >() const
  372. {
  373. return GetBuffer();
  374. }
  375. template<> void* Variant::Get<void*>() const
  376. {
  377. return GetPtr();
  378. }
  379. template<> ResourceRef Variant::Get<ResourceRef>() const
  380. {
  381. return GetResourceRef();
  382. }
  383. template<> ResourceRefList Variant::Get<ResourceRefList>() const
  384. {
  385. return GetResourceRefList();
  386. }
  387. template<> VariantVector Variant::Get<VariantVector>() const
  388. {
  389. return GetVariantVector();
  390. }
  391. template<> VariantMap Variant::Get<VariantMap>() const
  392. {
  393. return GetVariantMap();
  394. }
  395. template<> Vector2 Variant::Get<Vector2>() const
  396. {
  397. return GetVector2();
  398. }
  399. template<> Vector3 Variant::Get<Vector3>() const
  400. {
  401. return GetVector3();
  402. }
  403. template<> Vector4 Variant::Get<Vector4>() const
  404. {
  405. return GetVector4();
  406. }
  407. template<> Quaternion Variant::Get<Quaternion>() const
  408. {
  409. return GetQuaternion();
  410. }
  411. template<> Color Variant::Get<Color>() const
  412. {
  413. return GetColor();
  414. }
  415. template<> String Variant::Get<String>() const
  416. {
  417. return GetString();
  418. }
  419. template<> PODVector<unsigned char> Variant::Get<PODVector<unsigned char> >() const
  420. {
  421. return GetBuffer();
  422. }
  423. const String& Variant::GetTypeName(VariantType type)
  424. {
  425. return typeNames[type];
  426. }
  427. VariantType Variant::GetTypeFromName(const String& typeName)
  428. {
  429. return GetTypeFromName(typeName.CString());
  430. }
  431. VariantType Variant::GetTypeFromName(const char* typeName)
  432. {
  433. unsigned index = 0;
  434. while (!typeNames[index].Empty())
  435. {
  436. if (!typeNames[index].Compare(typeName, false))
  437. return (VariantType)index;
  438. ++index;
  439. }
  440. return VAR_NONE;
  441. }