Variant.cpp 14 KB

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