mpd/src/tag/Mask.hxx

97 lines
2.2 KiB
C++
Raw Normal View History

/*
2021-01-01 19:54:25 +01:00
* Copyright 2003-2021 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_TAG_MASK_HXX
#define MPD_TAG_MASK_HXX
2017-02-07 16:57:16 +01:00
#include "Type.h"
#include <cstdint>
2017-02-07 16:57:16 +01:00
class TagMask {
typedef uint_least32_t mask_t;
mask_t value;
2019-11-05 10:56:20 +01:00
explicit constexpr TagMask(uint_least32_t _value) noexcept
2017-02-07 16:57:16 +01:00
:value(_value) {}
public:
TagMask() = default;
2019-11-05 10:56:20 +01:00
constexpr TagMask(TagType tag) noexcept
2017-02-07 16:57:16 +01:00
:value(mask_t(1) << mask_t(tag)) {}
2019-11-05 10:56:20 +01:00
static constexpr TagMask None() noexcept {
2017-02-07 16:57:16 +01:00
return TagMask(mask_t(0));
}
2019-11-05 10:56:20 +01:00
static constexpr TagMask All() noexcept {
2017-02-07 16:57:16 +01:00
return ~None();
}
2019-11-05 10:56:20 +01:00
constexpr TagMask operator~() const noexcept {
2017-02-07 16:57:16 +01:00
return TagMask(~value);
}
2019-11-05 10:56:20 +01:00
constexpr TagMask operator&(TagMask other) const noexcept {
2017-02-07 16:57:16 +01:00
return TagMask(value & other.value);
}
2019-11-05 10:56:20 +01:00
constexpr TagMask operator|(TagMask other) const noexcept {
2017-02-07 16:57:16 +01:00
return TagMask(value | other.value);
}
2019-11-05 10:56:20 +01:00
constexpr TagMask operator^(TagMask other) const noexcept {
2017-02-07 16:57:16 +01:00
return TagMask(value ^ other.value);
}
2019-11-05 10:56:20 +01:00
TagMask &operator&=(TagMask other) noexcept {
2019-02-02 15:14:31 +01:00
value &= other.value;
2017-02-07 16:57:16 +01:00
return *this;
}
2019-11-05 10:56:20 +01:00
TagMask &operator|=(TagMask other) noexcept {
2017-02-07 16:57:16 +01:00
value |= other.value;
return *this;
}
2019-11-05 10:56:20 +01:00
TagMask &operator^=(TagMask other) noexcept {
value ^= other.value;
2017-02-07 16:57:16 +01:00
return *this;
}
2019-11-05 10:56:20 +01:00
constexpr bool TestAny() const noexcept {
2017-02-07 16:57:16 +01:00
return value != 0;
}
2019-11-05 10:56:20 +01:00
constexpr bool Test(TagType tag) const noexcept {
2017-02-07 16:57:16 +01:00
return (*this & tag).TestAny();
}
2019-11-05 10:56:20 +01:00
void Set(TagType tag) noexcept {
2017-02-07 16:57:16 +01:00
*this |= tag;
}
2018-08-10 18:35:29 +02:00
2019-11-05 10:56:20 +01:00
void Unset(TagType tag) noexcept {
*this &= ~TagMask(tag);
2018-08-10 18:35:29 +02:00
}
2017-02-07 16:57:16 +01:00
};
#endif