Module.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Copyright (c) 2006-2015 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. // std
  24. #include <map>
  25. #include <utility>
  26. #include <string>
  27. namespace
  28. {
  29. typedef std::map<std::string, love::Module*> ModuleRegistry;
  30. // The registry must be dynamically managed, because some modules
  31. // (the math module) are static globals that are not guaranteed to
  32. // be destroyed before other static globals.
  33. ModuleRegistry *registry = nullptr;
  34. ModuleRegistry &registryInstance()
  35. {
  36. if (!registry)
  37. registry = new ModuleRegistry;
  38. return *registry;
  39. }
  40. void freeEmptyRegistry()
  41. {
  42. if (registry && registry->empty())
  43. {
  44. delete registry;
  45. registry = nullptr;
  46. }
  47. }
  48. } // anonymous namespace
  49. namespace love
  50. {
  51. Module *Module::instances[] = {};
  52. Module::~Module()
  53. {
  54. ModuleRegistry &registry = registryInstance();
  55. // We can't use the overridden Module::getName() in this destructor.
  56. for (auto it = registry.begin(); it != registry.end(); ++it)
  57. {
  58. if (it->second == this)
  59. {
  60. registry.erase(it);
  61. break;
  62. }
  63. }
  64. // Same deal with Module::getModuleType().
  65. for (int i = 0; i < (int) M_MAX_ENUM; i++)
  66. {
  67. if (instances[i] == this)
  68. instances[i] = nullptr;
  69. }
  70. freeEmptyRegistry();
  71. }
  72. void Module::registerInstance(Module *instance)
  73. {
  74. if (instance == nullptr)
  75. throw Exception("Module instance is null");
  76. std::string name(instance->getName());
  77. ModuleRegistry &registry = registryInstance();
  78. auto it = registry.find(name);
  79. if (it != registry.end())
  80. {
  81. if (it->second == instance)
  82. return;
  83. throw Exception("Module %s already registered!", instance->getName());
  84. }
  85. registry.insert(make_pair(name, instance));
  86. ModuleType moduletype = instance->getModuleType();
  87. if (instances[moduletype] != nullptr)
  88. {
  89. printf("Warning: overwriting module instance %s with new instance %s\n",
  90. instances[moduletype]->getName(), instance->getName());
  91. }
  92. instances[moduletype] = instance;
  93. }
  94. Module *Module::getInstance(const std::string &name)
  95. {
  96. ModuleRegistry &registry = registryInstance();
  97. auto it = registry.find(name);
  98. if (registry.end() == it)
  99. return nullptr;
  100. return it->second;
  101. }
  102. } // love