Aria 2.0.0
C++23 MVVM framework (C++20 minimum) — reactive, coroutine-first, ABI-layered
Loading...
Searching...
No Matches
http_adapter.hpp
Go to the documentation of this file.
1#pragma once
50
51#include "aria/abi/export.hpp"
53#include "http_config.hpp"
54#include "http_view.hpp"
55
56#include <cstdint>
57#include <functional>
58#include <memory>
59#include <string>
60#include <string_view>
61#include <vector>
62
63// Forward declaration so consumers don't pay for the cpp-httplib include
64// unless they actually use the `native_server()` escape hatch below.
65namespace httplib { class Server; }
66
68
71public:
72 explicit HttpAdapter(HttpAdapterConfig config = {});
73 ~HttpAdapter() override;
74
75 HttpAdapter(const HttpAdapter&) = delete;
79
80 // ── Lifecycle ──────────────────────────────────────────────────────
81
84 bool start();
85
88 void stop();
89
91 [[nodiscard]] bool running() const noexcept;
92
95 [[nodiscard]] std::uint16_t actual_port() const noexcept;
96
98 [[nodiscard]] std::size_t client_count() const noexcept;
99
100 // ── View registry ──────────────────────────────────────────────────
101
107 HttpView& register_view(std::string id, std::string kind);
108
110 [[nodiscard]] HttpView* find_view(std::string_view id);
111
114 void unregister_view(std::string_view id);
115
118 struct ViewInfo {
119 std::string id;
120 std::string kind;
121 };
122 [[nodiscard]] std::vector<ViewInfo> list_views() const;
123
124 // ── IViewAdapter ───────────────────────────────────────────────────
125
126 [[nodiscard]] std::string_view platform_name() const noexcept override {
127 return "http";
128 }
129
130 void set_text(binding::IView& v, std::string_view text) override;
131 [[nodiscard]] std::string get_text(binding::IView& v) override;
133 binding::IView& v, std::function<void(std::string_view)> cb) override;
134
135 void set_bool(binding::IView& v, bool value) override;
136 [[nodiscard]] bool get_bool(binding::IView& v) override;
138 binding::IView& v, std::function<void(bool)> cb) override;
139
140 void set_int(binding::IView& v, int value) override;
141 [[nodiscard]] int get_int(binding::IView& v) override;
143 binding::IView& v, std::function<void(int)> cb) override;
144
145 void set_int64(binding::IView& v, std::int64_t value) override;
146 [[nodiscard]] std::int64_t get_int64(binding::IView& v) override;
148 binding::IView& v, std::function<void(std::int64_t)> cb) override;
149
150 void set_uint64(binding::IView& v, std::uint64_t value) override;
151 [[nodiscard]] std::uint64_t get_uint64(binding::IView& v) override;
153 binding::IView& v, std::function<void(std::uint64_t)> cb) override;
154
155 void set_float(binding::IView& v, float value) override;
156 [[nodiscard]] float get_float(binding::IView& v) override;
158 binding::IView& v, std::function<void(float)> cb) override;
159
160 void set_double(binding::IView& v, double value) override;
161 [[nodiscard]] double get_double(binding::IView& v) override;
163 binding::IView& v, std::function<void(double)> cb) override;
164
165 void set_visible(binding::IView& v, bool visible) override;
166 void set_enabled(binding::IView& v, bool enabled) override;
167
169 binding::IView& v, std::function<void()> cb) override;
170
171 // ── Custom command channel ─────────────────────────────────────────
172
176 std::function<std::string(std::string_view args_json)>;
177
182 void register_command(std::string_view view_id,
183 std::string_view command_name,
184 CommandHandler handler);
185
187 void unregister_command(std::string_view view_id,
188 std::string_view command_name);
189
190 // ── Native server escape hatch ─────────────────────────────────────
191
214 [[nodiscard]] ::httplib::Server& native_server() noexcept;
215
216private:
217 struct Impl;
218 std::shared_ptr<Impl> p_;
219};
220
221} // namespace aria::adapters::http
RAII handle to a single subscription.
Definition subscription.hpp:44
bool start()
Start the HTTP server on a background thread.
bool get_bool(binding::IView &v) override
std::uint64_t get_uint64(binding::IView &v) override
HttpView * find_view(std::string_view id)
Look up a previously-registered view by id, or nullptr.
void set_float(binding::IView &v, float value) override
void set_double(binding::IView &v, double value) override
::aria::Subscription on_click(binding::IView &v, std::function< void()> cb) override
void unregister_command(std::string_view view_id, std::string_view command_name)
Unregister a previously-registered command.
double get_double(binding::IView &v) override
std::size_t client_count() const noexcept
Number of currently connected SSE clients.
::httplib::Server & native_server() noexcept
Direct access to the underlying cpp-httplib server, for consumers that want to register their own RES...
HttpAdapter(const HttpAdapter &)=delete
HttpAdapter(HttpAdapterConfig config={})
HttpAdapter & operator=(const HttpAdapter &)=delete
std::uint16_t actual_port() const noexcept
Actual bound port — useful when config.port == 0 and the OS picked.
void set_uint64(binding::IView &v, std::uint64_t value) override
void set_text(binding::IView &v, std::string_view text) override
bool running() const noexcept
True iff the server thread is alive and listening.
::aria::Subscription on_text_changed(binding::IView &v, std::function< void(std::string_view)> cb) override
float get_float(binding::IView &v) override
::aria::Subscription on_bool_changed(binding::IView &v, std::function< void(bool)> cb) override
void register_command(std::string_view view_id, std::string_view command_name, CommandHandler handler)
Register a custom command.
void set_int(binding::IView &v, int value) override
HttpView & register_view(std::string id, std::string kind)
Register a logical view with stable id and kind.
HttpAdapter(HttpAdapter &&)=delete
void set_int64(binding::IView &v, std::int64_t value) override
::aria::Subscription on_int_changed(binding::IView &v, std::function< void(int)> cb) override
std::int64_t get_int64(binding::IView &v) override
void set_visible(binding::IView &v, bool visible) override
std::vector< ViewInfo > list_views() const
void set_enabled(binding::IView &v, bool enabled) override
std::function< std::string(std::string_view args_json)> CommandHandler
Custom command handler signature.
Definition http_adapter.hpp:175
void unregister_view(std::string_view id)
Unregister a view (also fires its on_destroy signal so any BindingEngine subscriptions are released c...
::aria::Subscription on_int64_changed(binding::IView &v, std::function< void(std::int64_t)> cb) override
::aria::Subscription on_float_changed(binding::IView &v, std::function< void(float)> cb) override
std::string_view platform_name() const noexcept override
Definition http_adapter.hpp:126
std::string get_text(binding::IView &v) override
void set_bool(binding::IView &v, bool value) override
void stop()
Stop the server; closes all SSE connections.
HttpAdapter & operator=(HttpAdapter &&)=delete
int get_int(binding::IView &v) override
::aria::Subscription on_uint64_changed(binding::IView &v, std::function< void(std::uint64_t)> cb) override
::aria::Subscription on_double_changed(binding::IView &v, std::function< void(double)> cb) override
Logical view exposed over HTTP.
Definition http_view.hpp:29
Abstract platform adapter — knows how to read/write/observe widgets.
Definition view_adapter.hpp:80
Abstract platform widget reference.
Definition view_adapter.hpp:28
#define ARIA_HTTP_API
Definition export.hpp:66
Configuration for the HTTP adapter.
Logical "view" exposed over HTTP/REST/SSE.
Definition http_adapter.hpp:67
Definition http_adapter.hpp:65
Definition validation_key.hpp:110
Snapshot of currently registered (id, kind) pairs.
Definition http_adapter.hpp:118
std::string kind
Definition http_adapter.hpp:120
std::string id
Definition http_adapter.hpp:119
Configuration for HttpAdapter.
Definition http_config.hpp:15