1 #ifndef AUDIOGRAPHER_FLAG_FIELD_H
2 #define AUDIOGRAPHER_FLAG_FIELD_H
8 #include <boost/operators.hpp>
10 namespace AudioGrapher {
12 /** Flag field capable of holding 32 flags.
13 * Easily grown in size to 64 flags by changing storage_type.
16 : public boost::less_than_comparable<FlagField>
17 , boost::equivalent<FlagField>
18 , boost::equality_comparable<FlagField>
23 typedef uint32_t storage_type;
25 /// Bi-directional iterator for flag set. Iterates over flags that are set in this field.
27 : public std::iterator<std::bidirectional_iterator_tag, Flag>
28 , public boost::less_than_comparable<iterator>
29 , boost::equivalent<iterator>
30 , boost::equality_comparable<iterator>
33 iterator (FlagField const & parent, Flag position) : parent (parent), position (position) {}
34 iterator (iterator const & other) : parent (other.parent), position (other.position) {}
36 value_type operator*() const { return position; }
37 value_type const * operator->() const { return &position; }
39 iterator & operator++()
43 } while (!parent.has (position) && position != max());
46 iterator operator++(int) { iterator copy (*this); ++(*this); return copy; }
48 iterator & operator--()
52 } while (!parent.has (position) && position != max());
55 iterator operator--(int) { iterator copy (*this); --(*this); return copy; }
57 bool operator< (iterator const & other) const { return position < other.position; }
60 FlagField const & parent;
66 FlagField() : _flags (0) {}
67 FlagField(FlagField const & other) : _flags (other._flags) {}
69 inline bool has (Flag flag) const { return _flags & (1 << flag); }
70 inline storage_type flags () const { return _flags; }
71 inline operator bool() const { return _flags; }
72 inline void set (Flag flag) { _flags |= (1 << flag); }
73 inline void remove (Flag flag) { _flags &= ~(1 << flag); }
74 inline void reset () { _flags = 0; }
76 /// Returns the flags in \a other that are not set in this field
77 inline FlagField unsupported_flags_of (FlagField const & other) const { return ~(_flags | ~other._flags); }
79 /// Set all flags that are set in \a other
80 inline FlagField & operator+= (FlagField const & other) { _flags |= other._flags; return *this; }
82 /** Checks whether this field has all the flags set that are set in \a other
83 * NOTE: Can NOT be used for strict weak ordering!
84 * \return \a true if \a other has flags set that this field does not
86 inline bool operator< (FlagField const & other) const { return unsupported_flags_of (other); }
88 iterator begin() const
90 iterator it (*this, 0);
91 if (!*this) { return end(); }
92 if (!has (0)) { ++it; }
95 iterator end() const { iterator it (*this, max()); return it; }
98 FlagField(storage_type flags) : _flags (flags) {}
99 static Flag max() { return CHAR_BIT * sizeof (storage_type) + 1; }
106 #endif // AUDIOGRAPHER_FLAG_FIELD_H