Variant.cpp 14 KB

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