TemplateCache.cpp 3.4 KB

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