StaticRegistry.h 1.5 KB

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