Variant.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. #include "DebugNew.h"
  28. const Variant Variant::EMPTY;
  29. const String Variant::emptyString;
  30. const PODVector<unsigned char> Variant::emptyBuffer;
  31. const ResourceRef Variant::emptyResourceRef;
  32. const ResourceRefList Variant::emptyResourceRefList;
  33. const VariantMap Variant::emptyVariantMap;
  34. const VariantVector Variant::emptyVariantVector;
  35. static const String typeNames[] =
  36. {
  37. "None",
  38. "Int",
  39. "Bool",
  40. "Float",
  41. "Vector2",
  42. "Vector3",
  43. "Vector4",
  44. "Quaternion",
  45. "Color",
  46. "String",
  47. "Buffer",
  48. "Pointer",
  49. "ResourceRef",
  50. "ResourceRefList",
  51. "VariantVector",
  52. "VariantMap"
  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_.ptr_)) = *(reinterpret_cast<const String*>(rhs.value_.ptr_));
  61. break;
  62. case VAR_BUFFER:
  63. *(reinterpret_cast<PODVector<unsigned char>*>(value_.ptr_)) = *(reinterpret_cast<const PODVector<unsigned char>*>(rhs.value_.ptr_));
  64. break;
  65. case VAR_RESOURCEREF:
  66. *(reinterpret_cast<ResourceRef*>(value_.ptr_)) = *(reinterpret_cast<ResourceRef*>(rhs.value_.ptr_));
  67. break;
  68. case VAR_RESOURCEREFLIST:
  69. *(reinterpret_cast<ResourceRefList*>(value_.ptr_)) = *(reinterpret_cast<ResourceRefList*>(rhs.value_.ptr_));
  70. break;
  71. case VAR_VARIANTVECTOR:
  72. *(reinterpret_cast<VariantVector*>(value_.ptr_)) = *(reinterpret_cast<VariantVector*>(rhs.value_.ptr_));
  73. break;
  74. case VAR_VARIANTMAP:
  75. *(reinterpret_cast<VariantMap*>(value_.ptr_)) = *(reinterpret_cast<VariantMap*>(rhs.value_.ptr_));
  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_NONE:
  90. return true;
  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. return *(reinterpret_cast<const Vector4*>(&value_)) == *(reinterpret_cast<const Vector4*>(&rhs.value_));
  103. case VAR_QUATERNION:
  104. return *(reinterpret_cast<const Quaternion*>(&value_)) == *(reinterpret_cast<const Quaternion*>(&rhs.value_));
  105. case VAR_COLOR:
  106. return *(reinterpret_cast<const Color*>(&value_)) == *(reinterpret_cast<const Color*>(&rhs.value_));
  107. case VAR_STRING:
  108. return *(reinterpret_cast<const String*>(value_.ptr_)) == *(reinterpret_cast<const String*>(rhs.value_.ptr_));
  109. case VAR_BUFFER:
  110. return *(reinterpret_cast<const PODVector<unsigned char>*>(value_.ptr_)) == *(reinterpret_cast<const PODVector<unsigned char>*>(rhs.value_.ptr_));
  111. case VAR_PTR:
  112. return value_.ptr_ == rhs.value_.ptr_;
  113. case VAR_RESOURCEREF:
  114. return *(reinterpret_cast<const ResourceRef*>(value_.ptr_)) == *(reinterpret_cast<const ResourceRef*>(rhs.value_.ptr_));
  115. case VAR_RESOURCEREFLIST:
  116. return *(reinterpret_cast<const ResourceRefList*>(value_.ptr_)) == *(reinterpret_cast<const ResourceRefList*>(rhs.value_.ptr_));
  117. case VAR_VARIANTVECTOR:
  118. return *(reinterpret_cast<const VariantVector*>(value_.ptr_)) == *(reinterpret_cast<const VariantVector*>(rhs.value_.ptr_));
  119. case VAR_VARIANTMAP:
  120. return *(reinterpret_cast<const VariantMap*>(value_.ptr_)) == *(reinterpret_cast<const VariantMap*>(rhs.value_.ptr_));
  121. default:
  122. return true;
  123. }
  124. }
  125. void Variant::FromString(const String& type, const String& value)
  126. {
  127. String typeLower = type.ToLower();
  128. if (typeLower == "none")
  129. SetType(VAR_NONE);
  130. else if (typeLower == "int")
  131. *this = ToInt(value);
  132. else if (typeLower == "bool")
  133. *this = ToBool(value);
  134. else if (typeLower == "float")
  135. *this = ToFloat(value);
  136. else if (typeLower == "vector2")
  137. *this = ToVector2(value);
  138. else if (typeLower == "vector3")
  139. *this = ToVector3(value);
  140. else if (typeLower == "vector4")
  141. *this = ToVector4(value);
  142. else if (typeLower == "quaternion")
  143. *this = ToQuaternion(value);
  144. else if (typeLower == "color")
  145. *this = ToColor(value);
  146. else if (typeLower == "string")
  147. *this = value;
  148. else if (typeLower == "buffer")
  149. {
  150. SetType(VAR_BUFFER);
  151. PODVector<unsigned char>& buffer = *(reinterpret_cast<PODVector<unsigned char>*>(value_.ptr_));
  152. Vector<String> values = value.Split(' ');
  153. buffer.Resize(values.Size());
  154. for (unsigned i = 0; i < values.Size(); ++i)
  155. buffer[i] = ToInt(values[i]);
  156. }
  157. else if (typeLower == "pointer")
  158. {
  159. *this = (void*)0;
  160. }
  161. else if (typeLower == "objectref")
  162. {
  163. Vector<String> values = value.Split(';');
  164. if (values.Size() == 2)
  165. {
  166. SetType(VAR_RESOURCEREF);
  167. ResourceRef& ref = *(reinterpret_cast<ResourceRef*>(value_.ptr_));
  168. ref.type_ = ShortStringHash(values[0]);
  169. ref.id_ = StringHash(values[1]);
  170. }
  171. }
  172. else if (typeLower == "objectreflist")
  173. {
  174. Vector<String> values = value.Split(';');
  175. if (values.Size() >= 1)
  176. {
  177. SetType(VAR_RESOURCEREFLIST);
  178. ResourceRefList& refList = *(reinterpret_cast<ResourceRefList*>(value_.ptr_));
  179. refList.type_ = ShortStringHash(values[0]);
  180. refList.ids_.Resize(values.Size() - 1);
  181. for (unsigned i = 1; i < values.Size(); ++i)
  182. refList.ids_[i - 1] = StringHash(values[i]);
  183. }
  184. }
  185. else
  186. SetType(VAR_NONE);
  187. }
  188. void Variant::SetBuffer(const void* data, unsigned size)
  189. {
  190. if (size && !data)
  191. size = 0;
  192. SetType(VAR_BUFFER);
  193. PODVector<unsigned char>& buffer = *(reinterpret_cast<PODVector<unsigned char>*>(value_.ptr_));
  194. buffer.Resize(size);
  195. if (size)
  196. memcpy(&buffer[0], data, size);
  197. }
  198. const String& Variant::GetTypeName() const
  199. {
  200. return typeNames[type_];
  201. }
  202. String Variant::ToString() const
  203. {
  204. switch (type_)
  205. {
  206. case VAR_INT:
  207. return String(value_.int_);
  208. case VAR_BOOL:
  209. return String(value_.bool_);
  210. case VAR_FLOAT:
  211. return String(value_.float_);
  212. case VAR_VECTOR2:
  213. return (reinterpret_cast<const Vector2*>(&value_))->ToString();
  214. case VAR_VECTOR3:
  215. return (reinterpret_cast<const Vector3*>(&value_))->ToString();
  216. case VAR_VECTOR4:
  217. return (reinterpret_cast<const Vector4*>(&value_))->ToString();
  218. case VAR_QUATERNION:
  219. return (reinterpret_cast<const Quaternion*>(&value_))->ToString();
  220. case VAR_COLOR:
  221. return (reinterpret_cast<const Color*>(&value_))->ToString();
  222. case VAR_STRING:
  223. return *(reinterpret_cast<const String*>(value_.ptr_));
  224. case VAR_BUFFER:
  225. {
  226. const PODVector<unsigned char>& buffer = *(reinterpret_cast<const PODVector<unsigned char>*>(value_.ptr_));
  227. String ret;
  228. for (PODVector<unsigned char>::ConstIterator i = buffer.Begin(); i != buffer.End(); ++i)
  229. {
  230. if (i != buffer.Begin())
  231. ret += " ";
  232. ret += String((unsigned)*i);
  233. }
  234. return ret;
  235. }
  236. case VAR_PTR:
  237. // Pointer serialization not supported (convert to null)
  238. return String(0);
  239. case VAR_RESOURCEREF:
  240. case VAR_RESOURCEREFLIST:
  241. case VAR_VARIANTVECTOR:
  242. case VAR_VARIANTMAP:
  243. // Reference string serialization requires hash-to-name mapping from the context & subsystems. Can not support here
  244. // Also variant map or vector string serialization is not supported. XML or binary save should be used instead
  245. return String();
  246. default:
  247. return String();
  248. }
  249. }
  250. void Variant::SetType(VariantType newType)
  251. {
  252. if (type_ == newType)
  253. return;
  254. switch (type_)
  255. {
  256. case VAR_STRING:
  257. delete reinterpret_cast<String*>(value_.ptr_);
  258. break;
  259. case VAR_BUFFER:
  260. delete reinterpret_cast<PODVector<unsigned char>*>(value_.ptr_);
  261. break;
  262. case VAR_RESOURCEREF:
  263. delete reinterpret_cast<ResourceRef*>(value_.ptr_);
  264. break;
  265. case VAR_RESOURCEREFLIST:
  266. delete reinterpret_cast<ResourceRefList*>(value_.ptr_);
  267. break;
  268. case VAR_VARIANTVECTOR:
  269. delete reinterpret_cast<VariantVector*>(value_.ptr_);
  270. break;
  271. case VAR_VARIANTMAP:
  272. delete reinterpret_cast<VariantMap*>(value_.ptr_);
  273. break;
  274. default:
  275. break;
  276. }
  277. type_ = newType;
  278. switch (type_)
  279. {
  280. case VAR_STRING:
  281. *reinterpret_cast<String**>(&value_.ptr_) = new String();
  282. break;
  283. case VAR_BUFFER:
  284. *reinterpret_cast<PODVector<unsigned char>**>(&value_.ptr_) = new PODVector<unsigned char>();
  285. break;
  286. case VAR_RESOURCEREF:
  287. *reinterpret_cast<ResourceRef**>(&value_.ptr_) = new ResourceRef();
  288. break;
  289. case VAR_RESOURCEREFLIST:
  290. *reinterpret_cast<ResourceRefList**>(&value_.ptr_) = new ResourceRefList();
  291. break;
  292. case VAR_VARIANTVECTOR:
  293. *reinterpret_cast<VariantVector**>(&value_.ptr_) = new VariantVector();
  294. break;
  295. case VAR_VARIANTMAP:
  296. *reinterpret_cast<VariantMap**>(&value_.ptr_) = new VariantMap();
  297. break;
  298. default:
  299. break;
  300. }
  301. }
  302. template<> int Variant::Get<int>() const
  303. {
  304. return GetInt();
  305. }
  306. template<> unsigned Variant::Get<unsigned>() const
  307. {
  308. return GetUInt();
  309. }
  310. template<> StringHash Variant::Get<StringHash>() const
  311. {
  312. return GetStringHash();
  313. }
  314. template<> ShortStringHash Variant::Get<ShortStringHash>() const
  315. {
  316. return GetShortStringHash();
  317. }
  318. template<> bool Variant::Get<bool>() const
  319. {
  320. return GetBool();
  321. }
  322. template<> float Variant::Get<float>() const
  323. {
  324. return GetFloat();
  325. }
  326. template<> const Vector2& Variant::Get<const Vector2&>() const
  327. {
  328. return GetVector2();
  329. }
  330. template<> const Vector3& Variant::Get<const Vector3&>() const
  331. {
  332. return GetVector3();
  333. }
  334. template<> const Vector4& Variant::Get<const Vector4&>() const
  335. {
  336. return GetVector4();
  337. }
  338. template<> const Quaternion& Variant::Get<const Quaternion&>() const
  339. {
  340. return GetQuaternion();
  341. }
  342. template<> const Color& Variant::Get<const Color&>() const
  343. {
  344. return GetColor();
  345. }
  346. template<> const String& Variant::Get<const String&>() const
  347. {
  348. return GetString();
  349. }
  350. template<> const PODVector<unsigned char>& Variant::Get<const PODVector<unsigned char>& >() const
  351. {
  352. return GetBuffer();
  353. }
  354. template<> void* Variant::Get<void*>() const
  355. {
  356. return GetPtr();
  357. }
  358. template<> ResourceRef Variant::Get<ResourceRef>() const
  359. {
  360. return GetResourceRef();
  361. }
  362. template<> ResourceRefList Variant::Get<ResourceRefList>() const
  363. {
  364. return GetResourceRefList();
  365. }
  366. template<> VariantVector Variant::Get<VariantVector>() const
  367. {
  368. return GetVariantVector();
  369. }
  370. template<> VariantMap Variant::Get<VariantMap>() const
  371. {
  372. return GetVariantMap();
  373. }
  374. template<> Vector2 Variant::Get<Vector2>() const
  375. {
  376. return GetVector2();
  377. }
  378. template<> Vector3 Variant::Get<Vector3>() const
  379. {
  380. return GetVector3();
  381. }
  382. template<> Vector4 Variant::Get<Vector4>() const
  383. {
  384. return GetVector4();
  385. }
  386. template<> Quaternion Variant::Get<Quaternion>() const
  387. {
  388. return GetQuaternion();
  389. }
  390. template<> Color Variant::Get<Color>() const
  391. {
  392. return GetColor();
  393. }
  394. template<> String Variant::Get<String>() const
  395. {
  396. return GetString();
  397. }
  398. template<> PODVector<unsigned char> Variant::Get<PODVector<unsigned char> >() const
  399. {
  400. return GetBuffer();
  401. }
  402. const String& Variant::GetTypeName(VariantType type)
  403. {
  404. return typeNames[type];
  405. }