Variant.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/StringUtils.h"
  24. #include "../IO/VectorBuffer.h"
  25. #include <cstring>
  26. namespace Atomic
  27. {
  28. const Variant Variant::EMPTY;
  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. const StringVector Variant::emptyStringVector;
  35. static const char* 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. "VoidPtr",
  49. "ResourceRef",
  50. "ResourceRefList",
  51. "VariantVector",
  52. "VariantMap",
  53. "IntRect",
  54. "IntVector2",
  55. "Ptr",
  56. "Matrix3",
  57. "Matrix3x4",
  58. "Matrix4",
  59. "Double",
  60. "StringVector",
  61. 0
  62. };
  63. Variant& Variant::operator =(const Variant& rhs)
  64. {
  65. SetType(rhs.GetType());
  66. switch (type_)
  67. {
  68. case VAR_STRING:
  69. *(reinterpret_cast<String*>(&value_)) = *(reinterpret_cast<const String*>(&rhs.value_));
  70. break;
  71. case VAR_BUFFER:
  72. *(reinterpret_cast<PODVector<unsigned char>*>(&value_)) = *(reinterpret_cast<const PODVector<unsigned char>*>(&rhs.value_));
  73. break;
  74. case VAR_RESOURCEREF:
  75. *(reinterpret_cast<ResourceRef*>(&value_)) = *(reinterpret_cast<const ResourceRef*>(&rhs.value_));
  76. break;
  77. case VAR_RESOURCEREFLIST:
  78. *(reinterpret_cast<ResourceRefList*>(&value_)) = *(reinterpret_cast<const ResourceRefList*>(&rhs.value_));
  79. break;
  80. case VAR_VARIANTVECTOR:
  81. *(reinterpret_cast<VariantVector*>(&value_)) = *(reinterpret_cast<const VariantVector*>(&rhs.value_));
  82. break;
  83. case VAR_STRINGVECTOR:
  84. *(reinterpret_cast<StringVector*>(&value_)) = *(reinterpret_cast<const StringVector*>(&rhs.value_));
  85. break;
  86. case VAR_VARIANTMAP:
  87. *(reinterpret_cast<VariantMap*>(&value_)) = *(reinterpret_cast<const VariantMap*>(&rhs.value_));
  88. break;
  89. case VAR_PTR:
  90. *(reinterpret_cast<WeakPtr<RefCounted>*>(&value_)) = *(reinterpret_cast<const WeakPtr<RefCounted>*>(&rhs.value_));
  91. break;
  92. case VAR_MATRIX3:
  93. *(reinterpret_cast<Matrix3*>(value_.ptr_)) = *(reinterpret_cast<const Matrix3*>(rhs.value_.ptr_));
  94. break;
  95. case VAR_MATRIX3X4:
  96. *(reinterpret_cast<Matrix3x4*>(value_.ptr_)) = *(reinterpret_cast<const Matrix3x4*>(rhs.value_.ptr_));
  97. break;
  98. case VAR_MATRIX4:
  99. *(reinterpret_cast<Matrix4*>(value_.ptr_)) = *(reinterpret_cast<const Matrix4*>(rhs.value_.ptr_));
  100. break;
  101. default:
  102. value_ = rhs.value_;
  103. break;
  104. }
  105. return *this;
  106. }
  107. Variant& Variant::operator =(const VectorBuffer& rhs)
  108. {
  109. SetType(VAR_BUFFER);
  110. *(reinterpret_cast<PODVector<unsigned char>*>(&value_)) = rhs.GetBuffer();
  111. return *this;
  112. }
  113. bool Variant::operator ==(const Variant& rhs) const
  114. {
  115. if (type_ == VAR_VOIDPTR || type_ == VAR_PTR)
  116. return GetVoidPtr() == rhs.GetVoidPtr();
  117. else if (type_ != rhs.type_)
  118. return false;
  119. switch (type_)
  120. {
  121. case VAR_INT:
  122. return value_.int_ == rhs.value_.int_;
  123. case VAR_BOOL:
  124. return value_.bool_ == rhs.value_.bool_;
  125. case VAR_FLOAT:
  126. return value_.float_ == rhs.value_.float_;
  127. case VAR_VECTOR2:
  128. return *(reinterpret_cast<const Vector2*>(&value_)) == *(reinterpret_cast<const Vector2*>(&rhs.value_));
  129. case VAR_VECTOR3:
  130. return *(reinterpret_cast<const Vector3*>(&value_)) == *(reinterpret_cast<const Vector3*>(&rhs.value_));
  131. case VAR_VECTOR4:
  132. case VAR_QUATERNION:
  133. case VAR_COLOR:
  134. // Hack: use the Vector4 compare for all these classes, as they have the same memory structure
  135. return *(reinterpret_cast<const Vector4*>(&value_)) == *(reinterpret_cast<const Vector4*>(&rhs.value_));
  136. case VAR_STRING:
  137. return *(reinterpret_cast<const String*>(&value_)) == *(reinterpret_cast<const String*>(&rhs.value_));
  138. case VAR_BUFFER:
  139. return *(reinterpret_cast<const PODVector<unsigned char>*>(&value_)) ==
  140. *(reinterpret_cast<const PODVector<unsigned char>*>(&rhs.value_));
  141. case VAR_RESOURCEREF:
  142. return *(reinterpret_cast<const ResourceRef*>(&value_)) == *(reinterpret_cast<const ResourceRef*>(&rhs.value_));
  143. case VAR_RESOURCEREFLIST:
  144. return *(reinterpret_cast<const ResourceRefList*>(&value_)) == *(reinterpret_cast<const ResourceRefList*>(&rhs.value_));
  145. case VAR_VARIANTVECTOR:
  146. return *(reinterpret_cast<const VariantVector*>(&value_)) == *(reinterpret_cast<const VariantVector*>(&rhs.value_));
  147. case VAR_STRINGVECTOR:
  148. return *(reinterpret_cast<const StringVector*>(&value_)) == *(reinterpret_cast<const StringVector*>(&rhs.value_));
  149. case VAR_VARIANTMAP:
  150. return *(reinterpret_cast<const VariantMap*>(&value_)) == *(reinterpret_cast<const VariantMap*>(&rhs.value_));
  151. case VAR_INTRECT:
  152. return *(reinterpret_cast<const IntRect*>(&value_)) == *(reinterpret_cast<const IntRect*>(&rhs.value_));
  153. case VAR_INTVECTOR2:
  154. return *(reinterpret_cast<const IntVector2*>(&value_)) == *(reinterpret_cast<const IntVector2*>(&rhs.value_));
  155. case VAR_MATRIX3:
  156. return *(reinterpret_cast<const Matrix3*>(value_.ptr_)) == *(reinterpret_cast<const Matrix3*>(rhs.value_.ptr_));
  157. case VAR_MATRIX3X4:
  158. return *(reinterpret_cast<const Matrix3x4*>(value_.ptr_)) == *(reinterpret_cast<const Matrix3x4*>(rhs.value_.ptr_));
  159. case VAR_MATRIX4:
  160. return *(reinterpret_cast<const Matrix4*>(value_.ptr_)) == *(reinterpret_cast<const Matrix4*>(rhs.value_.ptr_));
  161. case VAR_DOUBLE:
  162. return *(reinterpret_cast<const double*>(&value_)) == *(reinterpret_cast<const double*>(&rhs.value_));
  163. default:
  164. return true;
  165. }
  166. }
  167. bool Variant::operator ==(const PODVector<unsigned char>& rhs) const
  168. {
  169. // Use strncmp() instead of PODVector<unsigned char>::operator ==()
  170. const PODVector<unsigned char>& buffer = *(reinterpret_cast<const PODVector<unsigned char>*>(&value_));
  171. return type_ == VAR_BUFFER && buffer.Size() == rhs.Size() ?
  172. strncmp(reinterpret_cast<const char*>(&buffer[0]), reinterpret_cast<const char*>(&rhs[0]), buffer.Size()) == 0 :
  173. false;
  174. }
  175. bool Variant::operator ==(const VectorBuffer& rhs) const
  176. {
  177. const PODVector<unsigned char>& buffer = *(reinterpret_cast<const PODVector<unsigned char>*>(&value_));
  178. return type_ == VAR_BUFFER && buffer.Size() == rhs.GetSize() ?
  179. strncmp(reinterpret_cast<const char*>(&buffer[0]), reinterpret_cast<const char*>(rhs.GetData()), buffer.Size()) == 0 :
  180. false;
  181. }
  182. void Variant::FromString(const String& type, const String& value)
  183. {
  184. return FromString(GetTypeFromName(type), value.CString());
  185. }
  186. void Variant::FromString(const char* type, const char* value)
  187. {
  188. return FromString(GetTypeFromName(type), value);
  189. }
  190. void Variant::FromString(VariantType type, const String& value)
  191. {
  192. return FromString(type, value.CString());
  193. }
  194. void Variant::FromString(VariantType type, const char* value)
  195. {
  196. switch (type)
  197. {
  198. case VAR_INT:
  199. *this = ToInt(value);
  200. break;
  201. case VAR_BOOL:
  202. *this = ToBool(value);
  203. break;
  204. case VAR_FLOAT:
  205. *this = ToFloat(value);
  206. break;
  207. case VAR_VECTOR2:
  208. *this = ToVector2(value);
  209. break;
  210. case VAR_VECTOR3:
  211. *this = ToVector3(value);
  212. break;
  213. case VAR_VECTOR4:
  214. *this = ToVector4(value);
  215. break;
  216. case VAR_QUATERNION:
  217. *this = ToQuaternion(value);
  218. break;
  219. case VAR_COLOR:
  220. *this = ToColor(value);
  221. break;
  222. case VAR_STRING:
  223. *this = value;
  224. break;
  225. case VAR_BUFFER:
  226. {
  227. SetType(VAR_BUFFER);
  228. PODVector<unsigned char>& buffer = *(reinterpret_cast<PODVector<unsigned char>*>(&value_));
  229. StringToBuffer(buffer, value);
  230. }
  231. break;
  232. case VAR_VOIDPTR:
  233. // From string to void pointer not supported, set to null
  234. *this = (void*)0;
  235. break;
  236. case VAR_RESOURCEREF:
  237. {
  238. StringVector values = String::Split(value, ';');
  239. if (values.Size() == 2)
  240. {
  241. SetType(VAR_RESOURCEREF);
  242. ResourceRef& ref = *(reinterpret_cast<ResourceRef*>(&value_));
  243. ref.type_ = values[0];
  244. ref.name_ = values[1];
  245. }
  246. }
  247. break;
  248. case VAR_RESOURCEREFLIST:
  249. {
  250. StringVector values = String::Split(value, ';');
  251. if (values.Size() >= 1)
  252. {
  253. SetType(VAR_RESOURCEREFLIST);
  254. ResourceRefList& refList = *(reinterpret_cast<ResourceRefList*>(&value_));
  255. refList.type_ = values[0];
  256. refList.names_.Resize(values.Size() - 1);
  257. for (unsigned i = 1; i < values.Size(); ++i)
  258. refList.names_[i - 1] = values[i];
  259. }
  260. }
  261. break;
  262. case VAR_INTRECT:
  263. *this = ToIntRect(value);
  264. break;
  265. case VAR_INTVECTOR2:
  266. *this = ToIntVector2(value);
  267. break;
  268. case VAR_PTR:
  269. // From string to RefCounted pointer not supported, set to null
  270. *this = (RefCounted*)0;
  271. break;
  272. case VAR_MATRIX3:
  273. *this = ToMatrix3(value);
  274. break;
  275. case VAR_MATRIX3X4:
  276. *this = ToMatrix3x4(value);
  277. break;
  278. case VAR_MATRIX4:
  279. *this = ToMatrix4(value);
  280. break;
  281. case VAR_DOUBLE:
  282. *this = ToDouble(value);
  283. break;
  284. default:
  285. SetType(VAR_NONE);
  286. }
  287. }
  288. void Variant::SetBuffer(const void* data, unsigned size)
  289. {
  290. if (size && !data)
  291. size = 0;
  292. SetType(VAR_BUFFER);
  293. PODVector<unsigned char>& buffer = *(reinterpret_cast<PODVector<unsigned char>*>(&value_));
  294. buffer.Resize(size);
  295. if (size)
  296. memcpy(&buffer[0], data, size);
  297. }
  298. const VectorBuffer Variant::GetVectorBuffer() const
  299. {
  300. return VectorBuffer(type_ == VAR_BUFFER ? *reinterpret_cast<const PODVector<unsigned char>*>(&value_) : emptyBuffer);
  301. }
  302. String Variant::GetTypeName() const
  303. {
  304. return typeNames[type_];
  305. }
  306. String Variant::ToString() const
  307. {
  308. switch (type_)
  309. {
  310. case VAR_INT:
  311. return String(value_.int_);
  312. case VAR_BOOL:
  313. return String(value_.bool_);
  314. case VAR_FLOAT:
  315. return String(value_.float_);
  316. case VAR_VECTOR2:
  317. return (reinterpret_cast<const Vector2*>(&value_))->ToString();
  318. case VAR_VECTOR3:
  319. return (reinterpret_cast<const Vector3*>(&value_))->ToString();
  320. case VAR_VECTOR4:
  321. return (reinterpret_cast<const Vector4*>(&value_))->ToString();
  322. case VAR_QUATERNION:
  323. return (reinterpret_cast<const Quaternion*>(&value_))->ToString();
  324. case VAR_COLOR:
  325. return (reinterpret_cast<const Color*>(&value_))->ToString();
  326. case VAR_STRING:
  327. return *(reinterpret_cast<const String*>(&value_));
  328. case VAR_BUFFER:
  329. {
  330. const PODVector<unsigned char>& buffer = *(reinterpret_cast<const PODVector<unsigned char>*>(&value_));
  331. String ret;
  332. BufferToString(ret, buffer.Begin().ptr_, buffer.Size());
  333. return ret;
  334. }
  335. case VAR_VOIDPTR:
  336. case VAR_PTR:
  337. // Pointer serialization not supported (convert to null)
  338. return String(0);
  339. case VAR_INTRECT:
  340. return (reinterpret_cast<const IntRect*>(&value_))->ToString();
  341. case VAR_INTVECTOR2:
  342. return (reinterpret_cast<const IntVector2*>(&value_))->ToString();
  343. case VAR_MATRIX3:
  344. return (reinterpret_cast<const Matrix3*>(value_.ptr_))->ToString();
  345. case VAR_MATRIX3X4:
  346. return (reinterpret_cast<const Matrix3x4*>(value_.ptr_))->ToString();
  347. case VAR_MATRIX4:
  348. return (reinterpret_cast<const Matrix4*>(value_.ptr_))->ToString();
  349. case VAR_DOUBLE:
  350. return String(*reinterpret_cast<const double*>(&value_));
  351. default:
  352. // VAR_RESOURCEREF, VAR_RESOURCEREFLIST, VAR_VARIANTVECTOR, VAR_STRINGVECTOR, VAR_VARIANTMAP
  353. // Reference string serialization requires typehash-to-name mapping from the context. Can not support here
  354. // Also variant map or vector string serialization is not supported. XML or binary save should be used instead
  355. return String::EMPTY;
  356. }
  357. }
  358. bool Variant::IsZero() const
  359. {
  360. switch (type_)
  361. {
  362. case VAR_INT:
  363. return value_.int_ == 0;
  364. case VAR_BOOL:
  365. return value_.bool_ == false;
  366. case VAR_FLOAT:
  367. return value_.float_ == 0.0f;
  368. case VAR_VECTOR2:
  369. return *reinterpret_cast<const Vector2*>(&value_) == Vector2::ZERO;
  370. case VAR_VECTOR3:
  371. return *reinterpret_cast<const Vector3*>(&value_) == Vector3::ZERO;
  372. case VAR_VECTOR4:
  373. return *reinterpret_cast<const Vector4*>(&value_) == Vector4::ZERO;
  374. case VAR_QUATERNION:
  375. return *reinterpret_cast<const Quaternion*>(&value_) == Quaternion::IDENTITY;
  376. case VAR_COLOR:
  377. // WHITE is considered empty (i.e. default) color in the Color class definition
  378. return *reinterpret_cast<const Color*>(&value_) == Color::WHITE;
  379. case VAR_STRING:
  380. return reinterpret_cast<const String*>(&value_)->Empty();
  381. case VAR_BUFFER:
  382. return reinterpret_cast<const PODVector<unsigned char>*>(&value_)->Empty();
  383. case VAR_VOIDPTR:
  384. return value_.ptr_ == 0;
  385. case VAR_RESOURCEREF:
  386. return reinterpret_cast<const ResourceRef*>(&value_)->name_.Empty();
  387. case VAR_RESOURCEREFLIST:
  388. {
  389. const StringVector& names = reinterpret_cast<const ResourceRefList*>(&value_)->names_;
  390. for (StringVector::ConstIterator i = names.Begin(); i != names.End(); ++i)
  391. {
  392. if (!i->Empty())
  393. return false;
  394. }
  395. return true;
  396. }
  397. case VAR_VARIANTVECTOR:
  398. return reinterpret_cast<const VariantVector*>(&value_)->Empty();
  399. case VAR_STRINGVECTOR:
  400. return reinterpret_cast<const StringVector*>(&value_)->Empty();
  401. case VAR_VARIANTMAP:
  402. return reinterpret_cast<const VariantMap*>(&value_)->Empty();
  403. case VAR_INTRECT:
  404. return *reinterpret_cast<const IntRect*>(&value_) == IntRect::ZERO;
  405. case VAR_INTVECTOR2:
  406. return *reinterpret_cast<const IntVector2*>(&value_) == IntVector2::ZERO;
  407. case VAR_PTR:
  408. return *reinterpret_cast<const WeakPtr<RefCounted>*>(&value_) == (RefCounted*)0;
  409. case VAR_MATRIX3:
  410. return *reinterpret_cast<const Matrix3*>(value_.ptr_) == Matrix3::IDENTITY;
  411. case VAR_MATRIX3X4:
  412. return *reinterpret_cast<const Matrix3x4*>(value_.ptr_) == Matrix3x4::IDENTITY;
  413. case VAR_MATRIX4:
  414. return *reinterpret_cast<const Matrix4*>(value_.ptr_) == Matrix4::IDENTITY;
  415. case VAR_DOUBLE:
  416. return *reinterpret_cast<const double*>(&value_) == 0.0;
  417. default:
  418. return true;
  419. }
  420. }
  421. void Variant::SetType(VariantType newType)
  422. {
  423. if (type_ == newType)
  424. return;
  425. switch (type_)
  426. {
  427. case VAR_STRING:
  428. (reinterpret_cast<String*>(&value_))->~String();
  429. break;
  430. case VAR_BUFFER:
  431. (reinterpret_cast<PODVector<unsigned char>*>(&value_))->~PODVector<unsigned char>();
  432. break;
  433. case VAR_RESOURCEREF:
  434. (reinterpret_cast<ResourceRef*>(&value_))->~ResourceRef();
  435. break;
  436. case VAR_RESOURCEREFLIST:
  437. (reinterpret_cast<ResourceRefList*>(&value_))->~ResourceRefList();
  438. break;
  439. case VAR_VARIANTVECTOR:
  440. (reinterpret_cast<VariantVector*>(&value_))->~VariantVector();
  441. break;
  442. case VAR_STRINGVECTOR:
  443. (reinterpret_cast<StringVector*>(&value_))->~StringVector();
  444. break;
  445. case VAR_VARIANTMAP:
  446. (reinterpret_cast<VariantMap*>(&value_))->~VariantMap();
  447. break;
  448. case VAR_PTR:
  449. (reinterpret_cast<WeakPtr<RefCounted>*>(&value_))->~WeakPtr<RefCounted>();
  450. break;
  451. case VAR_MATRIX3:
  452. delete reinterpret_cast<Matrix3*>(value_.ptr_);
  453. break;
  454. case VAR_MATRIX3X4:
  455. delete reinterpret_cast<Matrix3x4*>(value_.ptr_);
  456. break;
  457. case VAR_MATRIX4:
  458. delete reinterpret_cast<Matrix4*>(value_.ptr_);
  459. break;
  460. default:
  461. break;
  462. }
  463. type_ = newType;
  464. switch (type_)
  465. {
  466. case VAR_STRING:
  467. new(reinterpret_cast<String*>(&value_)) String();
  468. break;
  469. case VAR_BUFFER:
  470. new(reinterpret_cast<PODVector<unsigned char>*>(&value_)) PODVector<unsigned char>();
  471. break;
  472. case VAR_RESOURCEREF:
  473. new(reinterpret_cast<ResourceRef*>(&value_)) ResourceRef();
  474. break;
  475. case VAR_RESOURCEREFLIST:
  476. new(reinterpret_cast<ResourceRefList*>(&value_)) ResourceRefList();
  477. break;
  478. case VAR_VARIANTVECTOR:
  479. new(reinterpret_cast<VariantVector*>(&value_)) VariantVector();
  480. break;
  481. case VAR_STRINGVECTOR:
  482. new(reinterpret_cast<StringVector*>(&value_)) StringVector();
  483. break;
  484. case VAR_VARIANTMAP:
  485. new(reinterpret_cast<VariantMap*>(&value_)) VariantMap();
  486. break;
  487. case VAR_PTR:
  488. new(reinterpret_cast<WeakPtr<RefCounted>*>(&value_)) WeakPtr<RefCounted>();
  489. break;
  490. case VAR_MATRIX3:
  491. value_.ptr_ = new Matrix3();
  492. break;
  493. case VAR_MATRIX3X4:
  494. value_.ptr_ = new Matrix3x4();
  495. break;
  496. case VAR_MATRIX4:
  497. value_.ptr_ = new Matrix4();
  498. break;
  499. default:
  500. break;
  501. }
  502. }
  503. template <> int Variant::Get<int>() const
  504. {
  505. return GetInt();
  506. }
  507. template <> unsigned Variant::Get<unsigned>() const
  508. {
  509. return GetUInt();
  510. }
  511. template <> StringHash Variant::Get<StringHash>() const
  512. {
  513. return GetStringHash();
  514. }
  515. template <> bool Variant::Get<bool>() const
  516. {
  517. return GetBool();
  518. }
  519. template <> float Variant::Get<float>() const
  520. {
  521. return GetFloat();
  522. }
  523. template <> double Variant::Get<double>() const
  524. {
  525. return GetDouble();
  526. }
  527. template <> const Vector2& Variant::Get<const Vector2&>() const
  528. {
  529. return GetVector2();
  530. }
  531. template <> const Vector3& Variant::Get<const Vector3&>() const
  532. {
  533. return GetVector3();
  534. }
  535. template <> const Vector4& Variant::Get<const Vector4&>() const
  536. {
  537. return GetVector4();
  538. }
  539. template <> const Quaternion& Variant::Get<const Quaternion&>() const
  540. {
  541. return GetQuaternion();
  542. }
  543. template <> const Color& Variant::Get<const Color&>() const
  544. {
  545. return GetColor();
  546. }
  547. template <> const String& Variant::Get<const String&>() const
  548. {
  549. return GetString();
  550. }
  551. template <> const IntRect& Variant::Get<const IntRect&>() const
  552. {
  553. return GetIntRect();
  554. }
  555. template <> const IntVector2& Variant::Get<const IntVector2&>() const
  556. {
  557. return GetIntVector2();
  558. }
  559. template <> const PODVector<unsigned char>& Variant::Get<const PODVector<unsigned char>&>() const
  560. {
  561. return GetBuffer();
  562. }
  563. template <> void* Variant::Get<void*>() const
  564. {
  565. return GetVoidPtr();
  566. }
  567. template <> RefCounted* Variant::Get<RefCounted*>() const
  568. {
  569. return GetPtr();
  570. }
  571. template <> const Matrix3& Variant::Get<const Matrix3&>() const
  572. {
  573. return GetMatrix3();
  574. }
  575. template <> const Matrix3x4& Variant::Get<const Matrix3x4&>() const
  576. {
  577. return GetMatrix3x4();
  578. }
  579. template <> const Matrix4& Variant::Get<const Matrix4&>() const
  580. {
  581. return GetMatrix4();
  582. }
  583. template <> ResourceRef Variant::Get<ResourceRef>() const
  584. {
  585. return GetResourceRef();
  586. }
  587. template <> ResourceRefList Variant::Get<ResourceRefList>() const
  588. {
  589. return GetResourceRefList();
  590. }
  591. template <> VariantVector Variant::Get<VariantVector>() const
  592. {
  593. return GetVariantVector();
  594. }
  595. template <> StringVector Variant::Get<StringVector >() const
  596. {
  597. return GetStringVector();
  598. }
  599. template <> VariantMap Variant::Get<VariantMap>() const
  600. {
  601. return GetVariantMap();
  602. }
  603. template <> Vector2 Variant::Get<Vector2>() const
  604. {
  605. return GetVector2();
  606. }
  607. template <> Vector3 Variant::Get<Vector3>() const
  608. {
  609. return GetVector3();
  610. }
  611. template <> Vector4 Variant::Get<Vector4>() const
  612. {
  613. return GetVector4();
  614. }
  615. template <> Quaternion Variant::Get<Quaternion>() const
  616. {
  617. return GetQuaternion();
  618. }
  619. template <> Color Variant::Get<Color>() const
  620. {
  621. return GetColor();
  622. }
  623. template <> String Variant::Get<String>() const
  624. {
  625. return GetString();
  626. }
  627. template <> IntRect Variant::Get<IntRect>() const
  628. {
  629. return GetIntRect();
  630. }
  631. template <> IntVector2 Variant::Get<IntVector2>() const
  632. {
  633. return GetIntVector2();
  634. }
  635. template <> PODVector<unsigned char> Variant::Get<PODVector<unsigned char> >() const
  636. {
  637. return GetBuffer();
  638. }
  639. template <> Matrix3 Variant::Get<Matrix3>() const
  640. {
  641. return GetMatrix3();
  642. }
  643. template <> Matrix3x4 Variant::Get<Matrix3x4>() const
  644. {
  645. return GetMatrix3x4();
  646. }
  647. template <> Matrix4 Variant::Get<Matrix4>() const
  648. {
  649. return GetMatrix4();
  650. }
  651. String Variant::GetTypeName(VariantType type)
  652. {
  653. return typeNames[type];
  654. }
  655. VariantType Variant::GetTypeFromName(const String& typeName)
  656. {
  657. return GetTypeFromName(typeName.CString());
  658. }
  659. VariantType Variant::GetTypeFromName(const char* typeName)
  660. {
  661. return (VariantType)GetStringListIndex(typeName, typeNames, VAR_NONE);
  662. }
  663. }