StaticRegistry.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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;
  69. enum class StreamTarget
  70. {
  71. FULL,
  72. CLIENT
  73. };