tag/Mask: wrap in class

This commit is contained in:
Max Kellermann
2017-02-07 16:57:16 +01:00
parent 17097d96b7
commit 2f3f075e4f
16 changed files with 114 additions and 42 deletions

View File

@@ -37,7 +37,7 @@ TagLoadConfig()
if (value == nullptr)
return;
global_tag_mask = 0;
global_tag_mask = TagMask::None();
if (StringEqualsCaseASCII(value, "none"))
return;
@@ -60,7 +60,7 @@ TagLoadConfig()
FormatFatalError("error parsing metadata item \"%s\"",
c);
global_tag_mask |= tag_mask_t(1) << unsigned(type);
global_tag_mask |= type;
s++;
c = s;

View File

@@ -20,8 +20,73 @@
#ifndef MPD_TAG_MASK_HXX
#define MPD_TAG_MASK_HXX
#include "Type.h"
#include <stdint.h>
typedef uint_least32_t tag_mask_t;
class TagMask {
typedef uint_least32_t mask_t;
mask_t value;
explicit constexpr TagMask(uint_least32_t _value)
:value(_value) {}
public:
TagMask() = default;
constexpr TagMask(TagType tag)
:value(mask_t(1) << mask_t(tag)) {}
static constexpr TagMask None() {
return TagMask(mask_t(0));
}
static constexpr TagMask All() {
return ~None();
}
constexpr TagMask operator~() const {
return TagMask(~value);
}
constexpr TagMask operator&(TagMask other) const {
return TagMask(value & other.value);
}
constexpr TagMask operator|(TagMask other) const {
return TagMask(value | other.value);
}
constexpr TagMask operator^(TagMask other) const {
return TagMask(value ^ other.value);
}
TagMask &operator&=(TagMask other) {
value |= other.value;
return *this;
}
TagMask &operator|=(TagMask other) {
value |= other.value;
return *this;
}
TagMask &operator^=(TagMask other) {
value |= other.value;
return *this;
}
constexpr bool TestAny() const {
return value != 0;
}
constexpr bool Test(TagType tag) const {
return (*this & tag).TestAny();
}
void Set(TagType tag) {
*this |= tag;
}
};
#endif

View File

@@ -19,6 +19,7 @@
#include "Set.hxx"
#include "Builder.hxx"
#include "Mask.hxx"
#include "Settings.hxx"
#include <assert.h>
@@ -58,16 +59,16 @@ CopyTagItem(TagBuilder &dest, const Tag &src, TagType type)
* Copy all tag items of the types in the mask.
*/
static void
CopyTagMask(TagBuilder &dest, const Tag &src, tag_mask_t mask)
CopyTagMask(TagBuilder &dest, const Tag &src, TagMask mask)
{
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
if ((mask & (tag_mask_t(1) << i)) != 0)
if (mask.Test(TagType(i)))
CopyTagItem(dest, src, TagType(i));
}
void
TagSet::InsertUnique(const Tag &src, TagType type, const char *value,
tag_mask_t group_mask)
TagMask group_mask)
{
TagBuilder builder;
if (value == nullptr)
@@ -85,7 +86,7 @@ TagSet::InsertUnique(const Tag &src, TagType type, const char *value,
bool
TagSet::CheckUnique(TagType dest_type,
const Tag &tag, TagType src_type,
tag_mask_t group_mask)
TagMask group_mask)
{
bool found = false;
@@ -101,12 +102,12 @@ TagSet::CheckUnique(TagType dest_type,
void
TagSet::InsertUnique(const Tag &tag,
TagType type, tag_mask_t group_mask)
TagType type, TagMask group_mask)
{
static_assert(sizeof(group_mask) * 8 >= TAG_NUM_OF_ITEM_TYPES,
"Mask is too small");
assert((group_mask & (tag_mask_t(1) << unsigned(type))) == 0);
assert(!group_mask.Test(type));
if (!CheckUnique(type, tag, type, group_mask) &&
(type != TAG_ALBUM_ARTIST ||

View File

@@ -22,12 +22,13 @@
#include "Compiler.h"
#include "Tag.hxx"
#include "Mask.hxx"
#include <set>
#include <string.h>
class TagMask;
/**
* Helper class for #TagSet which compares two #Tag objects.
*/
@@ -59,15 +60,15 @@ struct TagLess {
class TagSet : public std::set<Tag, TagLess> {
public:
void InsertUnique(const Tag &tag,
TagType type, tag_mask_t group_mask);
TagType type, TagMask group_mask);
private:
void InsertUnique(const Tag &src, TagType type, const char *value,
tag_mask_t group_mask);
TagMask group_mask);
bool CheckUnique(TagType dest_type,
const Tag &tag, TagType src_type,
tag_mask_t group_mask);
TagMask group_mask);
};
#endif

View File

@@ -19,4 +19,4 @@
#include "Settings.hxx"
tag_mask_t global_tag_mask = (tag_mask_t)-1 & ~(1 << TAG_COMMENT);
TagMask global_tag_mask = TagMask::All() & ~TagMask(TAG_COMMENT);

View File

@@ -24,20 +24,20 @@
#include "Type.h"
#include "Compiler.h"
extern tag_mask_t global_tag_mask;
gcc_const
static inline bool
IsTagEnabled(unsigned tag)
{
return global_tag_mask & (1u << tag);
}
extern TagMask global_tag_mask;
gcc_const
static inline bool
IsTagEnabled(TagType tag)
{
return IsTagEnabled(unsigned(tag));
return global_tag_mask.Test(tag);
}
gcc_const
static inline bool
IsTagEnabled(unsigned tag)
{
return IsTagEnabled(TagType(tag));
}
#endif