Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use single class for all allshader waveformwidget types with info class #13249

Closed
wants to merge 3 commits into from

Conversation

m0dB
Copy link
Contributor

@m0dB m0dB commented May 20, 2024

EDIT: I will redo this work on top of 13220 instead.

This is a code quality improvement PR.

There is a lot of code duplication in the waveform widget classes. This PR proposes a way to reduce this by introducing the WaveformWidgetInfo class which contains the information for each WaveformWidgetType, rather than having this information in concrete WaveformWidget classes. The WaveformWidgetInfo class also serves as a factory (creator) for the WaveformWidget class.

I have so far implemented this for the allshader widgets only, but this can easily be extended to the rest of the code.

Apart from the code duplication reduction, other advantages are that: it removes the need for huge switches in WaveformWidgetFactory, it replaces the need to use ifdefs with registered WaveformWidgetInfo's, and allows to obtain info directly based on the WaveformWidgetType instead of having to know the concrete WaveformWidget class.

@m0dB m0dB requested review from Swiftb0y and daschuer May 20, 2024 23:27
@m0dB m0dB requested a review from ywwg May 20, 2024 23:27
@m0dB m0dB marked this pull request as draft May 20, 2024 23:28
useOpenGLShaders = info->m_useGLSL;
category = info->m_category;
} else {
switch (type) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

once WaveformWidgetInfo's have been implemented for all WaveformWidgetTypes this switch can be removed.

// waveform widget types registered with WaveformWidgetInfo's
widget = info->create(viewer->getGroup(), viewer).release();
} else {
switch (type) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

likewise, once WaveformWidgetInfo's have been implemented for all WaveformWidgetTypes this switch can be removed, and the creation of the WaveformWidget's is done with

widget = info->create(viewer->getGroup(), viewer).release();

above.

Comment on lines 18 to 24
WaveformWidgetInfoBase(WaveformWidgetType::Type type,
QString name,
bool useGL,
bool useGLES,
bool useGLSL,
bool highDetail,
WaveformWidgetCategory category)
Copy link
Member

Choose a reason for hiding this comment

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

instead of an opaque constructor with a lot of bools, it would be clearer for this take a flag enum or bitset. or maybe this sweet new c++20 feature? https://www.cppstories.com/2017/03/on-toggle-parameters/#how-about-c20

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it most definitely would, but I prefer to do these refactorings one step at a time. The original code uses these booleans, so I will stay close to that approach in this PR. A follow up PR can be uses to change them for flags.

Copy link
Member

Choose a reason for hiding this comment

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

sgtm

Comment on lines +10 to +16
const WaveformWidgetType::Type m_type;
const QString m_name;
const bool m_useGL;
const bool m_useGLES;
const bool m_useGLSL;
const bool m_highDetail;
const WaveformWidgetCategory m_category;
Copy link
Member

Choose a reason for hiding this comment

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

CppCoreGuidelines C.12: Don't make data members const or references in a copyable or movable type.

Suggested change
const WaveformWidgetType::Type m_type;
const QString m_name;
const bool m_useGL;
const bool m_useGLES;
const bool m_useGLSL;
const bool m_highDetail;
const WaveformWidgetCategory m_category;
WaveformWidgetType::Type m_type;
QString m_name;
bool m_useGL;
bool m_useGLES;
bool m_useGLSL;
bool m_highDetail;
WaveformWidgetCategory m_category;

Comment on lines +69 to +71
category) {
infos().push_back(this);
}
Copy link
Member

Choose a reason for hiding this comment

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

this class looks like a value-type but in reality uses mutable static data internally thus calling this constructor is not thread-safe. This is a giant code-smell to me.

Copy link
Member

Choose a reason for hiding this comment

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

I see the value of the approach here, but I don't like the state in the value-type. Instead it would be nicer if the WaveformWidgetInfo remained a POD and all the static data handling was happening in a separate WaveformWidgetInfoRegistry class. That then contains the WaveformWidget::registerInfos and WaveformWidgetInfoBase::find implementations acting on its own private static data. Does that make sense?

@m0dB
Copy link
Contributor Author

m0dB commented May 22, 2024

This has been replaced with acolombier#2 , which is on top of @acolombier PR #13220

@m0dB m0dB closed this Jun 2, 2024
@Swiftb0y Swiftb0y deleted the waveformwidgetinfo branch June 2, 2024 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants