ScriptVector.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  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 "../Atomic/IO/Log.h"
  23. #include "ScriptSystem.h"
  24. #include "ScriptVariant.h"
  25. #pragma once
  26. namespace Atomic
  27. {
  28. class ScriptVector : public RefCounted
  29. {
  30. ATOMIC_REFCOUNTED(ScriptVector)
  31. public:
  32. ScriptVector() : RefCounted()
  33. {
  34. }
  35. virtual ~ScriptVector()
  36. {
  37. }
  38. RefCounted* At(unsigned index) const
  39. {
  40. if (index >= refVector_.Size())
  41. return 0;
  42. return refVector_[index];
  43. }
  44. void Push(RefCounted* refCounted)
  45. {
  46. // TODO: check null?
  47. refVector_.Push(SharedPtr<RefCounted>(refCounted));
  48. }
  49. void SetAt(unsigned index, RefCounted* refCounted)
  50. {
  51. refVector_[index] = refCounted;
  52. }
  53. void Insert(unsigned index, RefCounted* refCounted)
  54. {
  55. refVector_.Insert(index, SharedPtr<RefCounted>(refCounted));
  56. }
  57. void Erase(unsigned index)
  58. {
  59. refVector_.Erase(index);
  60. }
  61. bool Remove(RefCounted* refCounted)
  62. {
  63. return refVector_.Remove(SharedPtr<RefCounted>(refCounted));
  64. }
  65. void Clear()
  66. {
  67. refVector_.Clear();
  68. }
  69. unsigned GetSize() const
  70. {
  71. return refVector_.Size();
  72. }
  73. void Resize(unsigned size)
  74. {
  75. return refVector_.Resize(size);
  76. }
  77. bool AdaptFromVector(const VariantVector& vectorIn)
  78. {
  79. // copy to temporary vector, so refs we hold aren't deleted, before they can be set
  80. // also allows us to save some instantiations
  81. Vector<SharedPtr<RefCounted>> keepAlive = refVector_;
  82. refVector_.Clear();
  83. for (unsigned i = 0; i < vectorIn.Size(); i++)
  84. {
  85. if (i < keepAlive.Size())
  86. {
  87. const SharedPtr<RefCounted>& item = keepAlive[i];
  88. if (item.Null() || item->GetClassID() != ScriptVariant::GetClassIDStatic())
  89. {
  90. ATOMIC_LOGERROR("ScriptVector::AdaptFromVector(VariantVector) - item == null or item class != ScriptVariant");
  91. return false;
  92. }
  93. ScriptVariant* scriptVariant = static_cast<ScriptVariant*>(item.Get());
  94. scriptVariant->SetVariant(vectorIn[i]);
  95. refVector_.Push(item);
  96. }
  97. else
  98. {
  99. refVector_.Push(SharedPtr<RefCounted>(new ScriptVariant(vectorIn[i])));
  100. }
  101. }
  102. return true;
  103. }
  104. template <class T>
  105. bool AdaptFromVector(PODVector<T> vectorIn)
  106. {
  107. Vector<SharedPtr<RefCounted>> keepAlive = refVector_;
  108. refVector_.Clear();
  109. for (unsigned i = 0; i < vectorIn.Size(); i++)
  110. {
  111. refVector_.Push(SharedPtr<RefCounted>(vectorIn[i]));
  112. }
  113. return true;
  114. }
  115. template <class T>
  116. bool AdaptFromVector(Vector<T> vectorIn)
  117. {
  118. Vector<SharedPtr<RefCounted>> keepAlive = refVector_;
  119. refVector_.Clear();
  120. for (unsigned i = 0; i < vectorIn.Size(); i++)
  121. {
  122. refVector_.Push((T) vectorIn[i]);
  123. }
  124. return true;
  125. }
  126. template <class T>
  127. bool AdaptFromVector(Vector<WeakPtr<T>> vectorIn)
  128. {
  129. Vector<SharedPtr<RefCounted>> keepAlive = refVector_;
  130. refVector_.Clear();
  131. for (unsigned i = 0; i < vectorIn.Size(); i++)
  132. {
  133. refVector_.Push(SharedPtr<RefCounted>((RefCounted*) vectorIn[i]));
  134. }
  135. return true;
  136. }
  137. template <class T>
  138. bool AdaptFromVector(Vector<SharedPtr<T>> vectorIn)
  139. {
  140. Vector<SharedPtr<RefCounted>> keepAlive = refVector_;
  141. refVector_.Clear();
  142. for (unsigned i = 0; i < vectorIn.Size(); i++)
  143. {
  144. refVector_.Push(SharedPtr<RefCounted>((RefCounted*) vectorIn[i]));
  145. }
  146. return true;
  147. }
  148. bool AdaptToVector(VariantVector& vectorOut, const Variant& defaultValue = Variant::EMPTY)
  149. {
  150. vectorOut.Clear();
  151. for (unsigned i = 0; i < refVector_.Size(); i++)
  152. {
  153. const SharedPtr<RefCounted>& item = refVector_[i];
  154. if (item.Null() || item->GetClassID() != ScriptVariant::GetClassIDStatic())
  155. {
  156. vectorOut.Push(defaultValue);
  157. continue;
  158. }
  159. ScriptVariant* scriptVariant = static_cast<ScriptVariant*>(item.Get());
  160. vectorOut.Push(scriptVariant->GetVariant());
  161. }
  162. return true;
  163. }
  164. template <class T>
  165. bool AdaptToVector(PODVector<T> vectorOut)
  166. {
  167. vectorOut.Clear();
  168. for (unsigned i = 0; i < refVector_.Size(); i++)
  169. {
  170. vectorOut.Push(static_cast<T>(refVector_[i].Get()));
  171. }
  172. return true;
  173. }
  174. template <class T>
  175. bool AdaptToVector(Vector<T> vectorOut)
  176. {
  177. vectorOut.Clear();
  178. for (unsigned i = 0; i < refVector_.Size(); i++)
  179. {
  180. vectorOut.Push((T) refVector_[i]);
  181. }
  182. return true;
  183. }
  184. template <class T>
  185. bool AdaptToVector(Vector<SharedPtr<T>> vectorOut)
  186. {
  187. vectorOut.Clear();
  188. for (unsigned i = 0; i < refVector_.Size(); i++)
  189. {
  190. vectorOut.Push(SharedPtr<T>((T) refVector_[i]));
  191. }
  192. return true;
  193. }
  194. template <class T>
  195. bool AdaptToVector(Vector<WeakPtr<T>> vectorOut)
  196. {
  197. vectorOut.Clear();
  198. for (unsigned i = 0; i < refVector_.Size(); i++)
  199. {
  200. vectorOut.Push(WeakPtr<T>((T) refVector_[i]));
  201. }
  202. return true;
  203. }
  204. private:
  205. Vector<SharedPtr<RefCounted>> refVector_;
  206. };
  207. }