StaticRegistry.h 1.4 KB

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