namedSingleton.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #ifndef _TORQUE_CORE_UTIL_NAMEDSINGLETON_H_
  23. #define _TORQUE_CORE_UTIL_NAMEDSINGLETON_H_
  24. #include "platform/platform.h"
  25. #include "core/util/safeCast.h"
  26. #include "console/console.h"
  27. #include "core/stringTable.h"
  28. //--------------------------------------------------------------------------
  29. // StaticNamedSingleton.
  30. //--------------------------------------------------------------------------
  31. /// Collection of statically registered named singletons.
  32. ///
  33. /// This class is useful as a mix-in for classes that are supposed to
  34. /// represent a range of named singleton instances from which a specific
  35. /// instance is then selected at run-time.
  36. ///
  37. /// @param T Arbitrary type parameter; identical types will share
  38. /// static data.
  39. template< class T >
  40. struct StaticNamedSingleton
  41. {
  42. typedef StaticNamedSingleton This;
  43. StaticNamedSingleton( const char* name );
  44. virtual ~StaticNamedSingleton() {}
  45. const char* getName();
  46. T* getNext();
  47. static T* staticGetFirst();
  48. static T* staticFindSingleton( const char* name );
  49. static EnumTable* staticCreateEnumTable();
  50. static U32 staticGetNumSingletons();
  51. private:
  52. const char* mName;
  53. This* mNext;
  54. static This* smSingletons;
  55. };
  56. template< class T >
  57. StaticNamedSingleton< T >* StaticNamedSingleton< T >::smSingletons;
  58. template< class T >
  59. StaticNamedSingleton< T >::StaticNamedSingleton( const char* name )
  60. : mName( name )
  61. {
  62. mNext = smSingletons;
  63. smSingletons = this;
  64. }
  65. template< class T >
  66. inline const char* StaticNamedSingleton< T >::getName()
  67. {
  68. return mName;
  69. }
  70. template< class T >
  71. inline T* StaticNamedSingleton< T >::getNext()
  72. {
  73. return static_cast< T* >( mNext );
  74. }
  75. template< class T >
  76. T* StaticNamedSingleton< T >::staticGetFirst()
  77. {
  78. return static_cast< T* >( smSingletons );
  79. }
  80. /// Find the instance with the given name. Returns NULL if no such
  81. /// instance exists.
  82. template< class T >
  83. T* StaticNamedSingleton< T >::staticFindSingleton( const char* name )
  84. {
  85. for( This* ptr = smSingletons; ptr != 0; ptr = ptr->mNext )
  86. if( dStricmp( name, ptr->mName ) == 0 )
  87. return static_cast< T* >( ptr );
  88. return 0;
  89. }
  90. /// Create a TorqueScript EnumTable that contains all registered
  91. /// instance names.
  92. template< class T >
  93. EnumTable* StaticNamedSingleton< T >::staticCreateEnumTable()
  94. {
  95. U32 numSingletons = staticGetNumSingletons();
  96. // Create the enums.
  97. EnumTable::Enums* enums = new EnumTable::Enums[ numSingletons ];
  98. This* ptr = smSingletons;
  99. for( U32 i = 0; i < numSingletons; ++ i )
  100. {
  101. enums[ i ].index = i;
  102. enums[ i ].label = StringTable->insert( ptr->getName() );
  103. ptr = ptr->mNext;
  104. }
  105. // Create the table.
  106. return new EnumTable( numSingletons, enums );
  107. }
  108. /// Return the number of registered named singletons.
  109. template< class T >
  110. U32 StaticNamedSingleton< T >::staticGetNumSingletons()
  111. {
  112. U32 numSingletons = 0;
  113. for( This* ptr = smSingletons; ptr != 0; ptr = ptr->mNext )
  114. numSingletons ++;
  115. return numSingletons;
  116. }
  117. #endif // _TORQUE_CORE_UTIL_NAMEDSINGLETON_H_