componentInterface.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "component/simComponent.h"
  23. #include "component/componentInterface.h"
  24. #include "core/strings/findMatch.h"
  25. #include "core/stringTable.h"
  26. bool ComponentInterfaceCache::add( const char *type, const char *name, const SimComponent *owner, ComponentInterface *cinterface )
  27. {
  28. if( ( mInterfaceList.size() == 0 ) || ( enumerate( NULL, type, name, owner ) == 0 ) )
  29. {
  30. mInterfaceList.increment();
  31. // CodeReview [tom, 3/9/2007] Seems silly to keep calling last(), why not cache the var? Yes, I know I am pedantic.
  32. mInterfaceList.last().type = ( type == NULL ? NULL : StringTable->insert( type ) );
  33. mInterfaceList.last().name = ( name == NULL ? NULL : StringTable->insert( name ) );
  34. mInterfaceList.last().owner = owner;
  35. mInterfaceList.last().iface = cinterface;
  36. return true;
  37. }
  38. return false;
  39. }
  40. //------------------------------------------------------------------------------
  41. void ComponentInterfaceCache::clear()
  42. {
  43. mInterfaceList.clear();
  44. }
  45. //------------------------------------------------------------------------------
  46. U32 ComponentInterfaceCache::enumerate( ComponentInterfaceList *list, const char *type /* = NULL */,
  47. const char *name /* = NULL */, const SimComponent *owner /* = NULL */, bool notOwner /* = false */ ) const
  48. {
  49. U32 numMatches = 0;
  50. for( _InterfaceEntryItr i = mInterfaceList.begin(); i != mInterfaceList.end(); i++ )
  51. {
  52. // Early out if limiting results by component owner
  53. if( owner != NULL && (
  54. ( (*i).owner == owner && notOwner ) ||
  55. ( (*i).owner != owner && !notOwner ) ) )
  56. continue;
  57. // Match the type, short circuit if type == NULL
  58. if( type == NULL || FindMatch::isMatch( type, (*i).type ) )
  59. {
  60. // Match the name
  61. if( name == NULL || FindMatch::isMatch( name, (*i).name ) )
  62. {
  63. numMatches++;
  64. if( list != NULL )
  65. list->push_back( (*i).iface );
  66. }
  67. }
  68. }
  69. return numMatches;
  70. }