1 /*
2 * This source file is part of libRocket, the HTML/CSS Interface Middleware
3 *
4 * For the latest information, see http://www.librocket.com
5 *
6 * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 */
27
28 #include "precompiled.h"
29 #include "TemplateCache.h"
30 #include "../../Include/Rocket/Core/StreamFile.h"
31 #include "Template.h"
32 #include "../../Include/Rocket/Core/Log.h"
33
34 namespace Rocket {
35 namespace Core {
36
37 static TemplateCache* instance = NULL;
38
TemplateCache()39 TemplateCache::TemplateCache()
40 {
41 ROCKET_ASSERT(instance == NULL);
42 instance = this;
43 }
44
~TemplateCache()45 TemplateCache::~TemplateCache()
46 {
47 for (Templates::iterator itr = instance->templates.begin(); itr != instance->templates.end(); ++itr)
48 {
49 delete (*itr).second;
50 }
51
52 instance = NULL;
53 }
54
Initialise()55 bool TemplateCache::Initialise()
56 {
57 new TemplateCache();
58
59 return true;
60 }
61
Shutdown()62 void TemplateCache::Shutdown()
63 {
64 delete instance;
65 }
66
LoadTemplate(const String & name)67 Template* TemplateCache::LoadTemplate(const String& name)
68 {
69 // Check if the template is already loaded
70 Templates::iterator itr = instance->templates.find(name);
71 if (itr != instance->templates.end())
72 return (*itr).second;
73
74 // Nope, we better load it
75 Template* new_template = NULL;
76 StreamFile* stream = new StreamFile();
77 if (stream->Open(name))
78 {
79 new_template = new Template();
80 if (!new_template->Load(stream))
81 {
82 Log::Message(Log::LT_ERROR, "Failed to load template %s.", name.CString());
83 delete new_template;
84 new_template = NULL;
85 }
86 else if (new_template->GetName().Empty())
87 {
88 Log::Message(Log::LT_ERROR, "Failed to load template %s, template is missing its name.", name.CString());
89 delete new_template;
90 new_template = NULL;
91 }
92 else
93 {
94 instance->templates[name] = new_template;
95 instance->template_ids[new_template->GetName()] = new_template;
96 }
97 }
98 else
99 {
100 Log::Message(Log::LT_ERROR, "Failed to open template file %s.", name.CString());
101 }
102 stream->RemoveReference();
103
104 return new_template;
105 }
106
GetTemplate(const String & name)107 Template* TemplateCache::GetTemplate(const String& name)
108 {
109 // Check if the template is already loaded
110 Templates::iterator itr = instance->template_ids.find(name);
111 if (itr != instance->template_ids.end())
112 return (*itr).second;
113
114 return NULL;
115 }
116
Clear()117 void TemplateCache::Clear()
118 {
119 for (Templates::iterator i = instance->templates.begin(); i != instance->templates.end(); ++i)
120 delete (*i).second;
121
122 instance->templates.clear();
123 instance->template_ids.clear();
124 }
125
126 }
127 }
128