Variant.cpp 14 KB

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