KTextTemplate

cachingloaderdecorator.h
1/*
2 This file is part of the KTextTemplate library
3
4 SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7
8*/
9
10#ifndef KTEXTTEMPLATE_CACHINGLOADERDECORATOR_H
11#define KTEXTTEMPLATE_CACHINGLOADERDECORATOR_H
12
13#include "templateloader.h"
14
15#include "ktexttemplate_export.h"
16
17namespace KTextTemplate
18{
19
20class CachingLoaderDecoratorPrivate;
21
22/// @headerfile cachingloaderdecorator.h <KTextTemplate/CachingLoaderDecorator>
23
24/**
25 @brief Implements a loader decorator which caches compiled Template objects.
26
27 The **%CachingLoaderDecorator** can be used with any implementation of
28 KTextTemplate::AbstractTemplateLoader.
29
30 @code
31 auto loader = QSharedPointer<KTextTemplate::FileSystemTemplateLoader>::create();
32 loader->setTemplateDirs({"/path/one", "/path/two"});
33
34 auto cache =
35 QSharedPointer<KTextTemplate::CachingLoaderDecorator>::create( loader );
36 m_engine->addTemplateLoader( cache );
37 @endcode
38
39 The effect is that templates do not need to be loaded from the filesystem and
40 compiled into Template objects on each access, but may be returned from the
41 cache instead.
42
43 This can be significant if loading Templates in a loop, or loading the same
44 Template very often in an application.
45
46 @code
47 <ul>
48 {% for item in list %}
49 <li>{% include "itemtemplate.html" %}</li>
50 {% endfor %}
51 </ul>
52 @endcode
53
54 If the loading of Templates is a bottleneck in an application, it may make
55 sense to use the caching decorator.
56
57 @author Stephen Kelly <steveire@gmail.com>
58 */
59class KTEXTTEMPLATE_EXPORT CachingLoaderDecorator : public AbstractTemplateLoader
60{
61public:
62 /**
63 Constructor
64 */
66
67 /**
68 Destructor
69 */
70 ~CachingLoaderDecorator() override;
71
72 bool canLoadTemplate(const QString &name) const override;
73
74 std::pair<QString, QString> getMediaUri(const QString &fileName) const override;
75
76 Template loadByName(const QString &name, const KTextTemplate::Engine *engine) const override;
77
78 /**
79 Clears the Templates objects cached in the decorator.
80 */
81 void clear();
82
83 /**
84 Returns the number of Template objects cached in the decorator.
85 */
86 int size() const;
87
88 /**
89 Returns whether the cache is empty.
90 */
91 bool isEmpty() const;
92
93private:
94 Q_DECLARE_PRIVATE(CachingLoaderDecorator)
95 CachingLoaderDecoratorPrivate *const d_ptr;
96};
97}
98
99#endif
An retrieval interface to a storage location for Template objects.
Implements a loader decorator which caches compiled Template objects.
KTextTemplate::Engine is the main entry point for creating KTextTemplate Templates.
Definition engine.h:110
The Template class is a tree of nodes which may be rendered.
Definition template.h:85
The KTextTemplate namespace holds all public KTextTemplate API.
Definition Mainpage.dox:8
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:17 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.