Torque3D Documentation / _generateds / namedSingleton.h

namedSingleton.h

Engine/source/core/util/namedSingleton.h

More...

Classes:

class

Collection of statically registered named singletons.

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