simObjectList.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/simBase.h"
  25. #include "console/engineAPI.h"
  26. #include "console/sim.h"
  27. #include "console/simObject.h"
  28. String SimObjectList::smSortScriptCallbackFn;
  29. bool SimObjectList::pushBack(SimObject* obj)
  30. {
  31. if (T3D::find(begin(),end(),obj) == end())
  32. {
  33. push_back(obj);
  34. return true;
  35. }
  36. return false;
  37. }
  38. bool SimObjectList::pushBackForce(SimObject* obj)
  39. {
  40. iterator itr = T3D::find(begin(),end(),obj);
  41. if (itr == end())
  42. {
  43. push_back(obj);
  44. return true;
  45. }
  46. else
  47. {
  48. // Move to the back...
  49. //
  50. SimObject* pBack = *itr;
  51. removeStable(pBack);
  52. push_back(pBack);
  53. }
  54. return false;
  55. }
  56. bool SimObjectList::pushFront(SimObject* obj)
  57. {
  58. if (T3D::find(begin(),end(),obj) == end())
  59. {
  60. push_front(obj);
  61. return true;
  62. }
  63. return false;
  64. }
  65. bool SimObjectList::remove(SimObject* obj)
  66. {
  67. iterator ptr = T3D::find(begin(),end(),obj);
  68. if (ptr != end())
  69. {
  70. erase(ptr);
  71. return true;
  72. }
  73. return false;
  74. }
  75. bool SimObjectList::removeStable(SimObject* obj)
  76. {
  77. iterator ptr = T3D::find(begin(),end(),obj);
  78. if (ptr != end())
  79. {
  80. erase(ptr);
  81. return true;
  82. }
  83. return false;
  84. }
  85. S32 QSORT_CALLBACK SimObjectList::_compareId(const void* a,const void* b)
  86. {
  87. return (*reinterpret_cast<const SimObject* const*>(a))->getId() -
  88. (*reinterpret_cast<const SimObject* const*>(b))->getId();
  89. }
  90. void SimObjectList::sortId()
  91. {
  92. dQsort(address(),size(),sizeof(value_type),_compareId);
  93. }
  94. S32 QSORT_CALLBACK SimObjectList::_callbackSort( const void *a, const void *b )
  95. {
  96. const SimObject *objA = *reinterpret_cast<const SimObject* const*>( a );
  97. const SimObject *objB = *reinterpret_cast<const SimObject* const*>( b );
  98. static char idA[64];
  99. dSprintf( idA, sizeof( idA ), "%d", objA->getId() );
  100. static char idB[64];
  101. dSprintf( idB, sizeof( idB ), "%d", objB->getId() );
  102. ConsoleValue cValue = Con::executef( (const char*)smSortScriptCallbackFn, idA, idB );
  103. return cValue.getInt();
  104. }
  105. void SimObjectList::scriptSort( const String &scriptCallback )
  106. {
  107. AssertFatal( smSortScriptCallbackFn.isEmpty(), "SimObjectList::scriptSort() - The script sort is not reentrant!" );
  108. smSortScriptCallbackFn = scriptCallback;
  109. dQsort( address(), size(), sizeof(value_type), _callbackSort );
  110. smSortScriptCallbackFn = String::EmptyString;
  111. }