mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
undo: autogenerate string get() and data() functions using a template
Do this in analogy to other types. However, here we have to convert from / to QString. We could do this in an even more general way by using two templat parameters: one for the Qt type, one for the core type and define conversion functions. However, let's not do this for now. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
ea813938a8
commit
91f7689787
2 changed files with 27 additions and 30 deletions
|
@ -38,6 +38,19 @@ T EditDefaultSetter<T, ID, PTR>::data(struct dive *d) const
|
|||
return d->*PTR;
|
||||
}
|
||||
|
||||
template <DiveField::Flags ID, char *dive::*PTR>
|
||||
void EditStringSetter<ID, PTR>::set(struct dive *d, QString v) const
|
||||
{
|
||||
free(d->*PTR);
|
||||
d->*PTR = copy_qstring(v);
|
||||
}
|
||||
|
||||
template <DiveField::Flags ID, char *dive::*PTR>
|
||||
QString EditStringSetter<ID, PTR>::data(struct dive *d) const
|
||||
{
|
||||
return QString(d->*PTR);
|
||||
}
|
||||
|
||||
static std::vector<dive *> getDives(bool currentDiveOnly)
|
||||
{
|
||||
if (currentDiveOnly)
|
||||
|
@ -176,34 +189,12 @@ void EditBase<T>::redo()
|
|||
// Implementation of virtual functions
|
||||
|
||||
// ***** Notes *****
|
||||
void EditNotes::set(struct dive *d, QString s) const
|
||||
{
|
||||
free(d->notes);
|
||||
d->notes = strdup(qPrintable(s));
|
||||
}
|
||||
|
||||
QString EditNotes::data(struct dive *d) const
|
||||
{
|
||||
return QString(d->notes);
|
||||
}
|
||||
|
||||
QString EditNotes::fieldName() const
|
||||
{
|
||||
return Command::Base::tr("notes");
|
||||
}
|
||||
|
||||
// ***** Suit *****
|
||||
void EditSuit::set(struct dive *d, QString s) const
|
||||
{
|
||||
free(d->suit);
|
||||
d->suit = strdup(qPrintable(s));
|
||||
}
|
||||
|
||||
QString EditSuit::data(struct dive *d) const
|
||||
{
|
||||
return QString(d->suit);
|
||||
}
|
||||
|
||||
QString EditSuit::fieldName() const
|
||||
{
|
||||
return Command::Base::tr("suit");
|
||||
|
|
|
@ -84,19 +84,25 @@ private:
|
|||
T data(struct dive *d) const override final; // final prevents further overriding - then just don't use this template
|
||||
};
|
||||
|
||||
class EditNotes : public EditTemplate<QString, DiveField::NOTES> {
|
||||
// Automatically generate getter and setter in the case for string assignments.
|
||||
// The third parameter is a pointer to a C-style string in the dive structure.
|
||||
template <DiveField::Flags ID, char *dive::*PTR>
|
||||
class EditStringSetter : public EditTemplate<QString, ID> {
|
||||
private:
|
||||
using EditTemplate<QString, ID>::EditTemplate;
|
||||
void set(struct dive *d, QString) const override final; // final prevents further overriding - then just don't use this template
|
||||
QString data(struct dive *d) const override final; // final prevents further overriding - then just don't use this template
|
||||
};
|
||||
|
||||
class EditNotes : public EditStringSetter<DiveField::NOTES, &dive::notes> {
|
||||
public:
|
||||
using EditTemplate::EditTemplate; // Use constructor of base class.
|
||||
void set(struct dive *d, QString s) const override;
|
||||
QString data(struct dive *d) const override;
|
||||
using EditStringSetter::EditStringSetter; // Use constructor of base class.
|
||||
QString fieldName() const override;
|
||||
};
|
||||
|
||||
class EditSuit : public EditTemplate<QString, DiveField::SUIT> {
|
||||
class EditSuit : public EditStringSetter<DiveField::SUIT, &dive::suit> {
|
||||
public:
|
||||
using EditTemplate::EditTemplate; // Use constructor of base class.
|
||||
void set(struct dive *d, QString s) const override;
|
||||
QString data(struct dive *d) const override;
|
||||
using EditStringSetter::EditStringSetter; // Use constructor of base class.
|
||||
QString fieldName() const override;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue