MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SubsystemManager.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Core {
6
7class WindowManager;
12
13struct GlobalStreamInfo;
14struct GlobalGraphicsConfig;
15struct GlobalInputConfig;
16struct GlobalNetworkConfig;
17
18/**
19 * @class SubsystemManager
20 * @brief Central coordinator for all subsystems in the MayaFlux processing architecture
21 *
22 * Manages subsystem lifecycle, provides token-scoped processing handles, and coordinates
23 * cross-subsystem operations. Each subsystem receives a dedicated processing handle that
24 * ensures proper isolation and thread safety within its designated processing domain.
25 *
26 * Key responsibilities:
27 * - Subsystem registration and lifecycle management
28 * - Processing handle creation and distribution
29 * - Cross-subsystem data access control
30 * - Coordinated startup and shutdown sequences
31 */
32class MAYAFLUX_API SubsystemManager {
33public:
34 /**
35 * @brief Constructs SubsystemManager with required processing managers
36 * @param node_graph_manager Shared node graph manager for all subsystems
37 * @param buffer_manager Shared buffer manager for all subsystems
38 * @param task_scheduler Shared task scheduler for all subsystems
39 * @param window_manager Optional shared window manager for graphics subsystems
40 * @param input_manager Optional shared input manager for input subsystems
41 *
42 * Initializes the manager with references to the core processing systems.
43 * These managers are shared across all subsystems but accessed through
44 * token-scoped handles for proper isolation.
45 */
47 std::shared_ptr<Nodes::NodeGraphManager> node_graph_manager,
48 std::shared_ptr<Buffers::BufferManager> buffer_manager,
49 std::shared_ptr<Vruta::TaskScheduler> task_scheduler,
50 std::shared_ptr<Core::WindowManager> window_manager = nullptr,
51 std::shared_ptr<InputManager> input_manager = nullptr);
52
53 /**
54 * @brief Internal template method for type-safe subsystem creation
55 * @tparam SType Type of subsystem to create
56 * @tparam Args Constructor argument types
57 * @param type SubsystemType enum value identifying the subsystem category
58 * @param args Constructor arguments for the subsystem
59 *
60 * Creates a subsystem instance and registers it with the manager.
61 * Used internally by specific subsystem creation methods.
62 */
63 template <typename SType, typename... Args>
64 void create_subsystem_internal(SubsystemType type, Args&&... args)
65 {
66 auto subsystem = std::make_shared<SType>(std::forward<Args>(args)...);
67 add_subsystem(type, std::move(subsystem));
68 }
69
70 /**
71 * @brief Create and register the audio subsystem
72 * @tparam Args Constructor argument types
73 *
74 * Specialized creation method for AudioSubsystem. Only one audio subsystem
75 * is allowed per manager instance.
76 */
77 void create_audio_subsystem(GlobalStreamInfo& stream_info);
78
79 /**
80 * @brief Create and register the graphics subsystem
81 * @param graphics_config Global graphics configuration
82 *
83 * Specialized creation method for GraphicsSubsystem. Only one graphics
84 * subsystem is allowed per manager instance.
85 */
86 void create_graphics_subsystem(const GlobalGraphicsConfig& graphics_config);
87
88 /**
89 * @brief Create and register the input subsystem
90 * @param input_config Global input configuration
91 *
92 * Specialized creation method for InputSubsystem. Only one input
93 * subsystem is allowed per manager instance.
94 */
95 void create_input_subsystem(GlobalInputConfig& input_config);
96
97 /**
98 * @brief Create and register the network subsystem
99 * @param network_config Global network configuration
100 *
101 * Specialized creation method for NetworkSubsystem. Only one network
102 * subsystem is allowed per manager instance.
103 */
104 void create_network_subsystem(const GlobalNetworkConfig& network_config);
105
106 /** @brief Start all registered subsystems in coordination */
107 void start_all_subsystems();
108
109 /** @brief Pause all subsystems */
110 void pause_all_subsystems();
111
112 /** @brief Resume all paused subsystems */
113 void resume_all_subsystems();
114
115 /**
116 * @brief Get access to a specific subsystem by type
117 * @param type SubsystemType enum value identifying the subsystem
118 * @return Shared pointer to subsystem or nullptr if not found
119 *
120 * Provides access to registered subsystems for direct interaction.
121 * Returns nullptr if subsystem of specified type doesn't exist.
122 */
123 std::shared_ptr<ISubsystem> get_subsystem(SubsystemType type);
124
125 /**
126 * @brief Get typed access to the audio subsystem
127 * @return Shared pointer to AudioSubsystem or nullptr if not created
128 *
129 * Convenience method that automatically casts to AudioSubsystem type.
130 * Equivalent to dynamic_cast on get_subsystem(SubsystemType::AUDIO).
131 */
132 std::shared_ptr<AudioSubsystem> get_audio_subsystem();
133
134 /**
135 * @brief Get typed access to the graphics subsystem
136 * @return Shared pointer to GraphicsSubsystem or nullptr if not created
137 *
138 * Convenience method that automatically casts to GraphicsSubsystem type.
139 * Equivalent to dynamic_cast on get_subsystem(SubsystemType::GRAPHICS).
140 */
141 std::shared_ptr<GraphicsSubsystem> get_graphics_subsystem();
142
143 /**
144 * @brief Get typed access to the input subsystem
145 * @return Shared pointer to InputSubsystem or nullptr if not created
146 *
147 * Convenience method that automatically casts to InputSubsystem type.
148 * Equivalent to dynamic_cast on get_subsystem(SubsystemType::INPUT).
149 */
150 std::shared_ptr<InputSubsystem> get_input_subsystem();
151
152 /**
153 * @brief Get typed access to the network subsystem
154 * @return Shared pointer to NetworkSubsystem or nullptr if not created
155 *
156 * Convenience method that automatically casts to NetworkSubsystem type.
157 * Equivalent to dynamic_cast on get_subsystem(SubsystemType::NETWORK).
158 */
159 std::shared_ptr<NetworkSubsystem> get_network_subsystem();
160
161 /**
162 * @brief Check if a subsystem type exists
163 * @param type SubsystemType to check for existence
164 * @return True if subsystem of this type is registered
165 *
166 * Fast existence check without retrieving the subsystem instance.
167 */
168 inline bool has_subsystem(SubsystemType type) const { return m_subsystems.count(type) > 0; }
169
170 /**
171 * @brief Get all currently active subsystem types
172 * @return Vector of SubsystemType values for all registered subsystems
173 *
174 * Returns the types of all subsystems currently managed by this instance.
175 */
176 std::vector<SubsystemType> get_active_subsystem_types() const;
177
178 /**
179 * @brief Register a subsystem instance with the manager
180 * @param type SubsystemType enum value identifying the subsystem category
181 * @param subsystem Shared pointer to the subsystem instance
182 *
183 * Registers a pre-constructed subsystem with the manager and creates
184 * its processing handle. The subsystem is initialized but not started.
185 */
186 void add_subsystem(SubsystemType type, const std::shared_ptr<ISubsystem>& subsystem);
187
188 /**
189 * @brief Remove and shutdown a subsystem
190 * @param type SubsystemType enum value identifying the subsystem to remove
191 *
192 * Stops, shuts down, and removes the specified subsystem from management.
193 * Cleans up associated processing handles and resources.
194 */
195 void remove_subsystem(SubsystemType type);
196
197 /**
198 * @brief Query operational status of all subsystems
199 * @return Map of SubsystemType to boolean indicating ready/running status
200 *
201 * Returns the current operational status of all managed subsystems.
202 * Status reflects whether each subsystem is ready for operation.
203 */
204 std::unordered_map<SubsystemType, std::pair<bool, bool>> query_subsystem_status() const;
205
206 /**
207 * @brief Execute an operation with temporary elevated permissions
208 * @tparam Func Function type for the operation
209 * @param primary_tokens Primary subsystem tokens
210 * @param secondary_tokens Secondary subsystem tokens for cross-domain access
211 * @param operation Function to execute with combined access
212 *
213 * For special cross-domain operations (e.g., audio-reactive visuals).
214 * Creates a temporary handle with combined token access for controlled
215 * cross-subsystem operations.
216 */
217 template <typename Func>
219 SubsystemTokens primary_tokens,
220 SubsystemTokens /*secondary_tokens*/,
221 Func operation)
222 {
223 SubsystemTokens combined_tokens {
224 .Buffer = primary_tokens.Buffer,
225 .Node = primary_tokens.Node,
226 .Task = primary_tokens.Task
227 };
228
229 SubsystemProcessingHandle temp_handle(
230 m_buffer_manager,
231 m_node_graph_manager,
232 m_task_scheduler,
233 m_window_manager,
234 m_input_manager,
235 combined_tokens);
236
237 operation(temp_handle);
238 }
239
240 /** @brief Stop all subsystems */
241 void stop();
242
243 /** @brief Shutdown all subsystems in proper order */
244 void shutdown();
245
246 /**
247 * @brief Configure cross-subsystem data access permissions
248 * @param from SubsystemType that requests access
249 * @param to SubsystemType that provides data
250 *
251 * Establishes permission for one subsystem to read data from another.
252 * Required for cross-subsystem operations like audio-reactive visuals.
253 */
254 void allow_cross_access(SubsystemType from, SubsystemType to);
255
256 /**
257 * @brief Read data from another subsystem's buffers
258 * @param requesting_type SubsystemType making the data request
259 * @param target_type SubsystemType providing the data
260 * @param channel Channel index to read from
261 * @return Read-only span if access allowed, nullopt otherwise
262 *
263 * Enables controlled cross-subsystem data sharing with permission checking.
264 * Used for scenarios where one subsystem needs processed data from another.
265 */
266 std::optional<std::span<const double>> read_cross_subsystem_buffer(
267 SubsystemType requesting_type,
268 SubsystemType target_type,
269 uint32_t channel);
270
271 /**
272 * @brief Register a processing hook for a specific subsystem
273 * @param type SubsystemType to attach the hook to
274 * @param name Unique identifier for the hook
275 * @param hook Callback function to execute
276 * @param position When to execute the hook (PRE_PROCESS or POST_PROCESS)
277 *
278 * Process hooks allow custom code execution at specific points in the
279 * processing cycle. Used for monitoring, debugging, or additional processing.
280 */
281 void register_process_hook(SubsystemType type, const std::string& name, ProcessHook hook, HookPosition position = HookPosition::POST_PROCESS);
282
283 /**
284 * @brief Remove a previously registered processing hook
285 * @param type SubsystemType the hook is attached to
286 * @param name Unique identifier of the hook to remove
287 *
288 * Removes the named hook from the specified subsystem's processing cycle.
289 */
290 void unregister_process_hook(SubsystemType type, const std::string& name);
291
292 /**
293 * @brief Check if a processing hook exists
294 * @param type SubsystemType to check
295 * @param name Hook identifier to look for
296 * @return True if hook exists (in either pre or post position)
297 *
298 * Checks both pre-process and post-process hook collections for the named hook.
299 */
300 bool has_process_hook(SubsystemType type, const std::string& name);
301
302 /**
303 * @brief Get processing handle with validation
304 * @param type Subsystem type
305 * @return Valid handle or nullptr if validation fails
306 */
307 SubsystemProcessingHandle* get_validated_handle(SubsystemType type) const;
308
309 /** @brief Stop the audio subsystem */
310 void stop_audio_subsystem();
311
312 /** @brief Stop the graphics subsystem */
313 void stop_graphics_subsystem();
314
315 /** @brief Stop the input subsystem */
316 void stop_input_subsystem();
317
318private:
319 bool is_cross_access_allowed(SubsystemType from, SubsystemType to) const;
320
322
323 std::shared_ptr<Nodes::NodeGraphManager> m_node_graph_manager;
324 std::shared_ptr<Buffers::BufferManager> m_buffer_manager;
325 std::shared_ptr<Vruta::TaskScheduler> m_task_scheduler;
326 std::shared_ptr<Core::WindowManager> m_window_manager;
327 std::shared_ptr<InputManager> m_input_manager;
328
329 std::unordered_map<SubsystemType, std::shared_ptr<ISubsystem>> m_subsystems;
330 std::unordered_map<SubsystemType, std::unique_ptr<SubsystemProcessingHandle>> m_handles;
331 std::unordered_map<SubsystemType, std::unordered_set<SubsystemType>> m_cross_access_permissions;
332
333 mutable std::shared_mutex m_mutex; ///< Thread safety for subsystem operations
334};
335
336}
glm::vec3 position
uint32_t channel
std::shared_ptr< Buffers::BufferManager > m_buffer_manager
std::shared_ptr< Nodes::NodeGraphManager > m_node_graph_manager
std::shared_ptr< Vruta::TaskScheduler > m_task_scheduler
SubsystemTokens get_tokens_for_type(SubsystemType type) const
std::unordered_map< SubsystemType, std::unordered_set< SubsystemType > > m_cross_access_permissions
bool has_subsystem(SubsystemType type) const
Check if a subsystem type exists.
void execute_with_combined_tokens(SubsystemTokens primary_tokens, SubsystemTokens, Func operation)
Execute an operation with temporary elevated permissions.
std::shared_ptr< Core::WindowManager > m_window_manager
std::unordered_map< SubsystemType, std::unique_ptr< SubsystemProcessingHandle > > m_handles
void create_subsystem_internal(SubsystemType type, Args &&... args)
Internal template method for type-safe subsystem creation.
std::vector< SubsystemType > get_active_subsystem_types() const
Get all currently active subsystem types.
std::unordered_map< SubsystemType, std::shared_ptr< ISubsystem > > m_subsystems
std::shared_mutex m_mutex
Thread safety for subsystem operations.
std::shared_ptr< InputManager > m_input_manager
Central coordinator for all subsystems in the MayaFlux processing architecture.
Unified interface combining buffer and node processing for subsystems.
std::function< void(unsigned int num_frames)> ProcessHook
Function type for process hooks that can be registered with the engine.
HookPosition
Defines the position in the processing cycle where a hook should be executed.
@ AudioSubsystem
Audio subsystem operations (backend, device, stream management)
@ NetworkSubsystem
Network subsystem operations (endpoint management, data routing)
@ GraphicsSubsystem
Graphics subsystem operations (Vulkan, rendering pipeline)
@ InputSubsystem
Input subsystem operations (device management, event dispatch)
const Core::InputSubsystem & get_input_subsystem()
Gets the input subsystem.
Definition Input.cpp:10
Configuration for the InputSubsystem.
Configuration for the NetworkSubsystem.
Comprehensive configuration for digital audio stream processing.
MayaFlux::Vruta::ProcessingToken Task
Processing token for task scheduling operations.
MayaFlux::Buffers::ProcessingToken Buffer
Processing token for buffer operations.
MayaFlux::Nodes::ProcessingToken Node
Processing token for node graph operations.
Processing token configuration for subsystem operation.