HART  0.2.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
hart_channel_flags.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <bitset>
4#include <vector>
5
7
8namespace hart
9{
10
11/// @brief A set of boolean flags mapped to each audio channel
12/// @ingroup DataStructures
14{
15private:
16 static constexpr size_t m_maxChannels = 64; // Can be expanded if you need more
17
18public:
19 /// @brief Creates a new channel flags object
20 /// @param defaultValues Initial value for all flags
21 /// @param numChannels Size of the contailer
22 ChannelFlags (bool defaultValues = true, size_t numChannels = m_maxChannels)
23 {
24 if (numChannels > m_maxChannels)
25 HART_THROW_OR_RETURN_VOID (hart::SizeError, "Number of channels exceeds maximum possible amount");
26
27 m_numChannels = m_maxChannels;
28 setAllTo (defaultValues);
29 }
30
31 /// @brief Sets all flags to a new value
32 /// @param newValues New values for all flags
33 void setAllTo (bool newValues)
34 {
35 if (newValues == true)
36 m_flags.set();
37 else
38 m_flags.reset();
39 }
40
41 /// @brief Returns the size (not capacity) of the container
42 /// @details This size is guaranteed to be equal to the number
43 /// of channels in whatever it's assotiated with, or more.
44 size_t size() const noexcept
45 {
46 return m_numChannels;
47 }
48
49 /// @brief Resizes the container
50 /// @details Does not change the capacity - it's fixed.
51 /// Does not change the flags values.
52 void resize (size_t newNumChannels)
53 {
54 if (newNumChannels > m_maxChannels)
55 HART_THROW_OR_RETURN_VOID (hart::SizeError, "Number of channels exceeds maximum possible amount");
56
57 m_numChannels = newNumChannels;
58 }
59
60 /// @brief Access the flag value for a specific channel
61 /// @param channel Number of channel (0-based)
62 /// @return A reference to underlying storage for the corresponding flag
63 std::bitset<m_maxChannels>::reference operator[] (size_t channel) {
64 if (channel >= m_numChannels)
65 HART_THROW_OR_RETURN (hart::SizeError, "ChannelFlags index is out of range", {});
66
67 return m_flags[channel];
68 }
69
70 /// @brief Access the flag value for a specific channel
71 /// @param channel Number of channel (0-based)
72 /// @return Value of a corresponding flag
73 bool operator[] (size_t channel) const {
74 if (channel >= m_numChannels)
75 HART_THROW_OR_RETURN (hart::SizeError, "ChannelFlags index is out of range", false);
76
77 return m_flags.test (channel);
78 }
79
80 /// @brief Checks if all flags are set to `true`
81 /// @return `true` if all flags for all channels are `true`, `false` otherwise
82 bool allTrue() const noexcept
83 {
84 // TODO: Can it be O(1)?
85
86 for (size_t i = 0; i < m_maxChannels; ++i)
87 if (m_flags.test (i) == false)
88 return false;
89
90 return true;
91 }
92
93 /// @brief Checks how many channels are marked with `true`
94 /// @return Amount of flags set to `true`
95 size_t numTrue()
96 {
97 size_t res = 0;
98
99 for (size_t channel = 0; channel < m_maxChannels; ++channel)
100 res += m_flags.test (channel);
101
102 return res;
103 }
104
105 /// @brief Checks if any of the flags is set to `true`
106 /// @return `true` if flag for at least one channel is `true`, `false` otherwise
107 bool anyTrue() const noexcept
108 {
109 // TODO: Can it be O(1)?
110
111 for (size_t i = 0; i < m_maxChannels; ++i)
112 if (m_flags.test (i) == true)
113 return true;
114
115 return false;
116 }
117
118 /// @brief Makes text representation of itself as a initializer list of active channels
119 /// @param[out] stream Output stream to write to
120 void representAsInitializerList (std::ostream& stream) const
121 {
122 stream << '{';
123 bool notFirst = false;
124
125 for (size_t i = 0; i < m_numChannels; ++i)
126 {
127 if (m_flags.test (i) == false)
128 continue;
129
130 if (notFirst)
131 stream << ", ";
132
133 stream << i;
134 notFirst = true;
135 }
136
137 stream << '}';
138 }
139
140private:
141 size_t m_numChannels = m_maxChannels;
142 std::bitset<m_maxChannels> m_flags;
143};
144
145} // namespace hart
A set of boolean flags mapped to each audio channel.
bool allTrue() const noexcept
Checks if all flags are set to true
bool operator[](size_t channel) const
Access the flag value for a specific channel.
std::bitset< m_maxChannels >::reference operator[](size_t channel)
Access the flag value for a specific channel.
size_t size() const noexcept
Returns the size (not capacity) of the container.
ChannelFlags(bool defaultValues=true, size_t numChannels=m_maxChannels)
Creates a new channel flags object.
void resize(size_t newNumChannels)
Resizes the container.
bool anyTrue() const noexcept
Checks if any of the flags is set to true
void setAllTo(bool newValues)
Sets all flags to a new value.
void representAsInitializerList(std::ostream &stream) const
Makes text representation of itself as a initializer list of active channels.
size_t numTrue()
Checks how many channels are marked with true
Thrown when an unexpected container size is encountered.
#define HART_THROW_OR_RETURN_VOID(ExceptionType, message)
Throws an exception if HART_DO_NOT_THROW_EXCEPTIONS is set, prints a message and returns otherwise.
#define HART_THROW_OR_RETURN(ExceptionType, message, returnValue)
Throws an exception if HART_DO_NOT_THROW_EXCEPTIONS is set, prints a message and returns a specified ...