Skip to content

Add simple runtime version check API - #970

Merged
ufleisch merged 3 commits into
taglib:masterfrom
hyperquantum:runtime-version-check
Oct 6, 2023
Merged

ufleisch merged 3 commits into
taglib:masterfrom
hyperquantum:runtime-version-check

Conversation

@hyperquantum

@hyperquantum hyperquantum commented Sep 2, 2020 •

Copy link
Copy Markdown
Contributor

This adds a simple runtime version check as requested in #920 .

How to use:

  #include <taglib.h>

  if (TagLib::isTagLibVersionAtLeast(1, 11, 1)) {
    // running v1.11.1 or higher
  }

  if (TagLib::isTagLibVersionAtLeast(1, 12)) {
    // running v1.12 or higher
  }

EDIT: after the changes by @ufleisch this has become:

  #include <taglib/tversionnumber.h>

  using TagLib::VersionNumber;

  if (TagLib::runtimeVersion() >= VersionNumber(1, 11, 1)) {
    // running v1.11.1 or higher
  }

  if (TagLib::runtimeVersion() >= VersionNumber(1, 12)) {
    // running v1.12 or higher
  }

Fixes #920

@ufleisch ufleisch added the taglib2 Reconsider when breaking binary compatibility label Jan 12, 2021
@hyperquantum

Copy link
Copy Markdown
Contributor Author

Why has the label "taglib2" been applied to this PR? These changes were intended for version 1.

@ufleisch ufleisch removed the taglib2 Reconsider when breaking binary compatibility label Dec 31, 2021
@ufleisch

Copy link
Copy Markdown
Contributor

I removed the label "taglib2", since such a feature would not break binary compatibility. The reasoning was that it might be not so useful when introduced in a late 1.x version.

What I do not like about the suggested solution is that it covers exactly one use case, namely "is version at least". What if someone wants to know the exact version? So I would suggest a simpler API: Either a simple int tagLibVersion() function, which returns the TagLib version with 8 bits for major, minor and patch version (TAGLIB_MAJOR_VERSION << 16) + (TAGLIB_MINOR_VERSION << 8) + TAGLIB_PATCH_VERSION or a function returning the version as an object like QLibraryInfo::version(), but this might be a bit too much.

@hyperquantum

Copy link
Copy Markdown
Contributor Author

I initially only provided that one use case because:

  1. It seemed sufficient for doing compatibility checks.
  2. I was (and still am) reluctant of exposing the 'major', 'minor' and 'patch' components separately, because version number checks are easy to get wrong: if (major < 3 || minor < 11) { /* error: we require version 3.11 or higher */ } source

A while after I created this PR I checked out the taglib2 branch and discovered that it already has some support for getting the version numbers. Could this be reused? However I am not in favor of returning the TagLib version as one big number, it seems an inconvenient API to use. Suppose one wants to check if the version is 1.11.1 this way:

  • if (tagLibVersion() == ((1 << 16) | (11 << 8) | 1)) -- bit twiddling, ugh, not easy to read
  • if (tagLibVersion() == 0x010b01) -- hex, easy to get wrong, I almost wrote 0x011101 which means 1.17.1

What do you think about extending the existing code from this PR to something like this:

namespace TagLib {
  namespace Version {
    bool isAtLeast(int major, int minor);
    bool isAtLeast(int major, int minor, int patch);
    bool equals(int major, int minor, int patch);
    String asString(); /* returns <major>.<minor>.<patch>, e.g. "1.12.0" */
  }
}

Or alternatively, returning the version as an object would be a nice solution, although it may seem a bit overkill.

hyperquantum and others added 2 commits September 30, 2023 20:44
How to use:

  #include <taglib.h>

  if (TagLib::isTagLibVersionAtLeast(1, 11, 1)) {
    // running v1.11.1 or higher
  }

  if (TagLib::isTagLibVersionAtLeast(1, 12)) {
    // running v1.12 or higher
  }
How to use:

  #include "taglib.h"

  if (TagLib::runtimeVersion() >= TagLib::VersionNumber(1, 11, 1)) {
    // running v1.11.1 or higher
  }

  if (TagLib::runtimeVersion() >= TagLib::VersionNumber(1, 12)) {
    // running v1.12 or higher"
  }
@ufleisch
ufleisch force-pushed the runtime-version-check branch from 23741c8 to deac381 Compare September 30, 2023 20:15
@hyperquantum

Copy link
Copy Markdown
Contributor Author

Looks good to me.

It may be useful to also provide utility functions for converting a VersionNumber to a TagLib::String or std::string. But String probably cannot be used in taglib.h because tstring.h already includes taglib.h.

@ufleisch

ufleisch commented Oct 4, 2023

Copy link
Copy Markdown
Contributor

It is probably better to move the version number stuff into its own file. I have done this and also added a toString() method.

@ufleisch
ufleisch force-pushed the runtime-version-check branch from f22a844 to 2d877a3 Compare October 4, 2023 15:38
@hyperquantum

Copy link
Copy Markdown
Contributor Author

I have updated the example usage in the description of this PR.

For clarity I kept the code as similar as possible to the original, so the version numbers used (1.11.1 and 1.12) don't make much sense anymore, as the code will never be run on version 1.x.

@ufleisch
ufleisch merged commit c332aa0 into taglib:master Oct 6, 2023
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.

Runtime version check?

4 participants