simObjectList.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  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
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell 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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/simObjectList.h"
  24. #include "console/console.h"
  25. #include "console/sim.h"
  26. #include "console/simObject.h"
  27. String SimObjectList::smSortScriptCallbackFn;
  28. bool SimObjectList::pushBack(SimObject* obj)
  29. {
  30. if (find(begin(),end(),obj) == end())
  31. {
  32. push_back(obj);
  33. return true;
  34. }
  35. return false;
  36. }
  37. bool SimObjectList::pushBackForce(SimObject* obj)
  38. {
  39. iterator itr = find(begin(),end(),obj);
  40. if (itr == end())
  41. {
  42. push_back(obj);
  43. return true;
  44. }
  45. else
  46. {
  47. // Move to the back...
  48. //
  49. SimObject* pBack = *itr;
  50. removeStable(pBack);
  51. push_back(pBack);
  52. }
  53. return false;
  54. }
  55. bool SimObjectList::pushFront(SimObject* obj)
  56. {
  57. if (find(begin(),end(),obj) == end())
  58. {
  59. push_front(obj);
  60. return true;
  61. }
  62. return false;
  63. }
  64. bool SimObjectList::remove(SimObject* obj)
  65. {
  66. iterator ptr = find(begin(),end(),obj);
  67. if (ptr != end())
  68. {
  69. erase(ptr);
  70. return true;
  71. }
  72. return false;
  73. }
  74. bool SimObjectList::removeStable(SimObject* obj)
  75. {
  76. iterator ptr = find(begin(),end(),obj);
  77. if (ptr != end())
  78. {
  79. erase(ptr);
  80. return true;
  81. }
  82. return false;
  83. }
  84. S32 QSORT_CALLBACK SimObjectList::_compareId(const void* a,const void* b)
  85. {
  86. return (*reinterpret_cast<const SimObject* const*>(a))->getId() -
  87. (*reinterpret_cast<const SimObject* const*>(b))->getId();
  88. }
  89. void SimObjectList::sortId()
  90. {
  91. dQsort(address(),size(),sizeof(value_type),_compareId);
  92. }
  93. S32 QSORT_CALLBACK SimObjectList::_callbackSort( const void *a, const void *b )
  94. {
  95. const SimObject *objA = *reinterpret_cast<const SimObject* const*>( a );
  96. const SimObject *objB = *reinterpret_cast<const SimObject* const*>( b );
  97. static char idA[64];
  98. dSprintf( idA, sizeof( idA ), "%d", objA->getId() );
  99. static char idB[64];
  100. dSprintf( idB, sizeof( idB ), "%d", objB->getId() );
  101. return dAtoi( Con::executef( (const char*)smSortScriptCallbackFn, idA, idB ) );
  102. }
  103. void SimObjectList::scriptSort( const String &scriptCallback )
  104. {
  105. AssertFatal( smSortScriptCallbackFn.isEmpty(), "SimObjectList::scriptSort() - The script sort is not reentrant!" );
  106. smSortScriptCallbackFn = scriptCallback;
  107. dQsort( address(), size(), sizeof(value_type), _callbackSort );
  108. smSortScriptCallbackFn = String::EmptyString;
  109. }