StaticRegistry.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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;
  63. enum class StreamTarget
  64. {
  65. FULL,
  66. CLIENT
  67. };