cleanup: make templateOptions and printOptions reference types

These two structs describe options used during printing.
They are passed through numerous classes as pointer. In this
case, reference semantics are preferred, as references:
 - can never be null
 - can not change during their lifetime
This not only helps the compiler, as it can optimize away null
checks, but also your fellow coder. Moreover, it prevents
unintentional creation of uninitialized references: one can't
create an instance of a class without initializing a reference
member. It does not prevent references from going dangling.
However, pointers have the same disadvantage.

Contains a few whitespace cleanups.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-12-12 13:28:36 +01:00 committed by Dirk Hohndel
parent 7bdd968e05
commit 0cbb448740
9 changed files with 97 additions and 111 deletions

View file

@ -9,9 +9,9 @@
QList<QString> grantlee_templates, grantlee_statistics_templates;
int getTotalWork(print_options *printOptions)
int getTotalWork(const print_options &printOptions)
{
if (printOptions->print_selected) {
if (printOptions.print_selected) {
// return the correct number depending on all/selected dives
// but don't return 0 as we might divide by this number
return amount_selected && !in_planner() ? amount_selected : 1;
@ -94,10 +94,9 @@ void copy_bundled_templates(QString src, QString dst, QStringList *templateBacku
}
}
TemplateLayout::TemplateLayout(print_options *printOptions, template_options *templateOptions)
TemplateLayout::TemplateLayout(const print_options &printOptions, const template_options &templateOptions) :
printOptions(printOptions), templateOptions(templateOptions)
{
this->printOptions = printOptions;
this->templateOptions = templateOptions;
}
QString TemplateLayout::generate()
@ -117,7 +116,7 @@ QString TemplateLayout::generate()
int i;
for_each_dive (i, dive) {
//TODO check for exporting selected dives only
if (!dive->selected && printOptions->print_selected)
if (!dive->selected && printOptions.print_selected)
continue;
diveList.append(QVariant::fromValue(DiveObjectHelperGrantlee(dive)));
progress++;
@ -125,11 +124,11 @@ QString TemplateLayout::generate()
}
}
QString templateContents = readTemplate(printOptions->p_template);
QString templateContents = readTemplate(printOptions.p_template);
QHash<QString, QVariant> options;
options["print_options"] = QVariant::fromValue(*printOptions);
options["template_options"] = QVariant::fromValue(*templateOptions);
options["print_options"] = QVariant::fromValue(printOptions);
options["template_options"] = QVariant::fromValue(templateOptions);
options["dives"] = QVariant::fromValue(diveList);
QList<token> tokens = lexer(templateContents);
QString buffer;
@ -154,12 +153,12 @@ QString TemplateLayout::generateStatistics()
i++;
}
QString templateFile = QString("statistics") + QDir::separator() + printOptions->p_template;
QString templateFile = QString("statistics") + QDir::separator() + printOptions.p_template;
QString templateContents = readTemplate(templateFile);
QHash<QString, QVariant> options;
options["print_options"] = QVariant::fromValue(*printOptions);
options["template_options"] = QVariant::fromValue(*templateOptions);
options["print_options"] = QVariant::fromValue(printOptions);
options["template_options"] = QVariant::fromValue(templateOptions);
options["years"] = QVariant::fromValue(years);
QList<token> tokens = lexer(templateContents);
QString buffer;