MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TokenUnitManager.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::Buffers {
7
8using RootAudioProcessingFunction = std::function<void(std::vector<std::shared_ptr<RootAudioBuffer>>&, uint32_t)>;
9using RootGraphicsProcessingFunction = std::function<void(std::shared_ptr<RootGraphicsBuffer>&, uint32_t)>;
10
11/**
12 * @brief Represents a root audio unit containing buffers and processing chains for multiple channels
13 */
15 std::vector<std::shared_ptr<RootAudioBuffer>> root_buffers;
16 std::vector<std::shared_ptr<BufferProcessingChain>> processing_chains;
18 uint32_t channel_count = 0;
19 uint32_t buffer_size = 512;
20
21 [[nodiscard]] std::shared_ptr<RootAudioBuffer> get_buffer(uint32_t channel) const
22 {
23 return root_buffers[channel];
24 }
25
26 [[nodiscard]] std::shared_ptr<BufferProcessingChain> get_chain(uint32_t channel) const
27 {
29 }
30
31 void resize_channels(uint32_t new_count, uint32_t new_buffer_size, ProcessingToken token);
32
33 void resize_buffers(uint32_t new_buffer_size);
34};
35
36/**
37 * @brief Represents a root graphics unit containing a buffer and processing chain
38 */
40 std::shared_ptr<RootGraphicsBuffer> root_buffer;
41 std::shared_ptr<BufferProcessingChain> processing_chain;
42 uint32_t frame_count = 0; // For tracking processed frames
44
46
47 void initialize(ProcessingToken token);
48
49 [[nodiscard]] std::shared_ptr<RootGraphicsBuffer> get_buffer() const { return root_buffer; }
50
51 [[nodiscard]] std::shared_ptr<BufferProcessingChain> get_chain() const { return processing_chain; }
52};
53
54/**
55 * @class TokenUnitManager
56 * @brief Token-scoped unit storage and lifecycle management
57 *
58 * Manages the core data structures for both audio and graphics units, providing
59 * thread-safe, token-aware access patterns. This class is the single source of truth
60 * for unit lifecycle and storage.
61 *
62 * Design Principles:
63 * - Token-generic: Doesn't distinguish between audio/graphics at the data level
64 * - Thread-safe: All modifications guarded by mutex
65 * - Lazy creation: Units created on-demand via get_or_create patterns
66 * - Immutable queries: Const queries never create units
67 */
68class MAYAFLUX_API TokenUnitManager {
69public:
70 /**
71 * @brief Creates a new unit manager with initial audio unit configuration
72 * @param default_token Primary processing token (typically AUDIO_BACKEND)
73 * @param default_out_channels Output channels for default token
74 * @param default_buffer_size Buffer size for default token
75 */
77 ProcessingToken default_token,
78 uint32_t default_out_channels,
79 uint32_t default_buffer_size);
80
82 ProcessingToken default_audio_token,
83 ProcessingToken default_graphics_token,
84 uint32_t preferred_buffer_size);
85
86 ~TokenUnitManager() = default;
87
88 // =========================================================================
89 // Audio Unit Management
90 // =========================================================================
91
92 /**
93 * @brief Gets or creates a root audio unit for the specified token
94 * @param token Processing domain
95 * @return Reference to the root audio unit
96 *
97 * If the unit doesn't exist, creates it and returns it.
98 * Thread-safe creation via lock guard.
99 */
100 RootAudioUnit& get_or_create_audio_unit(ProcessingToken token);
101
102 /**
103 * @brief Gets an existing audio unit without creating if missing
104 * @param token Processing domain
105 * @return Const reference to the audio unit
106 * @throws std::out_of_range if token not found
107 *
108 * This is a query-only operation; will not create a unit if missing.
109 */
110 const RootAudioUnit& get_audio_unit(ProcessingToken token) const;
111
112 /**
113 * @brief Gets an existing audio unit without creating if missing (mutable)
114 * @param token Processing domain
115 * @return Reference to the audio unit
116 * @throws std::out_of_range if token not found
117 *
118 * This is a query-only operation; will not create a unit if missing.
119 * Mutable version for operations that need to modify the unit in-place.
120 */
121 RootAudioUnit& get_audio_unit_mutable(ProcessingToken token);
122
123 /**
124 * @brief Ensures a root audio unit exists for a specific token and channel
125 * @param token Processing domain
126 * @param channel Channel index
127 * @return Reference to the root audio unit for that token/channel
128 */
129 RootAudioUnit& ensure_and_get_audio_unit(ProcessingToken token, uint32_t channel);
130
131 /**
132 * @brief Checks if an audio unit exists for the given token
133 * @param token Processing domain
134 * @return True if the unit exists, false otherwise
135 */
136 bool has_audio_unit(ProcessingToken token) const;
137
138 /**
139 * @brief Gets all active audio processing tokens
140 * @return Vector of tokens that have audio units
141 */
142 std::vector<ProcessingToken> get_active_audio_tokens() const;
143
144 // =========================================================================
145 // Graphics Unit Management
146 // =========================================================================
147
148 /**
149 * @brief Gets or creates a root graphics unit for the specified token
150 * @param token Processing domain
151 * @return Reference to the root graphics unit
152 *
153 * If the unit doesn't exist, creates it and initializes it.
154 * Thread-safe creation via lock guard.
155 */
156 RootGraphicsUnit& get_or_create_graphics_unit(ProcessingToken token);
157
158 /**
159 * @brief Gets an existing graphics unit without creating if missing
160 * @param token Processing domain
161 * @return Const reference to the graphics unit
162 * @throws std::out_of_range if token not found
163 *
164 * This is a query-only operation; will not create a unit if missing.
165 */
166 const RootGraphicsUnit& get_graphics_unit(ProcessingToken token) const;
167
168 /**
169 * @brief Gets an existing graphics unit without creating if missing (mutable)
170 * @param token Processing domain
171 * @return Reference to the graphics unit
172 * @throws std::out_of_range if token not found
173 *
174 * This is a query-only operation; will not create a unit if missing.
175 * Mutable version for operations that need to modify the unit in-place.
176 */
177 RootGraphicsUnit& get_graphics_unit_mutable(ProcessingToken token);
178
179 /**
180 * @brief Checks if a graphics unit exists for the given token
181 * @param token Processing domain
182 * @return True if the unit exists, false otherwise
183 */
184 bool has_graphics_unit(ProcessingToken token) const;
185
186 /**
187 * @brief Gets all active graphics processing tokens
188 * @return Vector of tokens that have graphics units
189 */
190 std::vector<ProcessingToken> get_active_graphics_tokens() const;
191
192 // =========================================================================
193 // Audio Unit Operations
194 // =========================================================================
195
196 /**
197 * @brief Ensures an audio unit exists and resizes it to the specified channel count
198 * @param token Processing domain
199 * @param channel_count Minimum channel count to ensure
200 */
201 void ensure_audio_channels(ProcessingToken token, uint32_t channel_count);
202
203 /**
204 * @brief Resizes all buffers in an audio unit to the specified size
205 * @param token Processing domain
206 * @param buffer_size New buffer size
207 */
208 void resize_audio_buffers(ProcessingToken token, uint32_t buffer_size);
209
210 /**
211 * @brief Gets the number of channels in an audio unit
212 * @param token Processing domain
213 * @return Number of channels, or 0 if unit doesn't exist
214 */
215 uint32_t get_audio_channel_count(ProcessingToken token) const;
216
217 /**
218 * @brief Gets the buffer size for an audio unit
219 * @param token Processing domain
220 * @return Buffer size, or 512 if unit doesn't exist
221 */
222 uint32_t get_audio_buffer_size(ProcessingToken token) const;
223
224 /**
225 * @brief Validates the number of channels and resizes buffers if necessary
226 * @param token Processing domain
227 * @param num_channels Number of channels to validate
228 * @param buffer_size New buffer size to set
229 *
230 * This method ensures that the specified number of channels exists for the given token,
231 * resizing the root audio buffers accordingly.
232 */
233 inline void validate_num_audio_channels(ProcessingToken token, uint32_t num_channels, uint32_t buffer_size)
234 {
235 ensure_audio_channels(token, num_channels);
236 resize_audio_buffers(token, buffer_size);
237 }
238
239 // =========================================================================
240 // Configuration Access
241 // =========================================================================
242
243 /**
244 * @brief Gets the default processing token
245 * @return The token configured at construction
246 */
247 inline ProcessingToken get_default_audio_token() const { return m_default_audio_token; }
248
249 /**
250 * @brief Gets the default graphics processing token
251 * @return The graphics token configured at construction
252 */
253 inline ProcessingToken get_default_graphics_token() const { return m_default_graphics_token; }
254
255 // =========================================================================
256 // Thread Safety Access
257 // =========================================================================
258
259 /**
260 * @brief Acquires the manager's mutex for external synchronization
261 * @return Reference to the internal mutex
262 *
263 * Use this when you need to perform multiple atomic operations across
264 * the unit store. Example:
265 * @code
266 * std::lock_guard<std::mutex> lock(manager.get_mutex());
267 * auto& unit = manager.get_audio_unit_mutable(token);
268 * // Multiple operations on unit are now atomic
269 * @endcode
270 */
271 inline std::mutex& get_mutex() const { return m_manager_mutex; }
272
273private:
274 // =========================================================================
275 // Data Members
276 // =========================================================================
277
278 /// Default processing token for legacy compatibility and initialization
280
281 /// Default graphics processing token
283
284 /// Token-based map of root audio buffer units
285 /// Maps: ProcessingToken -> channel -> {root_buffers, processing chains}
286 std::unordered_map<ProcessingToken, RootAudioUnit> m_audio_units;
287
288 /// Token-based map of root graphics buffer units
289 std::unordered_map<ProcessingToken, RootGraphicsUnit> m_graphics_units;
290
291 /// Mutex for thread-safe access to all unit maps
292 mutable std::mutex m_manager_mutex;
293
295};
296
297}
uint32_t channel
ProcessingToken get_default_graphics_token() const
Gets the default graphics processing token.
std::mutex m_manager_mutex
Mutex for thread-safe access to all unit maps.
std::mutex & get_mutex() const
Acquires the manager's mutex for external synchronization.
std::unordered_map< ProcessingToken, RootGraphicsUnit > m_graphics_units
Token-based map of root graphics buffer units.
TokenUnitManager(ProcessingToken default_token, uint32_t default_out_channels, uint32_t default_buffer_size)
Creates a new unit manager with initial audio unit configuration.
std::unordered_map< ProcessingToken, RootAudioUnit > m_audio_units
Token-based map of root audio buffer units Maps: ProcessingToken -> channel -> {root_buffers,...
ProcessingToken m_default_audio_token
Default processing token for legacy compatibility and initialization.
ProcessingToken get_default_audio_token() const
Gets the default processing token.
void validate_num_audio_channels(ProcessingToken token, uint32_t num_channels, uint32_t buffer_size)
Validates the number of channels and resizes buffers if necessary.
ProcessingToken m_default_graphics_token
Default graphics processing token.
Token-scoped unit storage and lifecycle management.
void initialize()
Definition main.cpp:11
ProcessingToken
Bitfield enum defining processing characteristics and backend requirements for buffer operations.
std::function< void(std::shared_ptr< RootGraphicsBuffer > &, uint32_t)> RootGraphicsProcessingFunction
std::function< void(std::vector< std::shared_ptr< RootAudioBuffer > > &, uint32_t)> RootAudioProcessingFunction
std::vector< std::shared_ptr< BufferProcessingChain > > processing_chains
std::vector< std::shared_ptr< RootAudioBuffer > > root_buffers
void resize_buffers(uint32_t new_buffer_size)
RootAudioProcessingFunction custom_processor
std::shared_ptr< RootAudioBuffer > get_buffer(uint32_t channel) const
void resize_channels(uint32_t new_count, uint32_t new_buffer_size, ProcessingToken token)
std::shared_ptr< BufferProcessingChain > get_chain(uint32_t channel) const
Represents a root audio unit containing buffers and processing chains for multiple channels.
std::shared_ptr< RootGraphicsBuffer > root_buffer
std::shared_ptr< RootGraphicsBuffer > get_buffer() const
std::shared_ptr< BufferProcessingChain > processing_chain
std::shared_ptr< BufferProcessingChain > get_chain() const
RootGraphicsProcessingFunction custom_processor
Represents a root graphics unit containing a buffer and processing chain.