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

Access ConfigObjects by predefined constants instead of plain strings #4223

Merged
merged 4 commits into from
Aug 21, 2021
Merged

Access ConfigObjects by predefined constants instead of plain strings #4223

merged 4 commits into from
Aug 21, 2021

Conversation

uklotzde
Copy link
Contributor

@uklotzde uklotzde commented Aug 18, 2021

...just migrated some config keys as a starting point and prerequisite for #4218.

An attempt to tame the chaotic string-based access to configuration settings. Please give feed back what you think about it. The _prefs.h file serves a similar purpose as the _decl.h file. Instead of static class members namespaces are used. You don't need the whole class definition for this purpose, only some shared constants.

@coveralls
Copy link

coveralls commented Aug 18, 2021

Pull Request Test Coverage Report for Build 1152289915

  • 7 of 22 (31.82%) changed or added relevant lines in 4 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.008%) to 25.969%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/preferences/upgrade.cpp 0 1 0.0%
src/library/trackcollectionmanager.cpp 2 4 50.0%
src/library/library.cpp 0 12 0.0%
Files with Coverage Reduction New Missed Lines %
src/library/library.cpp 1 0%
Totals Coverage Status
Change from base Build 1152021735: 0.008%
Covered Lines: 20010
Relevant Lines: 77053

💛 - Coveralls

@uklotzde uklotzde changed the title Detach library preference constants from class definition Access ConfigObjects by predefined constants instead of plain strings Aug 20, 2021
@daschuer
Copy link
Member

What is the advantage of using namespaces and the extern keyword from ANSI-C legacy. For my understanding it is a big benefit to use classes, that the the compiler is able to do some sanity checks.

@uklotzde
Copy link
Contributor Author

uklotzde commented Aug 20, 2021

extern is not legacy. It is needed to create a single definition of the constant. Otherwise you get linker errors due to multiple definitions. If you place the definition in the header file you also end up with multiple definitions, one for each inclusion of the header.

Why should we enclose constants in classes instead of namespaces?? The heavy-weight Library class is not needed if you just need to access the config object.

Copy link
Member

@daschuer daschuer left a comment

Choose a reason for hiding this comment

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

The issue with "extern" comes up when you have typos in the cpp file:

In this case you get the linker error like that, without a hint where the typo is:

undefined reference to `mixxx::library::prefs::kLegacyDirectoryConfigKey'

Including the library_prefs.h in the library_prefs.cpp is redundant in you current implementation.

If you do the same with a class, you get a compiler way earlier error with a detailed location:

library_prefs.cpp:27:5: error: ‘const ConfigKey mixxx::library::prefs::kLegacyDirectoryConfigKey1’ is not a static data member of ‘class mixxx::library::prefs’

I have just noticed that you can achieve an early compiler error as well, if you remove the inner namespace and use it as prefix

library_prefs.cpp:8:24: error: ‘mixxx::library::prefs::kLegacyDirectoryConfigKey1’ should have been declared inside ‘mixxx::library::prefs’

That solves the issue for me.

@uklotzde
Copy link
Contributor Author

I have removed the nested namespaces in the .cpp file. But this requires a comment that explains the reason why this is needed.

We should strive to find a fool-proof solution that could be applied to all use cases in a straight-forward manner.

@daschuer
Copy link
Member

The current solution with the extra comment is already good IMHO.
Merge?

@uklotzde
Copy link
Contributor Author

I wonder why only the Windows build failed? https://github.com/mixxxdj/mixxx/pull/4223/checks?check_run_id=3386161822

Should work now, let's see.

We could use this as the starting point and keep an eye on usability issues that might occur while migrating more ConfigObject access code.

Copy link
Member

@daschuer daschuer left a comment

Choose a reason for hiding this comment

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

OK, LGTM. Thank you.

@daschuer daschuer merged commit 7bb2f29 into mixxxdj:main Aug 21, 2021
@uklotzde uklotzde deleted the library_prefs branch August 21, 2021 11:15
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