StaticRegistry.h 1.3 KB

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