MayaFlux 0.1.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 {
28 return processing_chains[channel];
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
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 = ProcessingToken::AUDIO_BACKEND,
83 ProcessingToken default_graphics_token = ProcessingToken::GRAPHICS_BACKEND);
84
85 ~TokenUnitManager() = default;
86
87 // =========================================================================
88 // Audio Unit Management
89 // =========================================================================
90
91 /**
92 * @brief Gets or creates a root audio unit for the specified token
93 * @param token Processing domain
94 * @return Reference to the root audio unit
95 *
96 * If the unit doesn't exist, creates it and returns it.
97 * Thread-safe creation via lock guard.
98 */
99 RootAudioUnit& get_or_create_audio_unit(ProcessingToken token);
100
101 /**
102 * @brief Gets an existing audio unit without creating if missing
103 * @param token Processing domain
104 * @return Const reference to the audio unit
105 * @throws std::out_of_range if token not found
106 *
107 * This is a query-only operation; will not create a unit if missing.
108 */
109 const RootAudioUnit& get_audio_unit(ProcessingToken token) const;
110
111 /**
112 * @brief Gets an existing audio unit without creating if missing (mutable)
113 * @param token Processing domain
114 * @return Reference to the audio unit
115 * @throws std::out_of_range if token not found
116 *
117 * This is a query-only operation; will not create a unit if missing.
118 * Mutable version for operations that need to modify the unit in-place.
119 */
120 RootAudioUnit& get_audio_unit_mutable(ProcessingToken token);
121
122 /**
123 * @brief Ensures a root audio unit exists for a specific token and channel
124 * @param token Processing domain
125 * @param channel Channel index
126 * @return Reference to the root audio unit for that token/channel
127 */
128 RootAudioUnit& ensure_and_get_audio_unit(ProcessingToken token, uint32_t channel);
129
130 /**
131 * @brief Checks if an audio unit exists for the given token
132 * @param token Processing domain
133 * @return True if the unit exists, false otherwise
134 */
135 bool has_audio_unit(ProcessingToken token) const;
136
137 /**
138 * @brief Gets all active audio processing tokens
139 * @return Vector of tokens that have audio units
140 */
141 std::vector<ProcessingToken> get_active_audio_tokens() const;
142
143 // =========================================================================
144 // Graphics Unit Management
145 // =========================================================================
146
147 /**
148 * @brief Gets or creates a root graphics unit for the specified token
149 * @param token Processing domain
150 * @return Reference to the root graphics unit
151 *
152 * If the unit doesn't exist, creates it and initializes it.
153 * Thread-safe creation via lock guard.
154 */
155 RootGraphicsUnit& get_or_create_graphics_unit(ProcessingToken token);
156
157 /**
158 * @brief Gets an existing graphics unit without creating if missing
159 * @param token Processing domain
160 * @return Const reference to the graphics unit
161 * @throws std::out_of_range if token not found
162 *
163 * This is a query-only operation; will not create a unit if missing.
164 */
165 const RootGraphicsUnit& get_graphics_unit(ProcessingToken token) const;
166
167 /**
168 * @brief Gets an existing graphics unit without creating if missing (mutable)
169 * @param token Processing domain
170 * @return Reference to the graphics unit
171 * @throws std::out_of_range if token not found
172 *
173 * This is a query-only operation; will not create a unit if missing.
174 * Mutable version for operations that need to modify the unit in-place.
175 */
176 RootGraphicsUnit& get_graphics_unit_mutable(ProcessingToken token);
177
178 /**
179 * @brief Checks if a graphics unit exists for the given token
180 * @param token Processing domain
181 * @return True if the unit exists, false otherwise
182 */
183 bool has_graphics_unit(ProcessingToken token) const;
184
185 /**
186 * @brief Gets all active graphics processing tokens
187 * @return Vector of tokens that have graphics units
188 */
189 std::vector<ProcessingToken> get_active_graphics_tokens() const;
190
191 // =========================================================================
192 // Audio Unit Operations
193 // =========================================================================
194
195 /**
196 * @brief Ensures an audio unit exists and resizes it to the specified channel count
197 * @param token Processing domain
198 * @param channel_count Minimum channel count to ensure
199 */
200 void ensure_audio_channels(ProcessingToken token, uint32_t channel_count);
201
202 /**
203 * @brief Resizes all buffers in an audio unit to the specified size
204 * @param token Processing domain
205 * @param buffer_size New buffer size
206 */
207 void resize_audio_buffers(ProcessingToken token, uint32_t buffer_size);
208
209 /**
210 * @brief Gets the number of channels in an audio unit
211 * @param token Processing domain
212 * @return Number of channels, or 0 if unit doesn't exist
213 */
214 uint32_t get_audio_channel_count(ProcessingToken token) const;
215
216 /**
217 * @brief Gets the buffer size for an audio unit
218 * @param token Processing domain
219 * @return Buffer size, or 512 if unit doesn't exist
220 */
221 uint32_t get_audio_buffer_size(ProcessingToken token) const;
222
223 /**
224 * @brief Validates the number of channels and resizes buffers if necessary
225 * @param token Processing domain
226 * @param num_channels Number of channels to validate
227 * @param buffer_size New buffer size to set
228 *
229 * This method ensures that the specified number of channels exists for the given token,
230 * resizing the root audio buffers accordingly.
231 */
232 inline void validate_num_audio_channels(ProcessingToken token, uint32_t num_channels, uint32_t buffer_size)
233 {
234 ensure_audio_channels(token, num_channels);
235 resize_audio_buffers(token, buffer_size);
236 }
237
238 // =========================================================================
239 // Configuration Access
240 // =========================================================================
241
242 /**
243 * @brief Gets the default processing token
244 * @return The token configured at construction
245 */
246 inline ProcessingToken get_default_audio_token() const { return m_default_audio_token; }
247
248 /**
249 * @brief Gets the default graphics processing token
250 * @return The graphics token configured at construction
251 */
252 inline ProcessingToken get_default_graphics_token() const { return m_default_graphics_token; }
253
254 // =========================================================================
255 // Thread Safety Access
256 // =========================================================================
257
258 /**
259 * @brief Acquires the manager's mutex for external synchronization
260 * @return Reference to the internal mutex
261 *
262 * Use this when you need to perform multiple atomic operations across
263 * the unit store. Example:
264 * @code
265 * std::lock_guard<std::mutex> lock(manager.get_mutex());
266 * auto& unit = manager.get_audio_unit_mutable(token);
267 * // Multiple operations on unit are now atomic
268 * @endcode
269 */
270 inline std::mutex& get_mutex() const { return m_manager_mutex; }
271
272private:
273 // =========================================================================
274 // Data Members
275 // =========================================================================
276
277 /// Default processing token for legacy compatibility and initialization
279
280 /// Default graphics processing token
282
283 /// Token-based map of root audio buffer units
284 /// Maps: ProcessingToken -> channel -> {root_buffers, processing chains}
285 std::unordered_map<ProcessingToken, RootAudioUnit> m_audio_units;
286
287 /// Token-based map of root graphics buffer units
288 std::unordered_map<ProcessingToken, RootGraphicsUnit> m_graphics_units;
289
290 /// Mutex for thread-safe access to all unit maps
291 mutable std::mutex m_manager_mutex;
292};
293
294}
static MayaFlux::Nodes::ProcessingToken token
Definition Timers.cpp:8
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.