attempt to be compliant with gcc 4.6 (assuming that its more compliant with standards...
[ardour.git] / libs / audiographer / audiographer / flag_field.h
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
10 namespace AudioGrapher {
11
12 /** Flag field capable of holding 32 flags.
13   * Easily grown in size to 64 flags by changing storage_type.
14   */
15 class FlagField
16   : public boost::less_than_comparable<FlagField>
17   , boost::equivalent<FlagField>
18   , boost::equality_comparable<FlagField>
19 {
20   public:
21         
22         typedef uint8_t  Flag;
23         typedef uint32_t storage_type;
24         
25         /// Bi-directional iterator for flag set. Iterates over flags that are set in this field.
26         class iterator
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>
31         {
32           public:
33                 iterator (FlagField const & parent, Flag position) : parent (parent), position (position) {}
34                 iterator (iterator const & other) : parent (other.parent), position (other.position) {}
35                 
36                 value_type operator*() const { return position; }
37                 value_type const * operator->() const { return &position; }
38                 
39                 iterator & operator++()
40                 {
41                         do {
42                                 ++position;
43                         } while (!parent.has (position) && position != max());
44                         return *this;
45                 }
46                 iterator operator++(int) { iterator copy (*this); ++(*this); return copy; }
47                 
48                 iterator & operator--()
49                 {
50                         do {
51                                 --position;
52                         } while (!parent.has (position) && position != max());
53                         return *this;
54                 }
55                 iterator   operator--(int) { iterator copy (*this); --(*this); return copy; }
56                 
57                 bool operator< (iterator const & other) const { return position < other.position; }
58                 
59           private:
60                 FlagField const & parent;
61                 Flag              position;
62         };
63         
64   public:
65         
66         FlagField() : _flags (0) {}
67         FlagField(FlagField const & other) : _flags (other._flags) {}
68         
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; }
75
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); }
78         
79         /// Set all flags that are set in \a other
80         inline FlagField & operator+= (FlagField const & other) { _flags |= other._flags; return *this; }
81         
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
85           */
86         inline bool operator< (FlagField const & other) const { return unsupported_flags_of (other); }
87
88         iterator begin() const
89         {
90                 iterator it (*this, 0);
91                 if (!*this) { return end(); }
92                 if (!has (0)) { ++it; }
93                 return it;
94         }
95         iterator end() const { iterator it (*this, max()); return it; }
96
97   private:
98         FlagField(storage_type flags) : _flags (flags) {}
99         static Flag max() { return CHAR_BIT * sizeof (storage_type) + 1; }
100         
101         storage_type _flags;
102 };
103
104 } // namespace
105
106 #endif // AUDIOGRAPHER_FLAG_FIELD_H