ROSE  0.9.10.135
BitFlags.h
1 #ifndef Rose_BitFlags_H
2 #define Rose_BitFlags_H
3 
4 #include <boost/format.hpp>
5 #include <Sawyer/Assert.h>
6 #include <vector>
7 
8 namespace Rose {
9 
55 template<typename E, typename V = unsigned long>
56 class BitFlags {
57 public:
58  typedef E Enum;
59  typedef V Vector;
60 
61 private:
62  Vector vector_;
63 
64 public:
67  : vector_(0) {}
68 
70  BitFlags(Vector v) /*implicit*/
71  : vector_(v) {}
72 
74  Vector vector() const {
75  return vector_;
76  }
77 
79  bool isSet(Enum e) const {
80  return (vector_ & Vector(e)) != 0;
81  }
82 
84  bool isClear(Enum e) const {
85  return !isSet(e);
86  }
87 
89  BitFlags& set(Enum e) {
90  vector_ |= Vector(e);
91  return *this;
92  }
93 
95  BitFlags& clear(Enum e) {
96  vector_ &= ~Vector(e);
97  return *this;
98  }
99 
101  bool testAndClear(Enum e) {
102  bool retval = isSet(e);
103  clear(e);
104  return retval;
105  }
106 
108  bool testAndSet(Enum e) {
109  bool retval = isSet(e);
110  set(e);
111  return retval;
112  }
113 
115  BitFlags& operator=(Vector v) {
116  vector_ = v;
117  return *this;
118  }
119 
122  vector_ = other.vector_;
123  return *this;
124  }
125 
129  bool operator==(BitFlags other) const {
130  return vector_ == other.vector_;
131  }
132  bool operator!=(BitFlags other) const {
133  return vector_ != other.vector_;
134  }
135  bool operator<(BitFlags other) const {
136  return vector_ < other.vector_;
137  }
138  bool operator<=(BitFlags other) const {
139  return vector_ <= other.vector_;
140  }
141  bool operator>(BitFlags other) const {
142  return vector_ > other.vector_;
143  }
144  bool operator>=(BitFlags other) const {
145  return vector_ >= other.vector_;
146  }
153  std::string toString(std::vector<long> constants, const char*(*stringifier)(long)) const {
154  std::string retval;
155  Vector tmp = vector_;
156  std::sort(constants.begin(), constants.end(), moreBits);
157  while (tmp) {
158  bool found = false;
159  for (size_t i=0; i<constants.size() && !found; ++i) {
160  if (Vector(tmp & constants[i]) == Vector(constants[i])) {
161  const char *name = stringifier(constants[i]);
162  ASSERT_not_null(name);
163  retval += std::string(retval.empty()?"":"|") + name;
164  tmp &= ~constants[i];
165  found = true;
166  }
167  }
168  if (!found) {
169  retval += retval.empty()?"":"|" + (boost::format("%lx") % (unsigned long)tmp).str();
170  tmp = 0;
171  break;
172  }
173  }
174 
175  if (!vector_) {
176  if (const char* name = stringifier(0)) {
177  retval = name;
178  } else {
179  retval = "0";
180  }
181  }
182 
183  return retval;
184  }
185 
186 private:
187  static size_t nBits(Vector vector) {
188  size_t retval = 0;
189  for (size_t i = 0; i < 8*sizeof(Vector); ++i) {
190  if ((vector & (Vector(1) << i)) != 0)
191  ++retval;
192  }
193  return retval;
194  }
195 
196  static bool moreBits(Vector a, Vector b) {
197  return nBits(a) > nBits(b);
198  }
199 };
200 
201 
202 } // namespace
203 
204 #endif
BitFlags & clear(Enum e)
Clear the specified bit.
Definition: BitFlags.h:95
bool operator==(BitFlags other) const
Compare two vectors.
Definition: BitFlags.h:129
bool testAndSet(Enum e)
Test whether a bit is set, then set it.
Definition: BitFlags.h:108
std::string toString(std::vector< long > constants, const char *(*stringifier)(long)) const
Convert to string.
Definition: BitFlags.h:153
bool operator>=(BitFlags other) const
Compare two vectors.
Definition: BitFlags.h:144
bool operator<=(BitFlags other) const
Compare two vectors.
Definition: BitFlags.h:138
bool operator>(BitFlags other) const
Compare two vectors.
Definition: BitFlags.h:141
Main namespace for the ROSE library.
Stores a vector of enum bit flags.
Definition: BitFlags.h:56
BitFlags(Vector v)
Construct bit vector from value or bit.
Definition: BitFlags.h:70
bool operator!=(BitFlags other) const
Compare two vectors.
Definition: BitFlags.h:132
bool testAndClear(Enum e)
Test whether a bit is set, then clear it.
Definition: BitFlags.h:101
BitFlags & set(Enum e)
Set the specified bit.
Definition: BitFlags.h:89
BitFlags & operator=(Vector v)
Set the vector to an exact value.
Definition: BitFlags.h:115
bool isClear(Enum e) const
Test whether a bit is clear.
Definition: BitFlags.h:84
BitFlags & operator=(BitFlags other)
Set the vector to the same as another.
Definition: BitFlags.h:121
BitFlags()
Default constructor with all bits clear.
Definition: BitFlags.h:66
bool isSet(Enum e) const
Test whether a bit is set.
Definition: BitFlags.h:79
Vector vector() const
Current value of the bit vector.
Definition: BitFlags.h:74
bool operator<(BitFlags other) const
Compare two vectors.
Definition: BitFlags.h:135