Module.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Copyright (c) 2006-2023 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()
  55. {
  56. initDeprecation();
  57. }
  58. Module::~Module()
  59. {
  60. ModuleRegistry &registry = registryInstance();
  61. // We can't use the overridden Module::getName() in this destructor.
  62. for (auto it = registry.begin(); it != registry.end(); ++it)
  63. {
  64. if (it->second == this)
  65. {
  66. registry.erase(it);
  67. break;
  68. }
  69. }
  70. // Same deal with Module::getModuleType().
  71. for (int i = 0; i < (int) M_MAX_ENUM; i++)
  72. {
  73. if (instances[i] == this)
  74. instances[i] = nullptr;
  75. }
  76. freeEmptyRegistry();
  77. deinitDeprecation();
  78. }
  79. void Module::registerInstance(Module *instance)
  80. {
  81. if (instance == nullptr)
  82. throw Exception("Module instance is null");
  83. std::string name(instance->getName());
  84. ModuleRegistry &registry = registryInstance();
  85. auto it = registry.find(name);
  86. if (it != registry.end())
  87. {
  88. if (it->second == instance)
  89. return;
  90. throw Exception("Module %s already registered!", instance->getName());
  91. }
  92. registry.insert(make_pair(name, instance));
  93. ModuleType mtype = instance->getModuleType();
  94. if (mtype != M_UNKNOWN)
  95. {
  96. if (instances[mtype] != nullptr)
  97. {
  98. printf("Warning: overwriting module instance %s with new instance %s\n",
  99. instances[mtype]->getName(), instance->getName());
  100. }
  101. instances[mtype] = instance;
  102. }
  103. }
  104. Module *Module::getInstance(const std::string &name)
  105. {
  106. ModuleRegistry &registry = registryInstance();
  107. auto it = registry.find(name);
  108. if (registry.end() == it)
  109. return nullptr;
  110. return it->second;
  111. }
  112. } // love