File indexing completed on 2025-02-09 04:28:36
0001 /* 0002 This file is part of the KTextTemplate library 0003 0004 SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com> 0005 0006 SPDX-License-Identifier: LGPL-2.1-or-later 0007 0008 */ 0009 0010 #ifndef KTEXTTEMPLATE_CACHINGLOADERDECORATOR_H 0011 #define KTEXTTEMPLATE_CACHINGLOADERDECORATOR_H 0012 0013 #include "templateloader.h" 0014 0015 #include "ktexttemplate_export.h" 0016 0017 namespace KTextTemplate 0018 { 0019 0020 class CachingLoaderDecoratorPrivate; 0021 0022 /// @headerfile cachingloaderdecorator.h <KTextTemplate/CachingLoaderDecorator> 0023 0024 /** 0025 @brief Implements a loader decorator which caches compiled Template objects. 0026 0027 The **%CachingLoaderDecorator** can be used with any implementation of 0028 KTextTemplate::AbstractTemplateLoader. 0029 0030 @code 0031 auto loader = QSharedPointer<KTextTemplate::FileSystemTemplateLoader>::create(); 0032 loader->setTemplateDirs({"/path/one", "/path/two"}); 0033 0034 auto cache = 0035 QSharedPointer<KTextTemplate::CachingLoaderDecorator>::create( loader ); 0036 m_engine->addTemplateLoader( cache ); 0037 @endcode 0038 0039 The effect is that templates do not need to be loaded from the filesystem and 0040 compiled into Template objects on each access, but may be returned from the 0041 cache instead. 0042 0043 This can be significant if loading Templates in a loop, or loading the same 0044 Template very often in an application. 0045 0046 @code 0047 <ul> 0048 {% for item in list %} 0049 <li>{% include "itemtemplate.html" %}</li> 0050 {% endfor %} 0051 </ul> 0052 @endcode 0053 0054 If the loading of Templates is a bottleneck in an application, it may make 0055 sense to use the caching decorator. 0056 0057 @author Stephen Kelly <steveire@gmail.com> 0058 */ 0059 class KTEXTTEMPLATE_EXPORT CachingLoaderDecorator : public AbstractTemplateLoader 0060 { 0061 public: 0062 /** 0063 Constructor 0064 */ 0065 CachingLoaderDecorator(QSharedPointer<AbstractTemplateLoader> loader); 0066 0067 /** 0068 Destructor 0069 */ 0070 ~CachingLoaderDecorator() override; 0071 0072 bool canLoadTemplate(const QString &name) const override; 0073 0074 std::pair<QString, QString> getMediaUri(const QString &fileName) const override; 0075 0076 Template loadByName(const QString &name, const KTextTemplate::Engine *engine) const override; 0077 0078 /** 0079 Clears the Templates objects cached in the decorator. 0080 */ 0081 void clear(); 0082 0083 /** 0084 Returns the number of Template objects cached in the decorator. 0085 */ 0086 int size() const; 0087 0088 /** 0089 Returns whether the cache is empty. 0090 */ 0091 bool isEmpty() const; 0092 0093 private: 0094 Q_DECLARE_PRIVATE(CachingLoaderDecorator) 0095 CachingLoaderDecoratorPrivate *const d_ptr; 0096 }; 0097 } 0098 0099 #endif