PluginRegistry.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "PluginRegistry.h"
  29. #include "../../Include/RmlUi/Core/Plugin.h"
  30. #include "ControlledLifetimeResource.h"
  31. #include <algorithm>
  32. namespace Rml {
  33. struct PluginVectors {
  34. Vector<Plugin*> basic;
  35. Vector<Plugin*> document;
  36. Vector<Plugin*> element;
  37. };
  38. static ControlledLifetimeResource<PluginVectors> plugin_vectors;
  39. static void EnsurePluginVectorsInitialized()
  40. {
  41. if (!plugin_vectors)
  42. {
  43. plugin_vectors.Initialize();
  44. }
  45. }
  46. void PluginRegistry::RegisterPlugin(Plugin* plugin)
  47. {
  48. EnsurePluginVectorsInitialized();
  49. int event_classes = plugin->GetEventClasses();
  50. if (event_classes & Plugin::EVT_BASIC)
  51. plugin_vectors->basic.push_back(plugin);
  52. if (event_classes & Plugin::EVT_DOCUMENT)
  53. plugin_vectors->document.push_back(plugin);
  54. if (event_classes & Plugin::EVT_ELEMENT)
  55. plugin_vectors->element.push_back(plugin);
  56. }
  57. void PluginRegistry::UnregisterPlugin(Plugin* plugin)
  58. {
  59. auto erase_value = [](Vector<Plugin*>& container, Plugin* value) {
  60. container.erase(std::remove(container.begin(), container.end(), value), container.end());
  61. };
  62. int event_classes = plugin->GetEventClasses();
  63. if (event_classes & Plugin::EVT_BASIC)
  64. erase_value(plugin_vectors->basic, plugin);
  65. if (event_classes & Plugin::EVT_DOCUMENT)
  66. erase_value(plugin_vectors->document, plugin);
  67. if (event_classes & Plugin::EVT_ELEMENT)
  68. erase_value(plugin_vectors->element, plugin);
  69. }
  70. void PluginRegistry::NotifyInitialise()
  71. {
  72. EnsurePluginVectorsInitialized();
  73. for (Plugin* plugin : plugin_vectors->basic)
  74. plugin->OnInitialise();
  75. }
  76. void PluginRegistry::NotifyShutdown()
  77. {
  78. while (!plugin_vectors->basic.empty())
  79. {
  80. Plugin* plugin = plugin_vectors->basic.back();
  81. PluginRegistry::UnregisterPlugin(plugin);
  82. plugin->OnShutdown();
  83. }
  84. plugin_vectors.Shutdown();
  85. }
  86. void PluginRegistry::NotifyContextCreate(Context* context)
  87. {
  88. for (Plugin* plugin : plugin_vectors->basic)
  89. plugin->OnContextCreate(context);
  90. }
  91. void PluginRegistry::NotifyContextDestroy(Context* context)
  92. {
  93. for (Plugin* plugin : plugin_vectors->basic)
  94. plugin->OnContextDestroy(context);
  95. }
  96. void PluginRegistry::NotifyDocumentOpen(Context* context, const String& document_path)
  97. {
  98. for (Plugin* plugin : plugin_vectors->document)
  99. plugin->OnDocumentOpen(context, document_path);
  100. }
  101. void PluginRegistry::NotifyDocumentLoad(ElementDocument* document)
  102. {
  103. for (Plugin* plugin : plugin_vectors->document)
  104. plugin->OnDocumentLoad(document);
  105. }
  106. void PluginRegistry::NotifyDocumentUnload(ElementDocument* document)
  107. {
  108. for (Plugin* plugin : plugin_vectors->document)
  109. plugin->OnDocumentUnload(document);
  110. }
  111. void PluginRegistry::NotifyElementCreate(Element* element)
  112. {
  113. for (Plugin* plugin : plugin_vectors->element)
  114. plugin->OnElementCreate(element);
  115. }
  116. void PluginRegistry::NotifyElementDestroy(Element* element)
  117. {
  118. for (Plugin* plugin : plugin_vectors->element)
  119. plugin->OnElementDestroy(element);
  120. }
  121. } // namespace Rml