StaticRegistry.h 1.3 KB

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