StaticRegistry.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. #define REGISTRABLE(c) \
  3. \
  4. public: \
  5. static const c* INSTANCE; \
  6. static const int ID; \
  7. \
  8. private:
  9. #define REGISTER(c, typ)
  10. template<typename T> 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) return 0;
  50. return registry[id];
  51. }
  52. int getCount() const
  53. {
  54. return count;
  55. }
  56. friend T;
  57. };