TemplateCache.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "TemplateCache.h"
  29. #include "../../Include/RmlUi/Core/Log.h"
  30. #include "StreamFile.h"
  31. #include "Template.h"
  32. namespace Rml {
  33. static TemplateCache* instance = nullptr;
  34. TemplateCache::TemplateCache()
  35. {
  36. RMLUI_ASSERT(instance == nullptr);
  37. instance = this;
  38. }
  39. TemplateCache::~TemplateCache()
  40. {
  41. for (Templates::iterator itr = instance->templates.begin(); itr != instance->templates.end(); ++itr)
  42. {
  43. delete (*itr).second;
  44. }
  45. instance = nullptr;
  46. }
  47. bool TemplateCache::Initialise()
  48. {
  49. new TemplateCache();
  50. return true;
  51. }
  52. void TemplateCache::Shutdown()
  53. {
  54. delete instance;
  55. }
  56. Template* TemplateCache::LoadTemplate(const String& name)
  57. {
  58. // Check if the template is already loaded
  59. Templates::iterator itr = instance->templates.find(name);
  60. if (itr != instance->templates.end())
  61. return (*itr).second;
  62. // Nope, we better load it
  63. Template* new_template = nullptr;
  64. auto stream = MakeUnique<StreamFile>();
  65. if (stream->Open(name))
  66. {
  67. new_template = new Template();
  68. if (!new_template->Load(stream.get()))
  69. {
  70. Log::Message(Log::LT_ERROR, "Failed to load template %s.", name.c_str());
  71. delete new_template;
  72. new_template = nullptr;
  73. }
  74. else if (new_template->GetName().empty())
  75. {
  76. Log::Message(Log::LT_ERROR, "Failed to load template %s, template is missing its name.", name.c_str());
  77. delete new_template;
  78. new_template = nullptr;
  79. }
  80. else
  81. {
  82. instance->templates[name] = new_template;
  83. instance->template_ids[new_template->GetName()] = new_template;
  84. }
  85. }
  86. else
  87. {
  88. Log::Message(Log::LT_ERROR, "Failed to open template file %s.", name.c_str());
  89. }
  90. return new_template;
  91. }
  92. Template* TemplateCache::GetTemplate(const String& name)
  93. {
  94. // Check if the template is already loaded
  95. Templates::iterator itr = instance->template_ids.find(name);
  96. if (itr != instance->template_ids.end())
  97. return (*itr).second;
  98. return nullptr;
  99. }
  100. void TemplateCache::Clear()
  101. {
  102. for (Templates::iterator i = instance->templates.begin(); i != instance->templates.end(); ++i)
  103. delete (*i).second;
  104. instance->templates.clear();
  105. instance->template_ids.clear();
  106. }
  107. } // namespace Rml