ScriptVector.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "ScriptSystem.h"
  23. #pragma once
  24. namespace Atomic
  25. {
  26. class ScriptVector : public RefCounted
  27. {
  28. ATOMIC_REFCOUNTED(ScriptVector)
  29. public:
  30. ScriptVector() : RefCounted()
  31. {
  32. }
  33. virtual ~ScriptVector()
  34. {
  35. }
  36. RefCounted* At(unsigned index) const
  37. {
  38. if (index >= refVector_.Size())
  39. return 0;
  40. return refVector_[index];
  41. }
  42. void Push(RefCounted* refCounted)
  43. {
  44. // TODO: check null?
  45. refVector_.Push(SharedPtr<RefCounted>(refCounted));
  46. }
  47. void SetAt(unsigned index, RefCounted* refCounted)
  48. {
  49. refVector_[index] = refCounted;
  50. }
  51. void Insert(unsigned index, RefCounted* refCounted)
  52. {
  53. refVector_.Insert(index, SharedPtr<RefCounted>(refCounted));
  54. }
  55. void Erase(unsigned index)
  56. {
  57. refVector_.Erase(index);
  58. }
  59. bool Remove(RefCounted* refCounted)
  60. {
  61. return refVector_.Remove(SharedPtr<RefCounted>(refCounted));
  62. }
  63. void Clear()
  64. {
  65. refVector_.Clear();
  66. }
  67. unsigned GetSize() const
  68. {
  69. return refVector_.Size();
  70. }
  71. template <class T>
  72. bool AdaptFromVector(PODVector<T> vectorIn)
  73. {
  74. Vector<SharedPtr<RefCounted>> keepAlive = refVector_;
  75. refVector_.Clear();
  76. for (unsigned i = 0; i < vectorIn.Size(); i++)
  77. {
  78. refVector_.Push(SharedPtr<RefCounted>(vectorIn[i]));
  79. }
  80. return true;
  81. }
  82. template <class T>
  83. bool AdaptFromVector(Vector<T> vectorIn)
  84. {
  85. Vector<SharedPtr<RefCounted>> keepAlive = refVector_;
  86. refVector_.Clear();
  87. for (unsigned i = 0; i < vectorIn.Size(); i++)
  88. {
  89. refVector_.Push((T) vectorIn[i]);
  90. }
  91. return true;
  92. }
  93. template <class T>
  94. bool AdaptFromVector(Vector<WeakPtr<T>> vectorIn)
  95. {
  96. Vector<SharedPtr<RefCounted>> keepAlive = refVector_;
  97. refVector_.Clear();
  98. for (unsigned i = 0; i < vectorIn.Size(); i++)
  99. {
  100. refVector_.Push(SharedPtr<RefCounted>((RefCounted*) vectorIn[i]));
  101. }
  102. return true;
  103. }
  104. template <class T>
  105. bool AdaptFromVector(Vector<SharedPtr<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>((RefCounted*) vectorIn[i]));
  112. }
  113. return true;
  114. }
  115. template <class T>
  116. bool AdaptToVector(PODVector<T> vectorOut)
  117. {
  118. vectorOut.Clear();
  119. for (unsigned i = 0; i < refVector_.Size(); i++)
  120. {
  121. vectorOut.Push(static_cast<T>(refVector_[i].Get()));
  122. }
  123. return true;
  124. }
  125. template <class T>
  126. bool AdaptToVector(Vector<T> vectorOut)
  127. {
  128. vectorOut.Clear();
  129. for (unsigned i = 0; i < refVector_.Size(); i++)
  130. {
  131. vectorOut.Push((T) refVector_[i]);
  132. }
  133. return true;
  134. }
  135. template <class T>
  136. bool AdaptToVector(Vector<SharedPtr<T>> vectorOut)
  137. {
  138. vectorOut.Clear();
  139. for (unsigned i = 0; i < refVector_.Size(); i++)
  140. {
  141. vectorOut.Push(SharedPtr<T>((T) refVector_[i]));
  142. }
  143. return true;
  144. }
  145. template <class T>
  146. bool AdaptToVector(Vector<WeakPtr<T>> vectorOut)
  147. {
  148. vectorOut.Clear();
  149. for (unsigned i = 0; i < refVector_.Size(); i++)
  150. {
  151. vectorOut.Push(WeakPtr<T>((T) refVector_[i]));
  152. }
  153. return true;
  154. }
  155. private:
  156. Vector<SharedPtr<RefCounted>> refVector_;
  157. };
  158. }