8 #ifndef Sawyer_BitFlags_H
9 #define Sawyer_BitFlags_H
11 #include <Sawyer/Sawyer.h>
13 #include <boost/foreach.hpp>
14 #include <boost/serialization/access.hpp>
15 #include <Sawyer/Assert.h>
65 template<
typename E,
typename V =
int64_t>
75 friend class boost::serialization::access;
78 void serialize(S &s,
const unsigned ) {
79 s & BOOST_SERIALIZATION_NVP(vector_);
92 : vector_(Vector(e)) {}
101 return (vector_ & Vector(e)) != 0;
106 return (vector_ & other.vector_) == other.vector_;
111 return (vector_ & other.vector_) != 0;
131 vector_ |= Vector(e);
137 vector_ |= other.vector_;
143 vector_ &= ~Vector(e);
149 vector_ &= ~other.vector_;
161 bool retval =
isSet(e);
168 bool retval =
isSet(e);
181 vector_ = other.vector_;
189 return vector_ | other.vector_;
192 return vector_ | Vector(e);
200 return vector_ & other.vector_;
203 return vector_ & Vector(e);
217 return vector_ == other.vector_;
220 return vector_ != other.vector_;
223 return vector_ < other.vector_;
226 return vector_ <= other.vector_;
229 return vector_ > other.vector_;
232 return vector_ >= other.vector_;
241 std::vector<Enum>
split(std::vector<int64_t> constants, Vector &leftovers )
const {
242 leftovers = Vector(0);
243 std::vector<Enum> retval;
244 std::sort(constants.begin(), constants.end(), moreBits);
245 Vector tmp = vector_;
248 for (
size_t i=0; i<constants.size() && !found; ++i) {
249 if (Vector(tmp & constants[i]) == Vector(constants[i]) && constants[i] != 0) {
250 retval.push_back(Enum(constants[i]));
251 tmp &= ~constants[i];
264 std::vector<Enum>
split(Vector &leftovers )
const {
265 std::vector<Enum> retval;
266 for (
size_t i = 0; i < 8*
sizeof(Enum); ++i) {
267 Enum e =
static_cast<Enum
>(uint64_t(1) << i);
274 #if __cplusplus >= 201103L
279 void each(std::vector<int64_t> constants,
const F &functor)
const {
281 for (Enum e:
split(constants, leftovers))
289 void each(
const F &functor)
const {
291 for (Enum e:
split(leftovers))
297 static size_t nBits(Vector
vector) {
299 for (
size_t i = 0; i < 8*
sizeof(Vector); ++i) {
300 if ((
vector & (Vector(1) << i)) != 0)
306 static bool moreBits(Vector a, Vector b) {
307 return nBits(a) > nBits(b);
bool isAnySet(BitFlags other) const
True if any of the specified bits are set.
BitFlags & set(Enum e)
Set the specified bit.
BitFlags & operator=(Vector v)
Set the vector to an exact value.
bool testAndSet(Enum e)
Test whether a bit is set, then set it.
BitFlags operator&(BitFlags other) const
Create a new vector that's the intersection of two vectors.
bool operator==(BitFlags other) const
Compare two vectors.
BitFlags & clear(Enum e)
Clear the specified bit.
BitFlags intersection(Enum e) const
Create a new vector that's the intersection of two vectors.
BitFlags()
Default constructor with all bits clear.
bool operator<=(BitFlags other) const
Compare two vectors.
Vector vector() const
Current value of the bit vector.
bool isAllSet(BitFlags other) const
True if all specified bits are set.
BitFlags intersection(BitFlags other) const
Create a new vector that's the intersection of two vectors.
std::vector< Enum > split(Vector &leftovers) const
Split a vector into the individual bits values.
BitFlags operator|(Enum e) const
Create a new vector that's the union of two vectors.
BitFlags operator|(BitFlags other) const
Create a new vector that's the union of two vectors.
BitFlags operator&(Enum e) const
Create a new vector that's the intersection of two vectors.
BitFlags & clear(BitFlags other)
Clear all bits that are set in other.
BitFlags & operator=(BitFlags other)
Set the vector to the same as another.
bool operator!=(BitFlags other) const
Compare two vectors.
bool isAnySet() const
True if any bit is set.
BitFlags(Vector v)
Construct bit vector from value or bit.
bool isSet(Enum e) const
Test whether a bit is set.
BitFlags & set(BitFlags other)
Set all bits that are set in other.
bool testAndClear(Enum e)
Test whether a bit is set, then clear it.
Stores a vector of enum bit flags.
Name space for the entire library.
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.
bool operator>=(BitFlags other) const
Compare two vectors.
bool isClear(Enum e) const
Test whether a bit is clear.
bool isEmpty() const
True if no bits are set.
bool operator>(BitFlags other) const
Compare two vectors.
BitFlags & clear()
Clear all bits.