Add simple runtime version check API (#970)

How to use:

  #include <tversionnumber.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
  }

---------

Co-authored-by: Urs Fleisch <ufleisch@users.sourceforge.net>
This commit is contained in:
Kevin André 2023-10-06 14:15:30 +02:00 committed by GitHub
parent 24e0ac7aa4
commit c332aa04f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 284 additions and 0 deletions

View File

@ -48,6 +48,7 @@ set(tag_HDRS
toolkit/tmap.tcc
toolkit/tpropertymap.h
toolkit/tdebuglistener.h
toolkit/tversionnumber.h
mpeg/mpegfile.h
mpeg/mpegproperties.h
mpeg/mpegheader.h
@ -307,6 +308,7 @@ set(toolkit_SRCS
toolkit/tpropertymap.cpp
toolkit/tdebuglistener.cpp
toolkit/tzlib.cpp
toolkit/tversionnumber.cpp
)
set(tag_LIB_SRCS

View File

@ -0,0 +1,54 @@
/***************************************************************************
copyright : (C) 2020 by Kevin Andre
email : hyperquantum@gmail.com
copyright : (C) 2023 by Urs Fleisch
email : ufleisch@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#include "tversionnumber.h"
#include "tstring.h"
#include "taglib.h"
using namespace TagLib;
////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////
String VersionNumber::toString() const
{
return String::number(majorVersion()) + '.' +
String::number(minorVersion()) + '.' +
String::number(patchVersion());
}
////////////////////////////////////////////////////////////////////////////////
// related functions
////////////////////////////////////////////////////////////////////////////////
VersionNumber TagLib::runtimeVersion()
{
return VersionNumber(TAGLIB_MAJOR_VERSION, TAGLIB_MINOR_VERSION, TAGLIB_PATCH_VERSION);
}

View File

@ -0,0 +1,143 @@
/***************************************************************************
copyright : (C) 2020 by Kevin Andre
email : hyperquantum@gmail.com
copyright : (C) 2023 by Urs Fleisch
email : ufleisch@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifndef TAGLIB_VERSIONNUMBER_H
#define TAGLIB_VERSIONNUMBER_H
#include "taglib_export.h"
namespace TagLib {
class String;
/*!
* Version number with major, minor and patch segments.
*/
class TAGLIB_EXPORT VersionNumber {
public:
/*!
* Constructs a version number from \a major, \a minor and \a patch segments.
*/
constexpr VersionNumber(unsigned int major, unsigned int minor,
unsigned int patch = 0)
: m_combined(((major & 0xff) << 16) | ((minor & 0xff) << 8)
| (patch & 0xff)) {
}
/*!
* Returns the version as an unsigned integer in the form
* (major version << 16) | (minor version << 8) | (patch version),
* e.g. 0x020100 for version 2.1.0.
*/
constexpr unsigned int combinedVersion() const {
return m_combined;
}
/*!
* Returns the major version, e.g. 2
*/
constexpr unsigned int majorVersion() const {
return (m_combined & 0xff0000) >> 16;
}
/*!
* Returns the minor version, e.g. 1
*/
constexpr unsigned int minorVersion() const {
return (m_combined & 0xff00) >> 8;
}
/*!
* Returns the patch version, e.g. 0
*/
constexpr unsigned int patchVersion() const {
return m_combined & 0xff;
}
/*!
* Returns true if this version is equal to \a rhs.
*/
constexpr bool operator==(const VersionNumber &rhs) const {
return m_combined == rhs.m_combined;
}
/*!
* Returns true if this version is not equal to \a rhs.
*/
constexpr bool operator!=(const VersionNumber &rhs) const {
return m_combined != rhs.m_combined;
}
/*!
* Returns true if this version is less than \a rhs.
*/
constexpr bool operator<(const VersionNumber &rhs) const {
return m_combined < rhs.m_combined;
}
/*!
* Returns true if this version is greater than \a rhs.
*/
constexpr bool operator>(const VersionNumber &rhs) const {
return m_combined > rhs.m_combined;
}
/*!
* Returns true if this version is less or equal than \a rhs.
*/
constexpr bool operator<=(const VersionNumber &rhs) const {
return m_combined <= rhs.m_combined;
}
/*!
* Returns true if this version is greater or equal than \a rhs.
*/
constexpr bool operator>=(const VersionNumber &rhs) const {
return m_combined >= rhs.m_combined;
}
/*!
* Returns a string with major, minor, and patch versions separated by
* periods.
*/
String toString() const;
private:
unsigned int m_combined;
};
/*!
* \relates TagLib::VersionNumber
* Returns the version number of TagLib in use at runtime.
* This does not need not be the version the application was compiled with.
*/
TAGLIB_EXPORT VersionNumber runtimeVersion();
} // namespace TagLib
#endif

View File

@ -69,6 +69,7 @@ SET(test_runner_SRCS
test_speex.cpp
test_dsf.cpp
test_sizes.cpp
test_versionnumber.cpp
)
INCLUDE_DIRECTORIES(${CPPUNIT_INCLUDE_DIR})

View File

@ -0,0 +1,84 @@
/***************************************************************************
copyright : (C) 2020 by Kevin Andre
email : hyperquantum@gmail.com
copyright : (C) 2023 by Urs Fleisch
email : ufleisch@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#include "tversionnumber.h"
#include "tstring.h"
#include "taglib.h"
#include <cppunit/extensions/HelperMacros.h>
using namespace TagLib;
class TestTagLib : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(TestTagLib);
CPPUNIT_TEST(testVersionNumber);
CPPUNIT_TEST(testRuntimeVersion);
CPPUNIT_TEST_SUITE_END();
public:
void testVersionNumber()
{
VersionNumber v210(2, 1, 0);
VersionNumber v211(2, 1, 1);
VersionNumber v220(2, 2, 0);
VersionNumber v300(3, 0, 0);
CPPUNIT_ASSERT_EQUAL(0x020100U, v210.combinedVersion());
CPPUNIT_ASSERT_EQUAL(2U, v210.majorVersion());
CPPUNIT_ASSERT_EQUAL(1U, v210.minorVersion());
CPPUNIT_ASSERT_EQUAL(0U, v210.patchVersion());
CPPUNIT_ASSERT(v210 == VersionNumber(2, 1));
CPPUNIT_ASSERT(!(v210 == v211));
CPPUNIT_ASSERT(v210 != v211);
CPPUNIT_ASSERT(!(v210 != v210));
CPPUNIT_ASSERT(v210 < v211);
CPPUNIT_ASSERT(!(v220 < v210));
CPPUNIT_ASSERT(v220 > v211);
CPPUNIT_ASSERT(!(v210 > v211));
CPPUNIT_ASSERT(v210 <= v210);
CPPUNIT_ASSERT(v210 <= v300);
CPPUNIT_ASSERT(!(v300 <= v220));
CPPUNIT_ASSERT(v210 >= v210);
CPPUNIT_ASSERT(v220 >= v210);
CPPUNIT_ASSERT(!(v210 >= v300));
CPPUNIT_ASSERT_EQUAL(String("2.1.0"), v210.toString());
}
void testRuntimeVersion()
{
CPPUNIT_ASSERT(runtimeVersion() >= VersionNumber(
TAGLIB_MAJOR_VERSION, TAGLIB_MINOR_VERSION));
CPPUNIT_ASSERT(runtimeVersion() >= VersionNumber(
TAGLIB_MAJOR_VERSION, TAGLIB_MINOR_VERSION, TAGLIB_PATCH_VERSION));
CPPUNIT_ASSERT(runtimeVersion() == VersionNumber(
TAGLIB_MAJOR_VERSION, TAGLIB_MINOR_VERSION, TAGLIB_PATCH_VERSION));
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(TestTagLib);