StaticRegistry.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. private:
  13. T** registry;
  14. int count;
  15. public:
  16. StaticRegistry()
  17. {
  18. count = 1;
  19. registry = new T * [count];
  20. memset(registry, 0, sizeof(T*) * count);
  21. }
  22. ~StaticRegistry()
  23. {
  24. for (int index = 0; index < count; index++)
  25. {
  26. if (registry[index])
  27. {
  28. registry[index]->release();
  29. registry[index] = 0;
  30. }
  31. }
  32. delete[]registry;
  33. }
  34. void registerT(T* type, int id)
  35. {
  36. if (id >= count)
  37. {
  38. T** temp = new T * [id + 1];
  39. memcpy(temp, registry, sizeof(T*) * count);
  40. memset(temp + count, 0, sizeof(T*) * (id + 1 - count));
  41. delete[]registry;
  42. registry = temp;
  43. count = id + 1;
  44. }
  45. registry[id] = type;
  46. }
  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. };