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