ContainerUtil.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifndef Spine_ContainerUtil_h
  30. #define Spine_ContainerUtil_h
  31. #include <spine/extension.h>
  32. #include <spine/Vector.h>
  33. #include <spine/HashMap.h>
  34. #include <spine/SpineObject.h>
  35. #include <spine/SpineString.h>
  36. #include <assert.h>
  37. namespace spine {
  38. class SP_API ContainerUtil : public SpineObject {
  39. public:
  40. /// Finds an item by comparing each item's name.
  41. /// It is more efficient to cache the results of this method than to call it multiple times.
  42. /// @return May be NULL.
  43. template<typename T>
  44. static T* findWithName(Vector<T*>& items, const String& name) {
  45. assert(name.length() > 0);
  46. for (size_t i = 0; i < items.size(); ++i) {
  47. T* item = items[i];
  48. if (item->getName() == name) {
  49. return item;
  50. }
  51. }
  52. return NULL;
  53. }
  54. /// @return -1 if the item was not found.
  55. template<typename T>
  56. static int findIndexWithName(Vector<T*>& items, const String& name) {
  57. assert(name.length() > 0);
  58. for (size_t i = 0, len = items.size(); i < len; ++i) {
  59. T* item = items[i];
  60. if (item->getName() == name) {
  61. return static_cast<int>(i);
  62. }
  63. }
  64. return -1;
  65. }
  66. /// Finds an item by comparing each item's name.
  67. /// It is more efficient to cache the results of this method than to call it multiple times.
  68. /// @return May be NULL.
  69. template<typename T>
  70. static T* findWithDataName(Vector<T*>& items, const String& name) {
  71. assert(name.length() > 0);
  72. for (size_t i = 0; i < items.size(); ++i) {
  73. T* item = items[i];
  74. if (item->getData().getName() == name) {
  75. return item;
  76. }
  77. }
  78. return NULL;
  79. }
  80. /// @return -1 if the item was not found.
  81. template<typename T>
  82. static int findIndexWithDataName(Vector<T*>& items, const String& name) {
  83. assert(name.length() > 0);
  84. for (size_t i = 0, len = items.size(); i < len; ++i) {
  85. T* item = items[i];
  86. if (item->getData().getName() == name) {
  87. return static_cast<int>(i);
  88. }
  89. }
  90. return -1;
  91. }
  92. template<typename T>
  93. static void cleanUpVectorOfPointers(Vector<T*>& items) {
  94. for (int i = (int)items.size() - 1; i >= 0; i--) {
  95. T* item = items[i];
  96. delete item;
  97. items.removeAt(i);
  98. }
  99. }
  100. private:
  101. // ctor, copy ctor, and assignment should be private in a Singleton
  102. ContainerUtil();
  103. ContainerUtil(const ContainerUtil&);
  104. ContainerUtil& operator=(const ContainerUtil&);
  105. };
  106. }
  107. #endif /* Spine_ContainerUtil_h */