StaticRegistry.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #pragma once
  2. #define REGISTRABLE(x) \
  3. public: \
  4. static const x *INSTANCE; \
  5. \
  6. private:
  7. template<typename T>
  8. class StaticRegistry
  9. {
  10. public:
  11. static StaticRegistry<T> INSTANCE;
  12. private:
  13. T **registry;
  14. int count;
  15. StaticRegistry()
  16. {
  17. count = 100;
  18. registry = new T * [ count ];
  19. memset( registry, 0, sizeof( T * ) * count );
  20. }
  21. ~StaticRegistry()
  22. {
  23. for( int index = 0; index < count; index++ )
  24. {
  25. if( registry[ index ] )
  26. {
  27. registry[ index ]->release();
  28. registry[ index ] = 0;
  29. }
  30. }
  31. delete[]registry;
  32. }
  33. void registerT( T *type, int id )
  34. {
  35. if( id >= count )
  36. {
  37. T **temp = new T * [ id + 1 ];
  38. memcpy( temp, registry, sizeof( T * ) * count );
  39. memset( temp + count, 0, sizeof( T * ) * ( id + 1 - count ) );
  40. delete[]registry;
  41. registry = temp;
  42. count = id + 1;
  43. }
  44. registry[ id ] = type;
  45. }
  46. public:
  47. T *zElement( int id )
  48. {
  49. if( id < 0 || id >= count )
  50. return 0;
  51. return registry[ id ];
  52. }
  53. int getCount() const
  54. {
  55. return count;
  56. }
  57. friend T;
  58. };
  59. template <typename T>
  60. StaticRegistry<T> StaticRegistry<T>::INSTANCE;