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];
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 {
290 for (Enum e:
split())
296 static size_t nBits(Vector
vector) {
298 for (
size_t i = 0; i < 8*
sizeof(Vector); ++i) {
299 if ((vector & (Vector(1) << i)) != 0)
305 static bool moreBits(Vector a, Vector b) {
306 return nBits(a) > nBits(b);
BitFlags & clear()
Clear all bits.
bool operator>=(BitFlags other) const
Compare two vectors.
BitFlags & set(Enum e)
Set the specified bit.
bool operator<(BitFlags other) const
Compare two vectors.
bool testAndSet(Enum e)
Test whether a bit is set, then set it.
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.
BitFlags & operator=(BitFlags other)
Set the vector to the same as another.
std::vector< Enum > split() const
Split a vector into the individual bits values.
bool isSet(Enum e) const
Test whether a bit is set.
bool isEmpty() const
True if no bits are set.
bool operator!=(BitFlags other) const
Compare two vectors.
Stores a vector of enum bit flags.
Vector vector() const
Current value of the bit vector.
bool isAnySet() const
True if any bit is set.
bool operator<=(BitFlags other) const
Compare two vectors.
BitFlags operator|(Enum e) const
Create a new vector that's the union of two vectors.
Name space for the entire library.
BitFlags operator&(Enum e) const
Create a new vector that's the intersection of two vectors.
BitFlags operator|(BitFlags other) const
Create a new vector that's the union of two vectors.
BitFlags & clear(Enum e)
Clear the specified bit.
bool operator>(BitFlags other) const
Compare two vectors.
bool operator==(BitFlags other) const
Compare two vectors.
BitFlags & set(BitFlags other)
Set all bits that are set in other.
bool isClear(Enum e) const
Test whether a bit is clear.
BitFlags & operator=(Vector v)
Set the vector to an exact value.
bool testAndClear(Enum e)
Test whether a bit is set, then clear it.
BitFlags()
Default constructor with all bits clear.
BitFlags(Vector v)
Construct bit vector from value or bit.
bool isAllSet(BitFlags other) const
True if all specified bits are set.
BitFlags & clear(BitFlags other)
Clear all bits that are set in other.
bool isAnySet(BitFlags other) const
True if any of the specified bits are set.
BitFlags operator&(BitFlags other) const
Create a new vector that's the intersection of two vectors.
BitFlags intersection(Enum e) const
Create a new vector that's the intersection of two vectors.
Stores a vector of enum bit flags.