8#ifndef Sawyer_BitFlags_H
9#define Sawyer_BitFlags_H
11#include <Sawyer/Sawyer.h>
62template<
typename E,
typename V =
int64_t>
71#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
73 friend class boost::serialization::access;
76 void serialize(S &s,
const unsigned ) {
77 s & BOOST_SERIALIZATION_NVP(vector_);
81#ifdef SAWYER_HAVE_CEREAL
83 friend class cereal::access;
85 template<
class Archive>
86 void serialize(Archive &archive) {
87 archive(CEREAL_NVP(vector_));
101 : vector_(Vector(e)) {}
105 : vector_(other.vector_) {}
114 return (vector_ & Vector(e)) != 0;
119 return (vector_ & other.vector_) == other.vector_;
124 return (vector_ & other.vector_) != 0;
144 vector_ |= Vector(e);
150 vector_ |= other.vector_;
156 vector_ &= ~Vector(e);
162 vector_ &= ~other.vector_;
174 bool retval =
isSet(e);
181 bool retval =
isSet(e);
194 vector_ = other.vector_;
202 return vector_ | other.vector_;
205 return vector_ | Vector(e);
213 return vector_ & other.vector_;
216 return vector_ & Vector(e);
230 return vector_ == other.vector_;
233 return vector_ != other.vector_;
236 return vector_ < other.vector_;
239 return vector_ <= other.vector_;
242 return vector_ > other.vector_;
245 return vector_ >= other.vector_;
254 std::vector<Enum>
split(std::vector<int64_t> constants, Vector &leftovers )
const {
255 leftovers = Vector(0);
256 std::vector<Enum> retval;
257 std::sort(constants.begin(), constants.end(), moreBits);
258 Vector tmp = vector_;
261 for (
size_t i=0; i<constants.size() && !found; ++i) {
262 if (Vector(tmp & constants[i]) == Vector(constants[i]) && constants[i] != 0) {
263 retval.push_back(Enum(constants[i]));
264 tmp &= ~constants[i];
278 std::vector<Enum> retval;
279 for (
size_t i = 0; i < 8*
sizeof(Enum); ++i) {
280 Enum e =
static_cast<Enum
>(uint64_t(1) << i);
287#if __cplusplus >= 201103L
292 void each(std::vector<int64_t> constants,
const F &functor)
const {
294 for (Enum e:
split(constants, leftovers))
302 void each(
const F &functor)
const {
303 for (Enum e:
split())
309 static size_t nBits(Vector
vector) {
311 for (
size_t i = 0; i < 8*
sizeof(Vector); ++i) {
312 if ((
vector & (Vector(1) << i)) != 0)
318 static bool moreBits(Vector a, Vector b) {
319 return nBits(a) > nBits(b);
Stores a vector of enum bit flags.
BitFlags & operator=(BitFlags other)
Set the vector to the same as another.
BitFlags & set(Enum e)
Set the specified bit.
BitFlags & clear(BitFlags other)
Clear all bits that are set in other.
bool operator<(BitFlags other) const
Compare two vectors.
bool isClear(Enum e) const
Test whether a bit is clear.
BitFlags & clear(Enum e)
Clear the specified bit.
BitFlags operator|(Enum e) const
Create a new vector that's the union of two vectors.
BitFlags(const BitFlags &other)
Copy constructor.
BitFlags operator|(BitFlags other) const
Create a new vector that's the union of two vectors.
bool operator<=(BitFlags other) const
Compare two vectors.
bool operator!=(BitFlags other) const
Compare two vectors.
Vector vector() const
Current value of the bit vector.
BitFlags intersection(Enum e) const
Create a new vector that's the intersection of two vectors.
bool operator==(BitFlags other) const
Compare two vectors.
std::vector< Enum > split(std::vector< int64_t > constants, Vector &leftovers) const
Split a vector into the individual enum values.
BitFlags intersection(BitFlags other) const
Create a new vector that's the intersection of two vectors.
bool operator>(BitFlags other) const
Compare two vectors.
bool isAllSet(BitFlags other) const
True if all specified bits are set.
bool testAndSet(Enum e)
Test whether a bit is set, then set it.
bool isAnySet() const
True if any bit is set.
BitFlags operator&(Enum e) const
Create a new vector that's the intersection of two vectors.
std::vector< Enum > split() const
Split a vector into the individual bits values.
bool testAndClear(Enum e)
Test whether a bit is set, then clear it.
BitFlags & clear()
Clear all bits.
BitFlags operator&(BitFlags other) const
Create a new vector that's the intersection of two vectors.
bool operator>=(BitFlags other) const
Compare two vectors.
bool isEmpty() const
True if no bits are set.
BitFlags & set(BitFlags other)
Set all bits that are set in other.
bool isAnySet(BitFlags other) const
True if any of the specified bits are set.
BitFlags(Vector v)
Construct bit vector from value or bit.
BitFlags & operator=(Vector v)
Set the vector to an exact value.
bool isSet(Enum e) const
Test whether a bit is set.
BitFlags()
Default constructor with all bits clear.