diff --git a/taglib/CMakeLists.txt b/taglib/CMakeLists.txt index 480366d3..447eadaa 100644 --- a/taglib/CMakeLists.txt +++ b/taglib/CMakeLists.txt @@ -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 diff --git a/taglib/toolkit/tversionnumber.cpp b/taglib/toolkit/tversionnumber.cpp new file mode 100644 index 00000000..6c7cec5b --- /dev/null +++ b/taglib/toolkit/tversionnumber.cpp @@ -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); +} diff --git a/taglib/toolkit/tversionnumber.h b/taglib/toolkit/tversionnumber.h new file mode 100644 index 00000000..9b112fba --- /dev/null +++ b/taglib/toolkit/tversionnumber.h @@ -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 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 481cf0e2..4b02303c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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}) diff --git a/tests/test_versionnumber.cpp b/tests/test_versionnumber.cpp new file mode 100644 index 00000000..b9d2149d --- /dev/null +++ b/tests/test_versionnumber.cpp @@ -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 + +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);