Torque3D Documentation / _generateds / simObjectList.cpp

simObjectList.cpp

Engine/source/console/simObjectList.cpp

More...

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platform/platform.h"
 25#include "console/simObjectList.h"
 26
 27#include "console/simBase.h"
 28#include "console/engineAPI.h"
 29#include "console/sim.h"
 30#include "console/simObject.h"
 31
 32
 33String SimObjectList::smSortScriptCallbackFn;
 34
 35bool SimObjectList::pushBack(SimObject* obj)
 36{
 37   if (T3D::find(begin(),end(),obj) == end())
 38   {
 39      push_back(obj);
 40      return true;
 41   }
 42   
 43   return false;
 44}
 45
 46bool SimObjectList::pushBackForce(SimObject* obj)
 47{
 48   iterator itr = T3D::find(begin(),end(),obj);
 49   if (itr == end())
 50   {
 51      push_back(obj);
 52      return true;
 53   }
 54   else
 55   {
 56      // Move to the back...
 57      //
 58      SimObject* pBack = *itr;
 59      removeStable(pBack);
 60      push_back(pBack);
 61   }
 62   
 63   return false;
 64}
 65
 66bool SimObjectList::pushFront(SimObject* obj)
 67{
 68   if (T3D::find(begin(),end(),obj) == end())
 69   {
 70      push_front(obj);
 71      return true;
 72   }
 73   
 74   return false;
 75}
 76
 77bool SimObjectList::remove(SimObject* obj)
 78{
 79   iterator ptr = T3D::find(begin(),end(),obj);
 80   if (ptr != end())
 81   {
 82      erase(ptr);
 83      return true;
 84   }
 85   
 86   return false;
 87}
 88
 89bool SimObjectList::removeStable(SimObject* obj)
 90{
 91   iterator ptr = T3D::find(begin(),end(),obj);
 92   if (ptr != end())
 93   {
 94      erase(ptr);
 95      return true;
 96   }
 97   
 98   return false;
 99}
100
101S32 QSORT_CALLBACK SimObjectList::_compareId(const void* a,const void* b)
102{
103   return (*reinterpret_cast<const SimObject* const*>(a))->getId() -
104      (*reinterpret_cast<const SimObject* const*>(b))->getId();
105}
106
107void SimObjectList::sortId()
108{
109   dQsort(address(),size(),sizeof(value_type),_compareId);
110}
111
112S32 QSORT_CALLBACK SimObjectList::_callbackSort( const void *a, const void *b )
113{
114   const SimObject *objA = *reinterpret_cast<const SimObject* const*>( a );
115   const SimObject *objB = *reinterpret_cast<const SimObject* const*>( b );   
116
117   static char idA[64];
118   dSprintf( idA, sizeof( idA ), "%d", objA->getId() );
119   static char idB[64];
120   dSprintf( idB, sizeof( idB ), "%d", objB->getId() );
121
122   return dAtoi( Con::executef( (const char*)smSortScriptCallbackFn, idA, idB ) );
123}
124
125void SimObjectList::scriptSort( const String &scriptCallback )
126{
127   AssertFatal( smSortScriptCallbackFn.isEmpty(), "SimObjectList::scriptSort() - The script sort is not reentrant!" );
128
129   smSortScriptCallbackFn = scriptCallback;
130   dQsort( address(), size(), sizeof(value_type), _callbackSort );
131   smSortScriptCallbackFn = String::EmptyString;
132}
133