Skip to content

Polymorphic ruler version 3 - #3257

Closed
Paul-Licameli wants to merge 17 commits into
audacity:masterfrom
micpap25:polymorphic-ruler-version-3
Closed

Paul-Licameli wants to merge 17 commits into
audacity:masterfrom
micpap25:polymorphic-ruler-version-3

Conversation

@Paul-Licameli

Copy link
Copy Markdown
Collaborator

Resolves: (direct link to the issue)

(short description of the changes and the motivation to make the changes)

  • I signed CLA
  • The title of the pull request describes an issue it addresses
  • If changes are extensive, then there is a sequence of easily reviewable commits
  • Each commit's message describes its purpose and effects
  • There are no behavior changes unnecessary for the stated purpose of the PR

Recommended:

  • Each commit compiles and runs on my machine without known undesirable changes of behavior

Comment thread src/widgets/Ruler.h Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All uses of SetUseZoomInfo are gone in this commit, replaced with this better, more general thing. Good! But then, don't leave behind a code comment referring to a nonexistent function. Say it in the commit comment.

Comment thread src/AudacityHeaders.h Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make a separate commit just for this one line.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Comment thread src/TimeTrack.cpp Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below, *mRuler is the simpler equivalent expression. I'm reviewing the first commit, but I think you fixed this in a later one. Good, but then do it in the first commit.

Comment thread src/widgets/Updater.h Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your own name!

Or as I often leave it, when splitting up legacy code into smaller files:

Dominic Mazzoni
Michael Papadopoulos split from Updater.h

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to rename the class (and file) as RulerUpdater?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change it to RulerUpdater and I'll update the credits when I do. No rush to change it.

Comment thread src/widgets/Updater.h Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you added const wherever possible for member functions of Updater and its nested types.

Can you use even more const ? ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will look through it

Comment thread src/widgets/CustomUpdater.h Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You ignore the argument z and always pass NULL (which should be nullptr to observe more modern C++ style).

Is that a mistake, or is z really not needed? If the latter, then omit it from the argument list.

Comment thread src/widgets/Ruler.h Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we avoid the friend declarations, and mention of higher level things?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had a long talk about just that, didn't we... I need to see the later commits

Comment thread src/widgets/Ruler.h Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see two overloads of SetUpdater. Can they be collapsed into one, by use of a default argument for leftOffset?

Yes: you might pass int* not int and default the argument to nullptr

But better than that -- avoiding funny looking function declarations that might leave the reviewer with doubts about lifetime questions -- you can make the intent even clearer by passing std::optional<int> around, by-value.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have never heard of std::optional<>. I will look into it.

Comment thread src/AdornedRulerPanel.cpp Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the second commit corrects several things I found doubtful in the first commit.

Good to correct them: but better not to introduce them though at any point in the history.

Maybe the first two commits should be squashed (after rebbasing on a separate small commit for AudacityHeaders.h)

But then, make the separate, and prior, commit to define RulerStruct with all necessary changes in Ruler.cpp. And then, rebase all onto that.

This will present a more reviewable sequence of changes. Ruler.cpp will make a lot of necessary, "noisy" changes in a prior commit that is mostly just the noise.

Then the later commit separates the "noise" from the more interesting structural change.

Comment thread src/widgets/Updater.cpp Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second commit introduces RulerStruct and this constructor.

This constructor has many assignments in it.

It's better C++ style (since 1998) to prefer member initialization where possible over assignments in the constructor body, for instance:

RulerStruct::RulerStruct()
   : mMin(0.0)
{

But since C++11, it's even better to prefer the "uniform initialization" syntax (curly braces) where possible:

RulerStruct::RulerStruct()
   : mMin{ 0.0 }
{

-- among other things, this disallows implicit narrowing convertions (as from float to integer).

But, also in C++11, and better still where it applies: use in-class initialization. That is, in Updater.h, declare the member as:

double mMin = 0.0;

or better

double mMin{ 0.0 };

or, even:

double mMin{};

Because 0 is the default value for all kinds of number types.

Any of those is better than

double mMin;

which implies no initialization.

When I review a class to understand it better, I want to start with the header file. If I see in-class initializers, I don't have to read the constructor definitions to have confidence that members are never left uninitialized by mistake. Rather I know that they default reasonably. And then when I do read the constructor, it's a shorter constructor.

Only when there is no reasonable, argument-independent default for a member -- then, leave it without an initializer. But then, I know that I must read the constructor body to understand fully what the member is for.

Comment thread src/AdornedRulerPanel.cpp Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirm that at the second commit, you do not introduce dependency cycles among sourc code files. 👍

Comment thread src/widgets/Updater.cpp Outdated

@Paul-Licameli Paul-Licameli Jul 19, 2022 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The third commit corrects the omission of the virtual destructor. Good: but, logically this should be squashed with the same commit that introduces

std::unique_ptr<Updater> mpUpdater;

in Ruler.h, so the known mistake of missing default constructors is never even introduced into the commit history.

Besides that, I recommend:

  • Define all of the destructors in this commit as non-inline, not only the one for Updater
  • You can use the C++11 alternative syntax: Updater::~Updater() = default
  • Also, now that I think of this too: when you introduce the subclasses of Updater, make them final if possible, e.g. struct LinearUpdater final : public Updater { ... this means: you do not intend further subclassing of those classes.

What are my reasons for these practices?

  • A linker has to put the virtual function table for each class in some unique place, and often implemented to follow this rule: the .cpp file definining the first non-inline virtual function generates the object file in which the table will live. But, if there is no non-inline virtual function, then the compiler has to fall back on generating multiple copies of the virtual function table that the linker just eliminates later. Moral: be nice to your linker, and avoid in-line virtuals.
  • in-line optimization is inherently incompatible with virtual function dispatch anyway, so why declare the functions inline anyway ? ...
  • ... except: when a class is final, and the declared object is of the leaf-class type, then the compiler can know that it really can to do the inline optimization, avoiding virtual function dispatch. Better to use final and let a smart compiler find the inlining opportunities.

Comment thread src/widgets/RulerPanel.h Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fourth commit splits RulerPanel from Ruler.

Observe that including Ruler.h in RulerPanel.h is unavoidable, because there is a nested Ruler object.

Ok then -- but that means the transitive inclusion of Ruler.h happens whever RulerPanel.h is included.

There are four such inclusions, including that in RulerPanel.cpp. I suggest then you simplify the lists of #inlude directives in each place.

Comment thread src/widgets/Ruler.cpp Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fifth commit moves this function.

I'd rather just squash that into the commit that introduces class Updater. It's not recommended to have the non-inline definitions of a class's member functions in more than one file: so, avoid putting things into this state to begin with.

Comment thread src/widgets/Updater.cpp Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last commit, too, might as well be squashed onto the commit that introduces Updater.

Rework update so that it is polymorphic. Break it into multiple files and allow the ruler's creator to set the updater with a unique pointer creator.
@micpap25
micpap25 force-pushed the polymorphic-ruler-version-3 branch 5 times, most recently from be122e6 to d5d0b36 Compare July 22, 2022 15:55
Comment thread src/widgets/Ruler.cpp Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "correct"

Comment thread src/widgets/Ruler.h Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewing the first again, I see the Ruler::mUseZoomInfo becomes unused at the first commit. Reassigned in Ruler::SetUpdater but not used elsewhere for anything. So eliminate it.

Comment thread src/widgets/Updater.h Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewing the commit "Remove circular dependencies..." which moves fields into the new type RulerStruct -- I see that the member mLeftOffset is never initialized.

A later commit removes the constructor in favor of in-class initializers, which is all right, but still does not correct this.

The remaining fields without in-class initializers are all right to remain so, because they are not primitive types. So their default constructors are implicitly invoked. You could write {} to initialize each but it isn't needed and makes no difference in compiled code. I omit such initializers myself in such cases.

Comment thread src/widgets/Ruler.h Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is incorrect about the purpose of the inclusion

Comment thread src/widgets/Ruler.cpp Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit that separates RulerPanel.cpp can also remove this #include which becomes unnecessary.

Comment thread src/commands/SetTrackInfoCommand.cpp Outdated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changed line should be preceded by an i18n-hint comment explaining that "amp" abbreviates "amplitude".

Those comments matter because xgettext extracts them to locale/audacity.pot when we update that file, at least once in each development cycle. Then the comments are copied to the several locale/*.po files, and serve as clues for the volunteer translators who update the string catalogs for other languages.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See scripts/update_po_files.sh if you are curious about that process.

micpap25 added 11 commits July 24, 2022 19:03
Create the virtual destructor for updater

Remove circular dependencies of Updater and Ruler

Streamline the dependencies of Updater and Ruler. Extract all necessary data for Updater functions down to Updater, and rework ruler calls to use this format. This allows Updater to be independent of Ruler.
Separates Ruler from RulerPanel to reduce unnecessary importing.
To be squashed into a past commit
Properly implements Custom functions and uses the LinearDb ruler as a space to test it.
@micpap25
micpap25 force-pushed the polymorphic-ruler-version-3 branch from 98ce83b to e9fc442 Compare July 24, 2022 23:08
micpap25 added 5 commits July 26, 2022 09:50
Imports an old change for GSoC to separate the units of a label from its text so different fonts can be used for each.
A basic start on making dB ruler functional for all settings. Will proceed more when design is discussed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants