Cutelee  6.1.0
cachingloaderdecorator.cpp
1 /*
2  This file is part of the Cutelee template system.
3 
4  Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either version
9  2.1 of the Licence, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #include "cachingloaderdecorator.h"
22 
23 namespace Cutelee
24 {
25 
27 {
28 public:
29  CachingLoaderDecoratorPrivate(std::shared_ptr<AbstractTemplateLoader> loader,
31  : q_ptr(qq), m_wrappedLoader(loader)
32  {
33  }
34 
35  Q_DECLARE_PUBLIC(CachingLoaderDecorator)
36  CachingLoaderDecorator *const q_ptr;
37 
38  const std::shared_ptr<AbstractTemplateLoader> m_wrappedLoader;
39 
40  mutable QHash<QString, Template> m_cache;
41 };
42 }
43 
44 using namespace Cutelee;
45 
46 CachingLoaderDecorator::CachingLoaderDecorator(std::shared_ptr<AbstractTemplateLoader> loader)
47  : d_ptr(new CachingLoaderDecoratorPrivate(loader, this))
48 {
49 }
50 
52 
54 {
55  Q_D(const CachingLoaderDecorator);
56  return d->m_wrappedLoader->canLoadTemplate(name);
57 }
58 
60 {
62  return d->m_cache.clear();
63 }
64 
66 {
67  Q_D(const CachingLoaderDecorator);
68  return d->m_cache.size();
69 }
70 
72 {
73  Q_D(const CachingLoaderDecorator);
74  return d->m_cache.isEmpty();
75 }
76 
77 std::pair<QString, QString>
79 {
80  Q_D(const CachingLoaderDecorator);
81  return d->m_wrappedLoader->getMediaUri(fileName);
82 }
83 
86  const Cutelee::Engine *engine) const
87 {
88  Q_D(const CachingLoaderDecorator);
89  const auto it = d->m_cache.constFind(name);
90  if (it != d->m_cache.constEnd()) {
91  return it.value();
92  }
93 
94  const auto t = d->m_wrappedLoader->loadByName(name, engine);
95 
96  d->m_cache.insert(name, t);
97 
98  return t;
99 }
Implements a loader decorator which caches compiled Template objects.
CachingLoaderDecorator(std::shared_ptr< AbstractTemplateLoader > loader)
std::pair< QString, QString > getMediaUri(const QString &fileName) const override
Template loadByName(const QString &name, const Cutelee::Engine *engine) const override
bool canLoadTemplate(const QString &name) const override
Cutelee::Engine is the main entry point for creating Cutelee Templates.
Definition: engine.h:121
The Template class is a tree of nodes which may be rendered.
Definition: template.h:95
The Cutelee namespace holds all public Cutelee API.
Definition: Mainpage.dox:8