Variant.cpp 14 KB

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