MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Lila.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <expected>
4
5namespace Lila {
6
7class ClangInterpreter;
8class Server;
9struct ClientInfo;
10
11enum class OperationMode : uint8_t {
12 Direct, ///< Only direct evaluation
13 Server, ///< Only server mode
14 Both ///< Both direct eval and server
15};
16
17/**
18 * @class Lila
19 * @brief Central orchestrator and entry point for live coding in MayaFlux
20 *
21 * The Lila class provides a unified interface for interactive live coding in MayaFlux.
22 * It manages the lifecycle and coordination of the embedded Clang interpreter and the TCP server,
23 * enabling both direct code evaluation and networked live coding sessions.
24 *
25 * ## Core Responsibilities
26 * - Initialize and configure the live coding environment (interpreter and/or server)
27 * - Evaluate C++ code snippets and files at runtime
28 * - Manage TCP server for remote live coding sessions
29 * - Provide symbol lookup and introspection
30 * - Allow customization of include paths and compile flags
31 * - Expose event/callback hooks for success, error, and server events
32 *
33 * ## Usage Flow
34 * 1. Construct a Lila instance
35 * 2. Call initialize() with desired mode (Direct, Server, Both)
36 * 3. For direct evaluation, use eval() or eval_file()
37 * 4. For server mode, use start_server(), stop_server(), and event hooks
38 * 5. Use get_symbol_address() and get_defined_symbols() for runtime introspection
39 * 6. Optionally configure include paths and compile flags before initialization
40 * 7. Register callbacks for success, error, and server/client events as needed
41 *
42 * Lila is intended as the main API for embedding live coding capabilities in MayaFlux.
43 * End users interact with Lila via higher-level interfaces or the live_server binary,
44 * not directly with its internal components.
45 */
46class LILA_API Lila {
47public:
48 /**
49 * @brief Constructs a Lila instance
50 */
51 Lila();
52
53 /**
54 * @brief Destructor; cleans up interpreter and server resources
55 */
56 ~Lila();
57
58 /**
59 * @brief Initializes the live coding environment.
60 *
61 * @param mode Operation mode (Direct, Server, Both).
62 * @param server_port TCP port for server mode (default: 9090).
63 * @param skip_host_library_load If true, the interpreter will not call
64 * LoadDynamicLibrary on MayaFluxLib at startup. Used by
65 * in-process attach scenarios where MayaFluxLib is already
66 * resident in the calling process. Default false preserves the
67 * lila_server behaviour.
68 * @return True if initialization succeeded, false otherwise.
69 */
70 bool initialize(
71 OperationMode mode = OperationMode::Direct,
72 int server_port = 9090,
73 bool skip_host_library_load = false) noexcept;
74
75 /**
76 * @brief Evaluates a C++ code snippet directly
77 * @param code Code to execute
78 * @return True if evaluation succeeded, false otherwise
79 */
80 bool eval(const std::string& code);
81
82 /**
83 * @brief Evaluates a C++ code file directly
84 * @param filepath Path to the file to execute
85 * @return True if evaluation succeeded, false otherwise
86 */
87 bool eval_file(const std::string& filepath);
88
89 /**
90 * @brief Starts the TCP server for remote live coding
91 * @param port TCP port to listen on (default: 9090)
92 */
93 void start_server(int port = 9090);
94
95 /**
96 * @brief Stops the TCP server and disconnects all clients
97 */
98 void stop_server();
99
100 /**
101 * @brief Checks if the server is currently running
102 * @return True if running, false otherwise
103 */
104 [[nodiscard]] bool is_server_running() const;
105
106#ifdef MAYAFLUX_PLATFORM_WINDOWS
107 void set_main_thread_id(uint32_t thread_id);
108#endif
109
110 /**
111 * @brief Gets the address of a symbol defined in the interpreter
112 * @param name Symbol name
113 * @return Pointer to symbol, or nullptr if not found
114 */
115 void* get_symbol_address(const std::string& name);
116
117 /**
118 * @brief Gets a list of all defined symbols
119 * @return Vector of symbol names
120 */
121 std::vector<std::string> get_defined_symbols();
122
123 /**
124 * @brief Adds an include path for code evaluation
125 * @param path Directory to add to the include search path
126 */
127 void add_include_path(const std::string& path);
128
129 /**
130 * @brief Adds a compile flag for code evaluation
131 * @param flag Compiler flag to add
132 */
133 void add_compile_flag(const std::string& flag);
134
135 /**
136 * @brief Registers a callback for successful code evaluation
137 * @param callback Function to call on success
138 */
139 void on_success(std::function<void()> callback);
140
141 /**
142 * @brief Registers a callback for code evaluation errors
143 * @param callback Function to call on error (receives error string)
144 */
145 void on_error(std::function<void(const std::string&)> callback);
146
147 /**
148 * @brief Registers a callback for new client connections (server mode)
149 * @param callback Function to call when a client connects
150 */
151 void on_server_client_connected(std::function<void(const ClientInfo&)> callback);
152
153 /**
154 * @brief Registers a callback for client disconnections (server mode)
155 * @param callback Function to call when a client disconnects
156 */
157 void on_server_client_disconnected(std::function<void(const ClientInfo&)> callback);
158
159 /**
160 * @brief Registers a callback for server start events
161 * @param callback Function to call when the server starts
162 */
163 void on_server_started(std::function<void()> callback);
164
165 /**
166 * @brief Blocks until server shutdown (main thread event loop)
167 * @param external_flag std::atomic<bool> Whether to force shutdown externally
168 *
169 * On macOS: Runs CFRunLoop to process main queue (required for JIT GLFW)
170 * On other platforms: Simple blocking wait
171 *
172 * Should be called on main thread after initialize() in Server mode.
173 * Returns when stop_server() is called or server fails.
174 */
175 void await_shutdown(const std::atomic<bool>* external_flag);
176
177 /**
178 * @brief Request shutdown from any thread
179 *
180 * Signals await_shutdown() to return. Thread-safe.
181 */
182 void request_shutdown();
183
184 /**
185 * @brief Gets the last error message
186 * @return String containing the last error
187 */
188 [[nodiscard]] std::string get_last_error() const;
189
190 /**
191 * @brief Gets the current operation mode
192 * @return OperationMode value
193 */
194 [[nodiscard]] OperationMode get_current_mode() const;
195
196 /**
197 * @brief Sets the server loop rate in seconds
198 * @param rate_seconds Loop rate in seconds
199 */
200 void set_server_loop_rate(float rate_seconds) { server_loop_rate = rate_seconds; }
201
202 /**
203 * @brief Gets the server loop rate in seconds
204 * @return Loop rate in seconds
205 */
206 [[nodiscard]]
208 {
209 return server_loop_rate;
210 }
211
212private:
213 std::unique_ptr<ClangInterpreter> m_interpreter; ///< Embedded Clang interpreter
214 std::unique_ptr<Server> m_server; ///< TCP server for live coding
215
216 OperationMode m_current_mode; ///< Current operation mode
217 std::function<void()> m_success_callback; ///< Success callback
218 std::function<void(const std::string&)> m_error_callback; ///< Error callback
219
220 bool initialize_interpreter(bool skip_host_library_load);
221 bool initialize_server(int port);
222 std::atomic<bool> m_shutdown_requested { false };
223
224 float server_loop_rate; ///< Server loop rate in seconds
225
226 std::expected<std::string, std::string> handle_server_message(std::string_view message);
227
228 /**
229 * @brief Escapes a string for safe JSON encoding
230 * @param str Input string
231 * @return Escaped string
232 */
233 static std::string escape_json(const std::string& str);
234};
235
236} // namespace Lila
float get_server_loop_rate() const
Gets the server loop rate in seconds.
Definition Lila.hpp:207
std::unique_ptr< Server > m_server
TCP server for live coding.
Definition Lila.hpp:214
std::function< void()> m_success_callback
Success callback.
Definition Lila.hpp:217
void set_server_loop_rate(float rate_seconds)
Sets the server loop rate in seconds.
Definition Lila.hpp:200
std::unique_ptr< ClangInterpreter > m_interpreter
Embedded Clang interpreter.
Definition Lila.hpp:213
std::function< void(const std::string &)> m_error_callback
Error callback.
Definition Lila.hpp:218
OperationMode m_current_mode
Current operation mode.
Definition Lila.hpp:216
float server_loop_rate
Server loop rate in seconds.
Definition Lila.hpp:224
OperationMode
Definition Lila.hpp:11
void initialize()
Definition main.cpp:11
Information about a connected client.
Definition EventBus.hpp:58