Merge branch 'release-type' into develop

This commit is contained in:
emeric
2023-04-20 16:15:06 +02:00
37 changed files with 826 additions and 121 deletions
+23 -16
View File
@@ -50,7 +50,7 @@ using TagMap = std::map<std::string, std::vector<std::string>>;
template<typename T>
std::vector<T>
getPropertyValuesFirstMatchAs(const TagMap& tags, const std::vector<std::string_view>& keys)
getPropertyValuesFirstMatchAs(const TagMap& tags, std::initializer_list<std::string_view> keys)
{
std::vector<T> res;
@@ -83,7 +83,7 @@ getPropertyValuesFirstMatchAs(const TagMap& tags, const std::vector<std::string_
template <typename T>
std::optional<T>
getPropertyValueFirstMatchAs(const TagMap& tags, const std::vector<std::string_view>& keys)
getPropertyValueFirstMatchAs(const TagMap& tags, std::initializer_list<std::string_view> keys)
{
std::optional<T> res;
std::vector<T> values {getPropertyValuesFirstMatchAs<T>(tags, keys)};
@@ -95,14 +95,14 @@ getPropertyValueFirstMatchAs(const TagMap& tags, const std::vector<std::string_v
template <typename T>
std::vector<T>
getPropertyValuesAs(const TagMap& tags, const std::string& key)
getPropertyValuesAs(const TagMap& tags, std::string_view key)
{
return getPropertyValuesFirstMatchAs<T>(tags, {key});
}
template <typename T>
std::optional<T>
getPropertyValueAs(const TagMap& tags, const std::string& key)
getPropertyValueAs(const TagMap& tags, std::string_view key)
{
return getPropertyValueFirstMatchAs<T>(tags, {key});
}
@@ -121,22 +121,22 @@ splitAndTrimString(std::string_view str, std::string_view delimiters)
static
std::vector<Artist>
getArtists(const TagMap& tags,
const std::vector<std::string_view>& artistTagNames,
const std::vector<std::string_view>& artistSortTagNames,
const std::vector<std::string_view>& artistMBIDTagNames
std::initializer_list<std::string_view> artistTagNames,
std::initializer_list<std::string_view> artistSortTagNames,
std::initializer_list<std::string_view> artistMBIDTagNames
)
{
const std::vector<std::string> artistNames {getPropertyValuesFirstMatchAs<std::string>(tags, artistTagNames)};
const std::vector<std::string_view> artistNames {getPropertyValuesFirstMatchAs<std::string_view>(tags, artistTagNames)};
if (artistNames.empty())
return {};
std::vector<Artist> artists;
artists.reserve(artistNames.size());
std::transform(std::cbegin(artistNames), std::cend(artistNames), std::back_inserter(artists),
[&](const std::string& name) { return Artist {name}; });
[&](std::string_view name) { return Artist {name}; });
{
const std::vector<std::string> artistSortNames {getPropertyValuesFirstMatchAs<std::string>(tags, artistSortTagNames)};
const std::vector<std::string_view> artistSortNames {getPropertyValuesFirstMatchAs<std::string_view>(tags, artistSortTagNames)};
if (artistSortNames.size() == artists.size())
{
for (std::size_t i {}; i < artistSortNames.size(); ++i)
@@ -161,14 +161,14 @@ getArtists(const TagMap& tags,
static
PerformerContainer
getPerformerArtists(const TagMap& tags,
const std::vector<std::string_view>& artistTagNames)
std::initializer_list<std::string_view> artistTagNames)
{
PerformerContainer performers;
// picard stores like this: (see https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html#performer)
// We may hit both styles for the same track
// PERFORMER: artist (role)
if (const std::vector<std::string> artistNames {getPropertyValuesFirstMatchAs<std::string>(tags, artistTagNames)}; !artistNames.empty())
if (const std::vector<std::string_view> artistNames {getPropertyValuesFirstMatchAs<std::string_view>(tags, artistTagNames)}; !artistNames.empty())
{
for (std::string_view entry : artistNames)
{
@@ -216,7 +216,7 @@ getRelease(const TagMap& tags)
if (!release->mediumCount)
{
// mediumCount may be encoded as "position/count"
if (const auto value {getPropertyValueAs<std::string>(tags, "DISCNUMBER")})
if (const auto value {getPropertyValueAs<std::string_view>(tags, "DISCNUMBER")})
{
// Expecting 'Number/Total'
const std::vector<std::string_view> strings {StringUtils::splitString(*value, "/") };
@@ -225,6 +225,13 @@ getRelease(const TagMap& tags)
}
}
release->primaryType = getPropertyValueFirstMatchAs<MetaData::Release::PrimaryType>(tags, {"MUSICBRAINZ_ALBUMTYPE", "RELEASETYPE", "MUSICBRAINZ ALBUM TYPE", "MUSICBRAINZ/ALBUM TYPE"});
if (release->primaryType)
{
const auto secondaryTypes {getPropertyValuesFirstMatchAs<MetaData::Release::SecondaryType>(tags, {"MUSICBRAINZ_ALBUMTYPE", "RELEASETYPE", "MUSICBRAINZ ALBUM TYPE", "MUSICBRAINZ/ALBUM TYPE"})};
release->secondaryTypes.assign(std::cbegin(secondaryTypes), std::cend(secondaryTypes));
}
return release;
}
@@ -241,7 +248,7 @@ getMedium(const TagMap& tags)
if (!medium->trackCount)
{
// totalTracks may be encoded as "position/count"
if (const auto value {getPropertyValueAs<std::string>(tags, "TRACKNUMBER")})
if (const auto value {getPropertyValueAs<std::string_view>(tags, "TRACKNUMBER")})
{
// Expecting 'Number/Total'
const std::vector<std::string_view> strings {StringUtils::splitString(*value, "/") };
@@ -445,7 +452,7 @@ TagLibParser::parse(const std::filesystem::path& p, bool debug)
for (const auto& [name, attributeList] : tag->attributeListMap())
{
std::string strName {name.to8Bit(true)};
std::string strName {StringUtils::stringToUpper(name.to8Bit(true))};
if (strName.find("WM/") == 0 || tags.find(strName) != std::cend(tags))
continue;
@@ -461,7 +468,7 @@ TagLibParser::parse(const std::filesystem::path& p, bool debug)
if (debug)
std::cout << "ASF property: '" << name << "'" << std::endl;
tags[strName] = std::move(attributes);
tags.emplace(strName, std::move(attributes));
}
}
}
+47
View File
@@ -119,3 +119,50 @@ namespace MetaData::Utils
}
}
namespace StringUtils
{
static bool iequals(std::string_view a, std::string_view b)
{
return std::equal(std::cbegin(a), std::cend(a),
std::cbegin(b), std::cend(b),
[](char a, char b) { return tolower(a) == tolower(b);}
);
}
template<>
std::optional<MetaData::Release::PrimaryType> readAs(std::string_view str)
{
str = stringTrim(str);
if (iequals(str, "album"))
return MetaData::Release::PrimaryType::Album;
else if (iequals(str, "single"))
return MetaData::Release::PrimaryType::Single;
else if (iequals(str, "EP"))
return MetaData::Release::PrimaryType::EP;
else if (iequals(str, "broadcast"))
return MetaData::Release::PrimaryType::Broadcast;
else if (iequals(str, "other"))
return MetaData::Release::PrimaryType::Other;
return std::nullopt;
}
template<>
std::optional<MetaData::Release::SecondaryType> readAs(std::string_view str)
{
str = stringTrim(str);
if (iequals(str, "compilation"))
return MetaData::Release::SecondaryType::Compilation;
else if (iequals(str, "soundtrack"))
return MetaData::Release::SecondaryType::Soundtrack;
else if (iequals(str, "live"))
return MetaData::Release::SecondaryType::Live;
else if (iequals(str, "demo"))
return MetaData::Release::SecondaryType::Demo;
return std::nullopt;
}
}
+9 -1
View File
@@ -38,6 +38,14 @@ namespace MetaData::Utils
// format is "artist name (role)"
PerformerArtist extractPerformerAndRole(std::string_view entry);
}
namespace StringUtils
{
template<>
std::optional<MetaData::Release::PrimaryType> readAs(std::string_view str);
template<>
std::optional<MetaData::Release::SecondaryType> readAs(std::string_view str);
}
@@ -29,6 +29,7 @@
#include <vector>
#include <Wt/WDate.h>
#include "utils/EnumSet.hpp"
#include "utils/UUID.hpp"
namespace MetaData
@@ -51,10 +52,37 @@ namespace MetaData
struct Release
{
// see https://musicbrainz.org/doc/Release_Group/Type
enum class PrimaryType
{
Album,
Single,
EP,
Broadcast,
Other
};
enum class SecondaryType
{
Compilation,
Soundtrack,
Spokenword,
Interview,
Audiobook,
AudioDrama,
Live,
Remix,
DJMix,
Mixtape_Street,
Demo,
};
std::optional<UUID> mbid;
std::string name;
std::vector<Artist> artists;
std::optional<std::size_t> mediumCount;
std::optional<PrimaryType> primaryType;
EnumSet<SecondaryType> secondaryTypes;
};
struct Medium
+57
View File
@@ -115,3 +115,60 @@ TEST(MetaData, extractPerformerAndRole)
EXPECT_EQ(performer.role, testCase.expectedRole) << " str was '" << testCase.str << "'";
}
}
TEST(MetaData, primaryReleaseTypes)
{
using namespace MetaData;
struct TestCase
{
std::string str;
std::optional<Release::PrimaryType> result;
} testCases []
{
{ "", std::nullopt },
{ "album", Release::PrimaryType::Album },
{ "Album", Release::PrimaryType::Album },
{ " Album", Release::PrimaryType::Album },
{ "Album ", Release::PrimaryType::Album },
{ "ep", Release::PrimaryType::EP },
{ " ep ", Release::PrimaryType::EP },
{ "broadcast", Release::PrimaryType::Broadcast },
{ "single", Release::PrimaryType::Single },
{ "other", Release::PrimaryType::Other },
};
for (const TestCase& testCase : testCases)
{
std::optional<Release::PrimaryType> parsed {StringUtils::readAs<Release::PrimaryType>(testCase.str)};
EXPECT_EQ(parsed, testCase.result) << " str was '" << testCase.str << "'";
}
}
TEST(MetaData, secondaryReleaseTypes)
{
using namespace MetaData;
struct TestCase
{
std::string str;
std::optional<Release::SecondaryType> result;
} testCases []
{
{ "", std::nullopt },
{ "compilation", Release::SecondaryType::Compilation },
{ " compilation ", Release::SecondaryType::Compilation },
{ "soundtrack", Release::SecondaryType::Soundtrack },
{ "live", Release::SecondaryType::Live },
{ "demo", Release::SecondaryType::Demo },
};
for (const TestCase& testCase : testCases)
{
std::optional<Release::SecondaryType> parsed {StringUtils::readAs<Release::SecondaryType>(testCase.str)};
EXPECT_EQ(parsed, testCase.result) << " str was '" << testCase.str << "'";
}
}
@@ -28,6 +28,7 @@
#include "utils/Logger.hpp"
#include "SqlQuery.hpp"
#include "Utils.hpp"
#include "EnumSetTraits.hpp"
#include "IdTypeTraits.hpp"
namespace Database
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2023 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <type_traits>
#include <Wt/Dbo/StdSqlTraits.h>
#include "utils/EnumSet.hpp"
namespace Wt::Dbo
{
template<typename T>
struct sql_value_traits<EnumSet<T>, void> : public sql_value_traits<long long>
{
using ValueType = typename EnumSet<T>::ValueType;
static_assert(sizeof(long long) > sizeof(ValueType));
static void bind(EnumSet<T> v, SqlStatement *statement, int column, int size)
{
sql_value_traits<long long>::bind(static_cast<long long>(v.getBitfield()), statement, column, size);
}
static bool read(EnumSet<T>& v, SqlStatement *statement, int column, int size)
{
long long val;
if (sql_value_traits<long long>::read(val, statement, column, size))
{
v.setBitfield(val);
return true;
}
v.clear();
return false;
}
};
}
@@ -653,6 +653,18 @@ CREATE TABLE IF NOT EXISTS "track_backup" (
ScanSettings::get(session).modify()->incScanVersion();
}
static
void
migrateFromV39(Session& session)
{
// add release type
session.getDboSession().execute("ALTER TABLE release ADD primary_type INTEGER");
session.getDboSession().execute("ALTER TABLE release ADD secondary_types INTEGER");
// Just increment the scan version of the settings to make the next scheduled scan rescan everything
ScanSettings::get(session).modify()->incScanVersion();
}
void
doDbMigration(Session& session)
{
@@ -698,6 +710,7 @@ CREATE TABLE IF NOT EXISTS "track_backup" (
{36, migrateFromV36},
{37, migrateFromV37},
{38, migrateFromV38},
{39, migrateFromV39},
};
while (1)
@@ -26,7 +26,7 @@ namespace Database
class Session;
using Version = std::size_t;
static constexpr Version LMS_DATABASE_VERSION {39};
static constexpr Version LMS_DATABASE_VERSION {40};
class VersionInfo
{
public:
@@ -28,6 +28,7 @@
#include "services/database/User.hpp"
#include "utils/Logger.hpp"
#include "SqlQuery.hpp"
#include "EnumSetTraits.hpp"
#include "IdTypeTraits.hpp"
#include "Utils.hpp"
@@ -138,6 +139,11 @@ createQuery(Session& session, const Release::FindParameters& params)
query.where(oss.str());
}
if (params.primaryType)
query.where("primary_type = ?").bind(*params.primaryType);
if (!params.secondaryTypes.empty())
query.where("secondary_type = ?").bind(params.secondaryTypes);
switch (params.sortMethod)
{
case ReleaseSortMethod::None:
@@ -40,6 +40,7 @@
#include "services/database/TrackList.hpp"
#include "services/database/TrackFeatures.hpp"
#include "services/database/User.hpp"
#include "EnumSetTraits.hpp"
#include "Migration.hpp"
namespace Database
@@ -61,6 +61,8 @@ class Release : public Object<Release, ReleaseId>
ArtistId artist; // only releases that involved this user
EnumSet<TrackArtistLinkType> trackArtistLinkTypes; // and for these link types
EnumSet<TrackArtistLinkType> excludedTrackArtistLinkTypes; // but not for these link types
std::optional<ReleaseTypePrimary> primaryType; // if, set, matching this primary type
EnumSet<ReleaseTypeSecondary> secondaryTypes; // Matching all this (if any)
FindParameters& setClusters(const std::vector<ClusterId>& _clusters) { clusters = _clusters; return *this; }
FindParameters& setKeywords(const std::vector<std::string_view>& _keywords) { keywords = _keywords; return *this; }
@@ -109,11 +111,15 @@ class Release : public Object<Release, ReleaseId>
std::size_t getDiscCount() const; // may not be total disc (if incomplete for example)
std::chrono::milliseconds getDuration() const;
Wt::WDateTime getLastWritten() const;
std::optional<ReleaseTypePrimary> getPrimaryType() const { return _primaryType; }
EnumSet<ReleaseTypeSecondary> getSecondaryTypes() const { return _secondaryTypes; }
// Setters
void setName(std::string_view name) { _name = name; }
void setMBID(const std::optional<UUID>& mbid) { _MBID = mbid ? mbid->getAsString() : ""; }
void setTotalDisc(std::optional<int> totalDisc) { _totalDisc = totalDisc; }
void setPrimaryType(std::optional<ReleaseTypePrimary> type) { _primaryType = type; }
void setSecondaryTypes(EnumSet<ReleaseTypeSecondary> types) { _secondaryTypes = types; }
// Get the artists of this release
std::vector<ObjectPtr<Artist>> getArtists(TrackArtistLinkType type = TrackArtistLinkType::Artist) const;
@@ -128,6 +134,8 @@ class Release : public Object<Release, ReleaseId>
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::field(a, _MBID, "mbid");
Wt::Dbo::field(a, _totalDisc, "total_disc");
Wt::Dbo::field(a, _primaryType, "primary_type");
Wt::Dbo::field(a, _secondaryTypes, "secondary_types");
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToOne, "release");
}
@@ -139,9 +147,11 @@ class Release : public Object<Release, ReleaseId>
static constexpr std::size_t _maxNameLength {128};
std::string _name;
std::string _MBID;
std::optional<int> _totalDisc {};
std::string _name;
std::string _MBID;
std::optional<int> _totalDisc {};
std::optional<ReleaseTypePrimary> _primaryType;
EnumSet<ReleaseTypeSecondary> _secondaryTypes;
Wt::Dbo::collection<Wt::Dbo::ptr<Track>> _tracks; // Tracks in the release
};
@@ -191,5 +191,30 @@ namespace Database
Playlist, // user controlled playlists
Internal, // internal usage (current playqueue, history, ...)
};
// as defined in https://musicbrainz.org/doc/Release_Group/Type
enum class ReleaseTypePrimary
{
Album,
Single,
EP,
Broadcast,
Other,
};
enum class ReleaseTypeSecondary
{
Compilation,
Soundtrack,
Spokenword,
Interview,
Audiobook,
AudioDrama,
Live,
Remix,
DJMix,
Mixtape_Street,
Demo,
};
}
@@ -507,3 +507,26 @@ TEST_F(DatabaseFixture, Release_getDiscCount)
EXPECT_EQ(release.get()->getDiscCount(), 2);
}
}
TEST_F(DatabaseFixture, Release_releaseType)
{
ScopedRelease release {session, "MyRelease"};
{
auto transaction {session.createSharedTransaction()};
EXPECT_EQ(release.get()->getPrimaryType(), std::nullopt);
EXPECT_EQ(release.get()->getSecondaryTypes(), EnumSet<ReleaseTypeSecondary> {});
}
{
auto transaction {session.createUniqueTransaction()};
release.get().modify()->setPrimaryType({ ReleaseTypePrimary::Album });
release.get().modify()->setSecondaryTypes({ ReleaseTypeSecondary::Compilation });
}
{
auto transaction {session.createSharedTransaction()};
EXPECT_EQ(release.get()->getPrimaryType(), ReleaseTypePrimary::Album);
EXPECT_TRUE(release.get()->getSecondaryTypes().contains(ReleaseTypeSecondary::Compilation));
}
}
@@ -115,6 +115,67 @@ namespace
return artists;
}
ReleaseTypePrimary convertReleaseTypePrimary(MetaData::Release::PrimaryType type)
{
switch (type)
{
case MetaData::Release::PrimaryType::Album: return ReleaseTypePrimary::Album;
case MetaData::Release::PrimaryType::Single: return ReleaseTypePrimary::Single;
case MetaData::Release::PrimaryType::EP: return ReleaseTypePrimary::EP;
case MetaData::Release::PrimaryType::Broadcast: return ReleaseTypePrimary::Broadcast;
case MetaData::Release::PrimaryType::Other: return ReleaseTypePrimary::Other;
}
return ReleaseTypePrimary::Other;
}
EnumSet<ReleaseTypeSecondary> convertReleaseTypesSecondary(EnumSet<MetaData::Release::SecondaryType> types)
{
EnumSet<ReleaseTypeSecondary> res;
for (MetaData::Release::SecondaryType type : types)
{
switch (type)
{
case MetaData::Release::SecondaryType::Compilation:
res.insert(ReleaseTypeSecondary::Compilation);
break;
case MetaData::Release::SecondaryType::Soundtrack:
res.insert(ReleaseTypeSecondary::Soundtrack);
break;
case MetaData::Release::SecondaryType::Spokenword:
res.insert(ReleaseTypeSecondary::Spokenword);
break;
case MetaData::Release::SecondaryType::Interview:
res.insert(ReleaseTypeSecondary::Interview);
break;
case MetaData::Release::SecondaryType::Audiobook:
res.insert(ReleaseTypeSecondary::Audiobook);
break;
case MetaData::Release::SecondaryType::AudioDrama:
res.insert(ReleaseTypeSecondary::AudioDrama);
break;
case MetaData::Release::SecondaryType::Live:
res.insert(ReleaseTypeSecondary::Live);
break;
case MetaData::Release::SecondaryType::Remix:
res.insert(ReleaseTypeSecondary::Remix);
break;
case MetaData::Release::SecondaryType::DJMix:
res.insert(ReleaseTypeSecondary::DJMix);
break;
case MetaData::Release::SecondaryType::Mixtape_Street:
res.insert(ReleaseTypeSecondary::Mixtape_Street);
break;
case MetaData::Release::SecondaryType::Demo:
res.insert(ReleaseTypeSecondary::Demo);
break;
}
}
return res;
}
void
updateReleaseIfNeeded(Release::pointer release, const MetaData::Release& releaseInfo)
{
@@ -122,6 +183,15 @@ namespace
release.modify()->setName(releaseInfo.name);
if (release->getTotalDisc() != releaseInfo.mediumCount)
release.modify()->setTotalDisc(releaseInfo.mediumCount);
if (releaseInfo.primaryType)
{
const ReleaseTypePrimary primaryType {convertReleaseTypePrimary(*releaseInfo.primaryType)};
if (release->getPrimaryType() != primaryType)
release.modify()->setPrimaryType(primaryType);
}
const EnumSet<ReleaseTypeSecondary> secondaryTypes {convertReleaseTypesSecondary(releaseInfo.secondaryTypes)};
if (release->getSecondaryTypes() != secondaryTypes)
release.modify()->setSecondaryTypes(secondaryTypes);
}
Release::pointer
+7
View File
@@ -63,6 +63,13 @@ readAs(std::string_view str)
return std::string {str};
}
template<>
std::optional<std::string_view>
readAs(std::string_view str)
{
return str;
}
template<>
std::optional<bool>
readAs(std::string_view str)
+44 -12
View File
@@ -31,9 +31,11 @@ class EnumSet
static_assert(std::is_enum<T>::value);
static_assert(std::is_same<underlying_type, std::uint64_t>::value || std::is_same<underlying_type, std::uint32_t>::value);
using index_type = std::uint_fast8_t;
using IndexType = std::uint_fast8_t;
public:
using ValueType = underlying_type;
EnumSet() = default;
constexpr EnumSet(std::initializer_list<T> values)
{
@@ -44,6 +46,13 @@ class EnumSet
template <typename It>
constexpr EnumSet(It begin, It end)
{
assign(begin, end);
}
template <typename It>
constexpr void assign(It begin, It end)
{
clear();
for (It it {begin}; it != end; ++it)
insert(*it);
}
@@ -71,6 +80,11 @@ class EnumSet
return _bitfield & (underlying_type{ 1 } << static_cast<underlying_type>(value));
}
constexpr void clear()
{
_bitfield = 0;
}
class iterator
{
public:
@@ -100,14 +114,14 @@ class EnumSet
private:
friend class EnumSet;
constexpr iterator(const EnumSet& _container, index_type _index)
constexpr iterator(const EnumSet& _container, IndexType _index)
: _container {_container}
, _index {_index}
{
}
const EnumSet& _container;
index_type _index;
IndexType _index;
};
constexpr iterator begin() const
@@ -120,25 +134,45 @@ class EnumSet
return iterator {*this, npos};
}
private:
static_assert(std::numeric_limits<index_type>::max() >= sizeof(underlying_type) * 8);
enum : index_type { npos = sizeof(underlying_type) * 8 };
constexpr underlying_type getBitfield() const
{
return _bitfield;
}
constexpr index_type getFirstBitSetIndex(index_type start = {}) const
constexpr void setBitfield(underlying_type bitfield)
{
_bitfield = bitfield;
}
constexpr bool operator==(const EnumSet other) const
{
return _bitfield == other._bitfield;
}
constexpr bool operator!=(const EnumSet other) const
{
return _bitfield != other._bitfield;
}
private:
static_assert(std::numeric_limits<IndexType>::max() >= sizeof(underlying_type) * 8);
enum : IndexType { npos = sizeof(underlying_type) * 8 };
constexpr IndexType getFirstBitSetIndex(IndexType start = {}) const
{
assert(start < npos);
// return npos if no bit found
index_type res {countTrailingZero(_bitfield >> start)};
IndexType res {countTrailingZero(_bitfield >> start)};
if (res == npos)
return res;
return res + start;
}
static constexpr index_type countTrailingZero(underlying_type bitField)
static constexpr IndexType countTrailingZero(underlying_type bitField)
{
index_type res {};
IndexType res {};
while (res < (sizeof(underlying_type) * 8) && (bitField & 1) == 0)
{
@@ -154,5 +188,3 @@ class EnumSet
underlying_type _bitfield{};
};
+5
View File
@@ -88,6 +88,11 @@ template<>
std::optional<std::string>
readAs(std::string_view str);
template<>
[[nodiscard]]
std::optional<std::string_view>
readAs(std::string_view str);
template<>
[[nodiscard]]
std::optional<bool>
+2 -1
View File
@@ -1,8 +1,9 @@
include(GoogleTest)
add_executable(test-utils
String.cpp
EnumSet.cpp
RecursiveSharedMutex.cpp
String.cpp
Utils.cpp
)
+60
View File
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtest/gtest.h>
#include "utils/EnumSet.hpp"
TEST(EnumSet, ctr)
{
enum class Foo
{
One,
Two,
};
{
constexpr EnumSet<Foo> test {Foo::One};
static_assert(!test.empty());
static_assert(test.contains(Foo::One));
static_assert(!test.contains(Foo::Two));
EXPECT_TRUE(!test.empty());
EXPECT_TRUE(test.contains(Foo::One));
EXPECT_FALSE(test.contains(Foo::Two));
static_assert(test.getBitfield() != 0);
}
{
constexpr EnumSet<Foo> test {Foo::One, Foo::Two};
constexpr auto bitfield {test.getBitfield()};
EnumSet<Foo> test2;
EXPECT_FALSE(test2.contains(Foo::One));
EXPECT_FALSE(test2.contains(Foo::Two));
test2.setBitfield(bitfield);
EXPECT_TRUE(test2.contains(Foo::One));
EXPECT_TRUE(test2.contains(Foo::Two));
EXPECT_EQ(test, test2);
}
}