Cleanup: remove TemplateLayout::m_engine member variable

TemplateLayout::m_engine is a Grantlee::Engine that is reallocated
for every function call. Instead of the archaic memory-management,
remove the member variable and make it a local variable of the
two functions that need it. There seems to be no point in keeping
the object alive beyond the function's scope.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-04-26 17:46:02 +02:00 committed by Dirk Hohndel
parent b2b328fd7e
commit 449dec8269
2 changed files with 6 additions and 16 deletions

View file

@ -92,18 +92,12 @@ void copy_bundled_templates(QString src, QString dst, QStringList *templateBacku
} }
} }
TemplateLayout::TemplateLayout(print_options *PrintOptions, template_options *templateOptions) : TemplateLayout::TemplateLayout(print_options *PrintOptions, template_options *templateOptions)
m_engine(NULL)
{ {
this->PrintOptions = PrintOptions; this->PrintOptions = PrintOptions;
this->templateOptions = templateOptions; this->templateOptions = templateOptions;
} }
TemplateLayout::~TemplateLayout()
{
delete m_engine;
}
/* a HTML pre-processor stage. acts like a compatibility layer /* a HTML pre-processor stage. acts like a compatibility layer
* between some Grantlee variables and DiveObjectHelper Q_PROPERTIES; * between some Grantlee variables and DiveObjectHelper Q_PROPERTIES;
*/ */
@ -143,8 +137,7 @@ QString TemplateLayout::generate()
int totalWork = getTotalWork(PrintOptions); int totalWork = getTotalWork(PrintOptions);
QString htmlContent; QString htmlContent;
delete m_engine; Grantlee::Engine engine(this);
m_engine = new Grantlee::Engine(this);
Grantlee::registerMetaType<template_options>(); Grantlee::registerMetaType<template_options>();
Grantlee::registerMetaType<print_options>(); Grantlee::registerMetaType<print_options>();
@ -177,7 +170,7 @@ QString TemplateLayout::generate()
QString preprocessed = preprocessTemplate(templateContents); QString preprocessed = preprocessTemplate(templateContents);
/* create the template from QString; is this thing allocating memory? */ /* create the template from QString; is this thing allocating memory? */
Grantlee::Template t = m_engine->newTemplate(preprocessed, PrintOptions->p_template); Grantlee::Template t = engine.newTemplate(preprocessed, PrintOptions->p_template);
if (!t || t->error()) { if (!t || t->error()) {
qDebug() << "Can't load template"; qDebug() << "Can't load template";
return htmlContent; return htmlContent;
@ -194,13 +187,12 @@ QString TemplateLayout::generate()
QString TemplateLayout::generateStatistics() QString TemplateLayout::generateStatistics()
{ {
QString htmlContent; QString htmlContent;
delete m_engine; Grantlee::Engine engine(this);
m_engine = new Grantlee::Engine(this);
QSharedPointer<Grantlee::FileSystemTemplateLoader> m_templateLoader = QSharedPointer<Grantlee::FileSystemTemplateLoader> m_templateLoader =
QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader()); QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader());
m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser() + QDir::separator() + QString("statistics")); m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser() + QDir::separator() + QString("statistics"));
m_engine->addTemplateLoader(m_templateLoader); engine.addTemplateLoader(m_templateLoader);
Grantlee::registerMetaType<YearInfo>(); Grantlee::registerMetaType<YearInfo>();
Grantlee::registerMetaType<template_options>(); Grantlee::registerMetaType<template_options>();
@ -222,7 +214,7 @@ QString TemplateLayout::generateStatistics()
c.insert("template_options", QVariant::fromValue(*templateOptions)); c.insert("template_options", QVariant::fromValue(*templateOptions));
c.insert("print_options", QVariant::fromValue(*PrintOptions)); c.insert("print_options", QVariant::fromValue(*PrintOptions));
Grantlee::Template t = m_engine->loadByName(PrintOptions->p_template); Grantlee::Template t = engine.loadByName(PrintOptions->p_template);
if (!t || t->error()) { if (!t || t->error()) {
qDebug() << "Can't load template"; qDebug() << "Can't load template";
return htmlContent; return htmlContent;

View file

@ -21,14 +21,12 @@ class TemplateLayout : public QObject {
Q_OBJECT Q_OBJECT
public: public:
TemplateLayout(print_options *PrintOptions, template_options *templateOptions); TemplateLayout(print_options *PrintOptions, template_options *templateOptions);
~TemplateLayout();
QString generate(); QString generate();
QString generateStatistics(); QString generateStatistics();
static QString readTemplate(QString template_name); static QString readTemplate(QString template_name);
static void writeTemplate(QString template_name, QString grantlee_template); static void writeTemplate(QString template_name, QString grantlee_template);
private: private:
Grantlee::Engine *m_engine;
print_options *PrintOptions; print_options *PrintOptions;
template_options *templateOptions; template_options *templateOptions;