MayaFlux 0.1.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 * @param mode Operation mode (Direct, Server, Both)
61 * @param server_port TCP port for server mode (default: 9090)
62 * @return True if initialization succeeded, false otherwise
63 */
64 bool initialize(OperationMode mode = OperationMode::Direct, int server_port = 9090) noexcept;
65
66 /**
67 * @brief Evaluates a C++ code snippet directly
68 * @param code Code to execute
69 * @return True if evaluation succeeded, false otherwise
70 */
71 bool eval(const std::string& code);
72
73 /**
74 * @brief Evaluates a C++ code file directly
75 * @param filepath Path to the file to execute
76 * @return True if evaluation succeeded, false otherwise
77 */
78 bool eval_file(const std::string& filepath);
79
80 /**
81 * @brief Starts the TCP server for remote live coding
82 * @param port TCP port to listen on (default: 9090)
83 */
84 void start_server(int port = 9090);
85
86 /**
87 * @brief Stops the TCP server and disconnects all clients
88 */
89 void stop_server();
90
91 /**
92 * @brief Checks if the server is currently running
93 * @return True if running, false otherwise
94 */
95 [[nodiscard]] bool is_server_running() const;
96
97 /**
98 * @brief Gets the address of a symbol defined in the interpreter
99 * @param name Symbol name
100 * @return Pointer to symbol, or nullptr if not found
101 */
102 void* get_symbol_address(const std::string& name);
103
104 /**
105 * @brief Gets a list of all defined symbols
106 * @return Vector of symbol names
107 */
108 std::vector<std::string> get_defined_symbols();
109
110 /**
111 * @brief Adds an include path for code evaluation
112 * @param path Directory to add to the include search path
113 */
114 void add_include_path(const std::string& path);
115
116 /**
117 * @brief Adds a compile flag for code evaluation
118 * @param flag Compiler flag to add
119 */
120 void add_compile_flag(const std::string& flag);
121
122 /**
123 * @brief Registers a callback for successful code evaluation
124 * @param callback Function to call on success
125 */
126 void on_success(std::function<void()> callback);
127
128 /**
129 * @brief Registers a callback for code evaluation errors
130 * @param callback Function to call on error (receives error string)
131 */
132 void on_error(std::function<void(const std::string&)> callback);
133
134 /**
135 * @brief Registers a callback for new client connections (server mode)
136 * @param callback Function to call when a client connects
137 */
138 void on_server_client_connected(std::function<void(const ClientInfo&)> callback);
139
140 /**
141 * @brief Registers a callback for client disconnections (server mode)
142 * @param callback Function to call when a client disconnects
143 */
144 void on_server_client_disconnected(std::function<void(const ClientInfo&)> callback);
145
146 /**
147 * @brief Registers a callback for server start events
148 * @param callback Function to call when the server starts
149 */
150 void on_server_started(std::function<void()> callback);
151
152 /**
153 * @brief Gets the last error message
154 * @return String containing the last error
155 */
156 [[nodiscard]] std::string get_last_error() const;
157
158 /**
159 * @brief Gets the current operation mode
160 * @return OperationMode value
161 */
162 [[nodiscard]] OperationMode get_current_mode() const;
163
164private:
165 std::unique_ptr<ClangInterpreter> m_interpreter; ///< Embedded Clang interpreter
166 std::unique_ptr<Server> m_server; ///< TCP server for live coding
167
168 OperationMode m_current_mode; ///< Current operation mode
169 std::function<void()> m_success_callback; ///< Success callback
170 std::function<void(const std::string&)> m_error_callback; ///< Error callback
171
172 bool initialize_interpreter();
173 bool initialize_server(int port);
174
175 std::expected<std::string, std::string> handle_server_message(std::string_view message);
176
177 /**
178 * @brief Escapes a string for safe JSON encoding
179 * @param str Input string
180 * @return Escaped string
181 */
182 static std::string escape_json(const std::string& str);
183};
184
185} // namespace Lila
std::unique_ptr< Server > m_server
TCP server for live coding.
Definition Lila.hpp:166
std::function< void()> m_success_callback
Success callback.
Definition Lila.hpp:169
std::unique_ptr< ClangInterpreter > m_interpreter
Embedded Clang interpreter.
Definition Lila.hpp:165
std::function< void(const std::string &)> m_error_callback
Error callback.
Definition Lila.hpp:170
OperationMode m_current_mode
Current operation mode.
Definition Lila.hpp:168
OperationMode
Definition Lila.hpp:11
void initialize()
Definition main.cpp:11
Information about a connected client.
Definition EventBus.hpp:58