diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-03-21 18:10:54 +0100 |
---|---|---|
committer | bstoeger <32835590+bstoeger@users.noreply.github.com> | 2020-03-31 21:53:19 +0200 |
commit | ea813938a838be57d6a616b0175d5d650fb8c2a2 (patch) | |
tree | f49d0bb9c6108d39da9ff9e1969699dbcc9aa751 /commands | |
parent | ad540ce5e8f928c69ba8061dd9886bdb2936ed61 (diff) | |
download | subsurface-ea813938a838be57d6a616b0175d5d650fb8c2a2.tar.gz |
undo: autogenerate trivial set() and data() functions by a template
Some dive-editing undo commands have trivial setter and getter
functions: they simply read and write a struct dive member.
Autogenerate these in a template to which we pass a pointer
to member as template argument.
For the invalid-flag we have to turn the edit command from int
to bool, since that is how the flag is store in the dive struct.
Sadly, quite a number of the setters do funky things and we
cannot autogenerate them.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'commands')
-rw-r--r-- | commands/command_edit.cpp | 84 | ||||
-rw-r--r-- | commands/command_edit.h | 53 |
2 files changed, 38 insertions, 99 deletions
diff --git a/commands/command_edit.cpp b/commands/command_edit.cpp index 999396db2..d41f595dc 100644 --- a/commands/command_edit.cpp +++ b/commands/command_edit.cpp @@ -26,6 +26,18 @@ DiveField EditTagsTemplate<ID>::fieldId() const return ID; } +template <typename T, DiveField::Flags ID, T dive::*PTR> +void EditDefaultSetter<T, ID, PTR>::set(struct dive *d, T v) const +{ + d->*PTR = v; +} + +template <typename T, DiveField::Flags ID, T dive::*PTR> +T EditDefaultSetter<T, ID, PTR>::data(struct dive *d) const +{ + return d->*PTR; +} + static std::vector<dive *> getDives(bool currentDiveOnly) { if (currentDiveOnly) @@ -147,6 +159,8 @@ EditBase<QString>::EditBase(QString newValue, bool currentDiveOnly); template EditBase<int>::EditBase(int newValue, bool currentDiveOnly); template +EditBase<bool>::EditBase(bool newValue, bool currentDiveOnly); +template EditBase<struct dive_site *>::EditBase(struct dive_site *newValue, bool currentDiveOnly); // Undo and redo do the same as just the stored value is exchanged @@ -196,96 +210,36 @@ QString EditSuit::fieldName() const } // ***** Rating ***** -void EditRating::set(struct dive *d, int value) const -{ - d->rating = value; -} - -int EditRating::data(struct dive *d) const -{ - return d->rating; -} - QString EditRating::fieldName() const { return Command::Base::tr("rating"); } // ***** Visibility ***** -void EditVisibility::set(struct dive *d, int value) const -{ - d->visibility = value; -} - -int EditVisibility::data(struct dive *d) const -{ - return d->visibility; -} - QString EditVisibility::fieldName() const { return Command::Base::tr("visibility"); } // ***** WaveSize ***** -void EditWaveSize::set(struct dive *d, int value) const -{ - d->wavesize = value; -} - -int EditWaveSize::data(struct dive *d) const -{ - return d->wavesize; -} - QString EditWaveSize::fieldName() const { return Command::Base::tr("wavesize"); } // ***** Current ***** -void EditCurrent::set(struct dive *d, int value) const -{ - d->current = value; -} - -int EditCurrent::data(struct dive *d) const -{ - return d->current; -} - QString EditCurrent::fieldName() const { return Command::Base::tr("current"); } // ***** Surge ***** -void EditSurge::set(struct dive *d, int value) const -{ - d->surge = value; -} - -int EditSurge::data(struct dive *d) const -{ - return d->surge; -} - QString EditSurge::fieldName() const { return Command::Base::tr("surge"); } // ***** Chill ***** -void EditChill::set(struct dive *d, int value) const -{ - d->chill = value; -} - -int EditChill::data(struct dive *d) const -{ - return d->chill; -} - QString EditChill::fieldName() const { return Command::Base::tr("chill"); @@ -507,16 +461,6 @@ QString EditMode::fieldName() const } // ***** Invalid ***** -void EditInvalid::set(struct dive *d, int invalid) const -{ - d->invalid = invalid; -} - -int EditInvalid::data(struct dive *d) const -{ - return d->invalid; -} - QString EditInvalid::fieldName() const { return Command::Base::tr("invalid"); diff --git a/commands/command_edit.h b/commands/command_edit.h index 8e9ca90e7..073853254 100644 --- a/commands/command_edit.h +++ b/commands/command_edit.h @@ -74,6 +74,16 @@ private: DiveField fieldId() const override final; // final prevents further overriding - then just don't use this template }; +// Automatically generate getter and setter in the case of simple assignments. +// The third parameter is a pointer to a member of the dive structure. +template <typename T, DiveField::Flags ID, T dive::*PTR> +class EditDefaultSetter : public EditTemplate<T, ID> { +private: + using EditTemplate<T, ID>::EditTemplate; + void set(struct dive *d, T) const override final; // final prevents further overriding - then just don't use this template + 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> { public: using EditTemplate::EditTemplate; // Use constructor of base class. @@ -90,52 +100,39 @@ public: QString fieldName() const override; }; -class EditRating : public EditTemplate<int, DiveField::RATING> { +class EditRating : public EditDefaultSetter<int, DiveField::RATING, &dive::rating> { public: - using EditTemplate::EditTemplate; // Use constructor of base class. - void set(struct dive *d, int value) const override; - int data(struct dive *d) const override; + using EditDefaultSetter::EditDefaultSetter; // Use constructor of base class. QString fieldName() const override; }; -class EditVisibility : public EditTemplate<int, DiveField::VISIBILITY> { +class EditVisibility : public EditDefaultSetter<int, DiveField::VISIBILITY, &dive::visibility> { public: - using EditTemplate::EditTemplate; // Use constructor of base class. - void set(struct dive *d, int value) const override; - int data(struct dive *d) const override; + using EditDefaultSetter::EditDefaultSetter; // Use constructor of base class. QString fieldName() const override; }; - -class EditWaveSize : public EditTemplate<int, DiveField::WAVESIZE> { +class EditWaveSize : public EditDefaultSetter<int, DiveField::WAVESIZE, &dive::wavesize> { public: - using EditTemplate::EditTemplate; // Use constructor of base class. - void set(struct dive *d, int value) const override; - int data(struct dive *d) const override; + using EditDefaultSetter::EditDefaultSetter; // Use constructor of base class. QString fieldName() const override; }; -class EditCurrent : public EditTemplate<int, DiveField::CURRENT> { +class EditCurrent : public EditDefaultSetter<int, DiveField::CURRENT, &dive::current> { public: - using EditTemplate::EditTemplate; // Use constructor of base class. - void set(struct dive *d, int value) const override; - int data(struct dive *d) const override; + using EditDefaultSetter::EditDefaultSetter; // Use constructor of base class. QString fieldName() const override; }; -class EditSurge : public EditTemplate<int, DiveField::SURGE> { +class EditSurge : public EditDefaultSetter<int, DiveField::SURGE, &dive::surge> { public: - using EditTemplate::EditTemplate; // Use constructor of base class. - void set(struct dive *d, int value) const override; - int data(struct dive *d) const override; + using EditDefaultSetter::EditDefaultSetter; // Use constructor of base class. QString fieldName() const override; }; -class EditChill : public EditTemplate<int, DiveField::CHILL> { +class EditChill : public EditDefaultSetter<int, DiveField::CHILL, &dive::chill> { public: - using EditTemplate::EditTemplate; // Use constructor of base class. - void set(struct dive *d, int value) const override; - int data(struct dive *d) const override; + using EditDefaultSetter::EditDefaultSetter; // Use constructor of base class. QString fieldName() const override; }; @@ -219,11 +216,9 @@ public: QString fieldName() const override; }; -class EditInvalid : public EditTemplate<int, DiveField::INVALID> { +class EditInvalid : public EditDefaultSetter<bool, DiveField::INVALID, &dive::invalid> { public: - using EditTemplate::EditTemplate; // Use constructor of base class. - void set(struct dive *d, int number) const override; - int data(struct dive *d) const override; + using EditDefaultSetter::EditDefaultSetter; // Use constructor of base class. QString fieldName() const override; }; |