Ardour  8.7-15-gadf511264b
flag_field.h
Go to the documentation of this file.
1 #ifndef AUDIOGRAPHER_FLAG_FIELD_H
2 #define AUDIOGRAPHER_FLAG_FIELD_H
3 
4 #include <stdint.h>
5 #include <iterator>
6 #include <climits>
7 
8 #include <boost/operators.hpp>
9 
11 
12 namespace AudioGrapher {
13 
18  : public boost::less_than_comparable<FlagField>
19  , boost::equivalent<FlagField>
20  , boost::equality_comparable<FlagField>
21 {
22  public:
23 
24  typedef uint8_t Flag;
25  typedef uint32_t storage_type;
26 
28  class iterator
29  : public boost::less_than_comparable<iterator>
30  , boost::equivalent<iterator>
31  , boost::equality_comparable<iterator>
32  {
33  public:
34  using iterator_category = std::bidirectional_iterator_tag;
35  using value_type = Flag;
36 
37  iterator (FlagField const & parent, Flag position) : parent (parent), position (position) {}
38  iterator (iterator const & other) : parent (other.parent), position (other.position) {}
39 
40  value_type operator*() const { return position; }
41  value_type const * operator->() const { return &position; }
42 
44  {
45  do {
46  ++position;
47  } while (!parent.has (position) && position != max());
48  return *this;
49  }
50  iterator operator++(int) { iterator copy (*this); ++(*this); return copy; }
51 
53  {
54  do {
55  --position;
56  } while (!parent.has (position) && position != max());
57  return *this;
58  }
59  iterator operator--(int) { iterator copy (*this); --(*this); return copy; }
60 
61  bool operator< (iterator const & other) const { return position < other.position; }
62 
63  private:
64  FlagField const & parent;
66  };
67 
68  public:
69 
70  FlagField() : _flags (0) {}
71  FlagField(FlagField const & other) : _flags (other._flags) {}
72 
73  inline bool has (Flag flag) const { return _flags & (1 << flag); }
74  inline storage_type flags () const { return _flags; }
75  inline operator bool() const { return _flags; }
76  inline void set (Flag flag) { _flags |= (1 << flag); }
77  inline void remove (Flag flag) { _flags &= ~(1 << flag); }
78  inline void reset () { _flags = 0; }
79 
81  inline FlagField unsupported_flags_of (FlagField const & other) const { return ~(_flags | ~other._flags); }
82 
84  inline FlagField & operator+= (FlagField const & other) { _flags |= other._flags; return *this; }
85 
90  inline bool operator< (FlagField const & other) const { return unsupported_flags_of (other); }
91 
92  iterator begin() const
93  {
94  iterator it (*this, 0);
95  if (!*this) { return end(); }
96  if (!has (0)) { ++it; }
97  return it;
98  }
99  iterator end() const { iterator it (*this, max()); return it; }
100 
101  private:
102  FlagField(storage_type flags) : _flags (flags) {}
103  static Flag max() { return CHAR_BIT * sizeof (storage_type) + 1; }
104 
106 };
107 
108 } // namespace
109 
110 #endif // AUDIOGRAPHER_FLAG_FIELD_H
Bi-directional iterator for flag set. Iterates over flags that are set in this field.
Definition: flag_field.h:32
iterator(iterator const &other)
Definition: flag_field.h:38
iterator(FlagField const &parent, Flag position)
Definition: flag_field.h:37
std::bidirectional_iterator_tag iterator_category
Definition: flag_field.h:34
value_type operator*() const
Definition: flag_field.h:40
value_type const * operator->() const
Definition: flag_field.h:41
void set(Flag flag)
Definition: flag_field.h:76
bool has(Flag flag) const
Definition: flag_field.h:73
FlagField(FlagField const &other)
Definition: flag_field.h:71
FlagField unsupported_flags_of(FlagField const &other) const
Returns the flags in other that are not set in this field.
Definition: flag_field.h:81
storage_type flags() const
Definition: flag_field.h:74
FlagField(storage_type flags)
Definition: flag_field.h:102
iterator end() const
Definition: flag_field.h:99
void remove(Flag flag)
Definition: flag_field.h:77
iterator begin() const
Definition: flag_field.h:92