summaryrefslogtreecommitdiff
path: root/libs/audiographer/audiographer/flag_field.h
blob: 34ab5efdd6152e9d60a7ca354298be896698e852 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef AUDIOGRAPHER_FLAG_FIELD_H
#define AUDIOGRAPHER_FLAG_FIELD_H

#include <stdint.h>
#include <iterator>
#include <climits>

#include <boost/operators.hpp>

#include "audiographer/visibility.h"

namespace AudioGrapher {

/** Flag field capable of holding 32 flags.
  * Easily grown in size to 64 flags by changing storage_type.
  */
class LIBAUDIOGRAPHER_API FlagField
  : public boost::less_than_comparable<FlagField>
  , boost::equivalent<FlagField>
  , boost::equality_comparable<FlagField>
{
  public:
	
	typedef uint8_t  Flag;
	typedef uint32_t storage_type;
	
	/// Bi-directional iterator for flag set. Iterates over flags that are set in this field.
	class iterator
	  : public std::iterator<std::bidirectional_iterator_tag, Flag>
	  , public boost::less_than_comparable<iterator>
	  , boost::equivalent<iterator>
	  , boost::equality_comparable<iterator>
	{
	  public:
		iterator (FlagField const & parent, Flag position) : parent (parent), position (position) {}
		iterator (iterator const & other) : parent (other.parent), position (other.position) {}
		
		value_type operator*() const { return position; }
		value_type const * operator->() const { return &position; }
		
		iterator & operator++()
		{
			do {
				++position;
			} while (!parent.has (position) && position != max());
			return *this;
		}
		iterator operator++(int) { iterator copy (*this); ++(*this); return copy; }
		
		iterator & operator--()
		{
			do {
				--position;
			} while (!parent.has (position) && position != max());
			return *this;
		}
		iterator   operator--(int) { iterator copy (*this); --(*this); return copy; }
		
		bool operator< (iterator const & other) const { return position < other.position; }
		
	  private:
		FlagField const & parent;
		Flag              position;
	};
	
  public:
	
	FlagField() : _flags (0) {}
	FlagField(FlagField const & other) : _flags (other._flags) {}
	
	inline bool has (Flag flag)    const { return _flags & (1 << flag); }
	inline storage_type flags ()   const { return _flags; }
	inline operator bool()         const { return _flags; }
	inline void set (Flag flag)          { _flags |= (1 << flag); }
	inline void remove (Flag flag)       { _flags &= ~(1 << flag); }
	inline void reset ()                 { _flags = 0; }

	/// Returns the flags in \a other that are not set in this field
	inline FlagField unsupported_flags_of (FlagField const & other) const { return ~(_flags | ~other._flags); }
	
	/// Set all flags that are set in \a other
	inline FlagField & operator+= (FlagField const & other) { _flags |= other._flags; return *this; }
	
	/** Checks whether this field has all the flags set that are set in \a other
	  * NOTE: Can NOT be used for strict weak ordering!
	  * \return \a true if \a other has flags set that this field does not
	  */
	inline bool operator< (FlagField const & other) const { return unsupported_flags_of (other); }

	iterator begin() const
	{
		iterator it (*this, 0);
		if (!*this) { return end(); }
		if (!has (0)) { ++it; }
		return it;
	}
	iterator end() const { iterator it (*this, max()); return it; }

  private:
	FlagField(storage_type flags) : _flags (flags) {}
	static Flag max() { return CHAR_BIT * sizeof (storage_type) + 1; }
	
	storage_type _flags;
};

} // namespace

#endif // AUDIOGRAPHER_FLAG_FIELD_H