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 /**
107 * @brief Gets the address of a symbol defined in the interpreter
108 * @param name Symbol name
109 * @return Pointer to symbol, or nullptr if not found
110 */
111 void* get_symbol_address(const std::string& name);
112
113 /**
114 * @brief Gets a list of all defined symbols
115 * @return Vector of symbol names
116 */
117 std::vector<std::string> get_defined_symbols();
118
119 /**
120 * @brief Adds an include path for code evaluation
121 * @param path Directory to add to the include search path
122 */
123 void add_include_path(const std::string& path);
124
125 /**
126 * @brief Adds a compile flag for code evaluation
127 * @param flag Compiler flag to add
128 */
129 void add_compile_flag(const std::string& flag);
130
131 /**
132 * @brief Load a shared library into the JIT symbol table.
133 * @param path Full path to the shared library.
134 */
135 void load_library(const std::string& path);
136
137 /**
138 * @brief Registers a callback for successful code evaluation
139 * @param callback Function to call on success
140 */
141 void on_success(std::function<void()> callback);
142
143 /**
144 * @brief Registers a callback for code evaluation errors
145 * @param callback Function to call on error (receives error string)
146 */
147 void on_error(std::function<void(const std::string&)> callback);
148
149 /**
150 * @brief Registers a callback for new client connections (server mode)
151 * @param callback Function to call when a client connects
152 */
153 void on_server_client_connected(std::function<void(const ClientInfo&)> callback);
154
155 /**
156 * @brief Registers a callback for client disconnections (server mode)
157 * @param callback Function to call when a client disconnects
158 */
159 void on_server_client_disconnected(std::function<void(const ClientInfo&)> callback);
160
161 /**
162 * @brief Registers a callback for server start events
163 * @param callback Function to call when the server starts
164 */
165 void on_server_started(std::function<void()> callback);
166
167 /**
168 * @brief Blocks until server shutdown (main thread event loop)
169 * @param external_flag std::atomic<bool> Whether to force shutdown externally
170 *
171 * On macOS: Runs CFRunLoop to process main queue (required for JIT GLFW)
172 * On other platforms: Simple blocking wait
173 *
174 * Should be called on main thread after initialize() in Server mode.
175 * Returns when stop_server() is called or server fails.
176 */
177 void await_shutdown(const std::atomic<bool>* external_flag);
178
179 /**
180 * @brief Request shutdown from any thread
181 *
182 * Signals await_shutdown() to return. Thread-safe.
183 */
184 void request_shutdown();
185
186 /**
187 * @brief Gets the last error message
188 * @return String containing the last error
189 */
190 [[nodiscard]] std::string get_last_error() const;
191
192 /**
193 * @brief Gets the current operation mode
194 * @return OperationMode value
195 */
196 [[nodiscard]] OperationMode get_current_mode() const;
197
198 /**
199 * @brief Sets the server loop rate in seconds
200 * @param rate_seconds Loop rate in seconds
201 */
202 void set_server_loop_rate(float rate_seconds) { server_loop_rate = rate_seconds; }
203
204 /**
205 * @brief Gets the server loop rate in seconds
206 * @return Loop rate in seconds
207 */
208 [[nodiscard]]
210 {
211 return server_loop_rate;
212 }
213
214private:
215 std::unique_ptr<ClangInterpreter> m_interpreter; ///< Embedded Clang interpreter
216 std::unique_ptr<Server> m_server; ///< TCP server for live coding
217
218 OperationMode m_current_mode; ///< Current operation mode
219 std::function<void()> m_success_callback; ///< Success callback
220 std::function<void(const std::string&)> m_error_callback; ///< Error callback
221
222 bool initialize_interpreter(bool skip_host_library_load);
223 bool initialize_server(int port);
224 std::atomic<bool> m_shutdown_requested { false };
225
226 float server_loop_rate; ///< Server loop rate in seconds
227
228 std::expected<std::string, std::string> handle_server_message(std::string_view message);
229
230 /**
231 * @brief Escapes a string for safe JSON encoding
232 * @param str Input string
233 * @return Escaped string
234 */
235 static std::string escape_json(const std::string& str);
236};
237
238} // namespace Lila
float get_server_loop_rate() const
Gets the server loop rate in seconds.
Definition Lila.hpp:209
std::unique_ptr< Server > m_server
TCP server for live coding.
Definition Lila.hpp:216
std::function< void()> m_success_callback
Success callback.
Definition Lila.hpp:219
void set_server_loop_rate(float rate_seconds)
Sets the server loop rate in seconds.
Definition Lila.hpp:202
std::unique_ptr< ClangInterpreter > m_interpreter
Embedded Clang interpreter.
Definition Lila.hpp:215
std::function< void(const std::string &)> m_error_callback
Error callback.
Definition Lila.hpp:220
OperationMode m_current_mode
Current operation mode.
Definition Lila.hpp:218
float server_loop_rate
Server loop rate in seconds.
Definition Lila.hpp:226
OperationMode
Definition Lila.hpp:11
void initialize()
Definition main.cpp:11
Information about a connected client.
Definition EventBus.hpp:58