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)) {}
96 : vector_(other.vector_) {}
105 return (vector_ & Vector(e)) != 0;
110 return (vector_ & other.vector_) == other.vector_;
115 return (vector_ & other.vector_) != 0;
135 vector_ |= Vector(e);
141 vector_ |= other.vector_;
147 vector_ &= ~Vector(e);
153 vector_ &= ~other.vector_;
165 bool retval =
isSet(e);
172 bool retval =
isSet(e);
185 vector_ = other.vector_;
193 return vector_ | other.vector_;
196 return vector_ | Vector(e);
204 return vector_ & other.vector_;
207 return vector_ & Vector(e);
221 return vector_ == other.vector_;
224 return vector_ != other.vector_;
227 return vector_ < other.vector_;
230 return vector_ <= other.vector_;
233 return vector_ > other.vector_;
236 return vector_ >= other.vector_;
245 std::vector<Enum>
split(std::vector<int64_t> constants, Vector &leftovers )
const {
246 leftovers = Vector(0);
247 std::vector<Enum> retval;
248 std::sort(constants.begin(), constants.end(), moreBits);
249 Vector tmp = vector_;
252 for (
size_t i=0; i<constants.size() && !found; ++i) {
253 if (Vector(tmp & constants[i]) == Vector(constants[i]) && constants[i] != 0) {
254 retval.push_back(Enum(constants[i]));
255 tmp &= ~constants[i];
269 std::vector<Enum> retval;
270 for (
size_t i = 0; i < 8*
sizeof(Enum); ++i) {
271 Enum e =
static_cast<Enum
>(uint64_t(1) << i);
278 #if __cplusplus >= 201103L
283 void each(std::vector<int64_t> constants,
const F &functor)
const {
285 for (Enum e:
split(constants, leftovers))
293 void each(
const F &functor)
const {
294 for (Enum e:
split())
300 static size_t nBits(Vector
vector) {
302 for (
size_t i = 0; i < 8*
sizeof(Vector); ++i) {
303 if ((vector & (Vector(1) << i)) != 0)
309 static bool moreBits(Vector a, Vector b) {
310 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.
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(const BitFlags &other)
Copy constructor.
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.