Module.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * Copyright (c) 2006-2024 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "Module.h"
  22. #include "Exception.h"
  23. #include "deprecation.h"
  24. // std
  25. #include <map>
  26. #include <utility>
  27. #include <string>
  28. namespace
  29. {
  30. typedef std::map<std::string, love::Module*> ModuleRegistry;
  31. // The registry must be dynamically managed, because some modules
  32. // (the math module) are static globals that are not guaranteed to
  33. // be destroyed before other static globals.
  34. ModuleRegistry *registry = nullptr;
  35. ModuleRegistry &registryInstance()
  36. {
  37. if (!registry)
  38. registry = new ModuleRegistry;
  39. return *registry;
  40. }
  41. void freeEmptyRegistry()
  42. {
  43. if (registry && registry->empty())
  44. {
  45. delete registry;
  46. registry = nullptr;
  47. }
  48. }
  49. } // anonymous namespace
  50. namespace love
  51. {
  52. love::Type Module::type("Module", &Object::type);
  53. Module *Module::instances[] = {};
  54. Module::Module(Module::ModuleType moduleType, const char *name)
  55. : moduleType(moduleType)
  56. , name(name)
  57. {
  58. initDeprecation();
  59. registerInstance(this);
  60. }
  61. Module::~Module()
  62. {
  63. ModuleRegistry &registry = registryInstance();
  64. // We can't use the overridden Module::getName() in this destructor.
  65. for (auto it = registry.begin(); it != registry.end(); ++it)
  66. {
  67. if (it->second == this)
  68. {
  69. registry.erase(it);
  70. break;
  71. }
  72. }
  73. // Same deal with Module::getModuleType().
  74. for (int i = 0; i < (int) M_MAX_ENUM; i++)
  75. {
  76. if (instances[i] == this)
  77. instances[i] = nullptr;
  78. }
  79. freeEmptyRegistry();
  80. deinitDeprecation();
  81. }
  82. void Module::registerInstance(Module *instance)
  83. {
  84. if (instance == nullptr)
  85. throw Exception("Module instance is null");
  86. std::string name(instance->getName());
  87. ModuleRegistry &registry = registryInstance();
  88. auto it = registry.find(name);
  89. if (it != registry.end())
  90. {
  91. if (it->second == instance)
  92. return;
  93. throw Exception("Module %s already registered!", instance->getName());
  94. }
  95. registry.insert(make_pair(name, instance));
  96. ModuleType mtype = instance->getModuleType();
  97. if (mtype != M_UNKNOWN)
  98. {
  99. if (instances[mtype] != nullptr)
  100. {
  101. printf("Warning: overwriting module instance %s with new instance %s\n",
  102. instances[mtype]->getName(), instance->getName());
  103. }
  104. instances[mtype] = instance;
  105. }
  106. }
  107. Module *Module::getInstance(const std::string &name)
  108. {
  109. ModuleRegistry &registry = registryInstance();
  110. auto it = registry.find(name);
  111. if (registry.end() == it)
  112. return nullptr;
  113. return it->second;
  114. }
  115. } // love