From ea813938a838be57d6a616b0175d5d650fb8c2a2 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 21 Mar 2020 18:10:54 +0100 Subject: 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 --- commands/command_edit.cpp | 84 ++++++++--------------------------------------- commands/command_edit.h | 53 ++++++++++++++---------------- 2 files changed, 38 insertions(+), 99 deletions(-) (limited to 'commands') 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::fieldId() const return ID; } +template +void EditDefaultSetter::set(struct dive *d, T v) const +{ + d->*PTR = v; +} + +template +T EditDefaultSetter::data(struct dive *d) const +{ + return d->*PTR; +} + static std::vector getDives(bool currentDiveOnly) { if (currentDiveOnly) @@ -147,6 +159,8 @@ EditBase::EditBase(QString newValue, bool currentDiveOnly); template EditBase::EditBase(int newValue, bool currentDiveOnly); template +EditBase::EditBase(bool newValue, bool currentDiveOnly); +template EditBase::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 +class EditDefaultSetter : public EditTemplate { +private: + using EditTemplate::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 { public: using EditTemplate::EditTemplate; // Use constructor of base class. @@ -90,52 +100,39 @@ public: QString fieldName() const override; }; -class EditRating : public EditTemplate { +class EditRating : public EditDefaultSetter { 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 { +class EditVisibility : public EditDefaultSetter { 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 { +class EditWaveSize : public EditDefaultSetter { 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 { +class EditCurrent : public EditDefaultSetter { 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 { +class EditSurge : public EditDefaultSetter { 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 { +class EditChill : public EditDefaultSetter { 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 { +class EditInvalid : public EditDefaultSetter { 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; }; -- cgit v1.2.3-70-g09d2