TemplateCache.cpp 3.4 KB

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