Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
ui_app.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/detail/move_only_function.hpp>
6
7#include <expected>
8#include <functional>
9#include <memory>
10#include <optional>
11#include <ostream>
12#include <span>
13#include <string>
14#include <string_view>
15#include <utility>
16
17namespace portals::ui {
18
22template <class T> class OptionsRef {
23public:
25 explicit OptionsRef(T &value) noexcept : value_(&value) {
26 }
27
29 OptionsRef(T &&value) = delete;
30
32 const T &get() const noexcept {
33 return *value_;
34 }
35
36private:
37 T *value_;
38};
39
44template <class T> OptionsRef<T> opts(T &value) noexcept {
45 return OptionsRef<T>(value);
46}
47
51template <class T> void opts(const T &&value) = delete;
52
54namespace app {
55
64
66class Arguments {
67public:
68 struct Data;
74 static std::expected<Arguments, ProcessContextError> capture(int argc, const char *const *argv);
76 int argc() const noexcept;
78 const char *const *argv() const noexcept;
80 std::size_t size() const noexcept;
85 std::string_view operator[](std::size_t index) const;
87 std::span<const std::string>::iterator begin() const noexcept;
89 std::span<const std::string>::iterator end() const noexcept;
90
91private:
92 explicit Arguments(std::shared_ptr<const Data> data) noexcept : data_(std::move(data)) {
93 }
94
95 std::shared_ptr<const Data> data_;
96 friend std::expected<Arguments, ProcessContextError> args();
97};
98
104class Environment {
105public:
106 struct Data;
107 using Entry = std::pair<std::string, std::string>;
111 static std::expected<Environment, ProcessContextError> fromEntries(std::span<const Entry> entries);
117 std::expected<std::optional<std::string_view>, ProcessContextError> get(std::string_view name) const noexcept;
119 std::size_t size() const noexcept;
123 bool operator==(const Environment &other) const noexcept;
124
125private:
126 explicit Environment(std::shared_ptr<const Data> data) noexcept : data_(std::move(data)) {
127 }
128
129 std::shared_ptr<const Data> data_;
130 friend std::expected<Environment, ProcessContextError> environment();
131};
132
141std::expected<void, ProcessContextError> initialize(int argc, const char *const *argv);
148std::expected<void, ProcessContextError> initialize(int argc, const char *const *argv, const Environment &environment);
150std::expected<Arguments, ProcessContextError> args();
152std::expected<Environment, ProcessContextError> environment();
153
155inline std::expected<Environment, ProcessContextError> env() {
156 return environment();
157}
158} // namespace app
159
166public:
168 LaunchText() noexcept = default;
169
173 LaunchText(std::string_view bytes) {
174 if (!bytes.empty()) {
175 bytes_ = std::make_shared<const std::string>(bytes.data(), bytes.size());
176 }
177 }
178
182 LaunchText(const std::string &bytes) : LaunchText(std::string_view(bytes)) {
183 }
184
188 LaunchText(const char *bytes) : LaunchText(bytes ? std::string_view(bytes) : std::string_view{}) {
189 }
190
192 LaunchText(const LaunchText &other) noexcept = default;
194 LaunchText(LaunchText &&other) noexcept = default;
198 LaunchText &operator=(const LaunchText &other) noexcept = default;
202 LaunchText &operator=(LaunchText &&other) noexcept = default;
203
205 const char *data() const noexcept {
206 return bytes_ ? bytes_->data() : "";
207 }
208
210 std::size_t size() const noexcept {
211 return bytes_ ? bytes_->size() : 0;
212 }
213
215 bool empty() const noexcept {
216 return size() == 0;
217 }
218
220 std::string_view view() const noexcept {
221 return {data(), size()};
222 }
223
228 std::size_t find(std::string_view text, std::size_t position = 0) const noexcept {
229 return view().find(text, position);
230 }
231
236 friend bool operator==(const LaunchText &left, const LaunchText &right) noexcept {
237 return left.view() == right.view();
238 }
239
244 friend bool operator==(const LaunchText &left, std::string_view right) noexcept {
245 return left.view() == right;
246 }
247
252 friend bool operator==(const LaunchText &left, const std::string &right) noexcept {
253 return left.view() == right;
254 }
255
260 friend bool operator==(const LaunchText &left, const char *right) noexcept {
261 return left.view() == (right ? std::string_view(right) : std::string_view{});
262 }
263
268 friend std::ostream &operator<<(std::ostream &stream, const LaunchText &text) {
269 return stream << text.view();
270 }
271
272private:
273 std::shared_ptr<const std::string> bytes_;
274};
275
277enum class CommandLineErrorCode {
280 NO_MEMORY,
281 PROCESS_CONTEXT
282};
283
286 CommandLineErrorCode code{};
288 std::optional<app::ProcessContextError> processContext{};
289};
290
291enum class LaunchDisposition {
292 CONTINUE,
293 FINISH
294};
295
298 LaunchDisposition disposition{LaunchDisposition::CONTINUE};
300};
301
303using CommandLineParser =
304 portals::detail::MoveOnlyFunction<std::expected<LaunchAction, CommandLineError>(const app::Arguments &)>;
305
306namespace detail {
314std::expected<LaunchAction, CommandLineError> commandLinePreflight(int argc, const char *const *argv,
315 CommandLineParser &parser) noexcept;
316} // namespace detail
317} // namespace portals::ui
Immutable owning launch text with allocation-free empty, copy and move operations....
Definition ui_app.hpp:165
LaunchText(const std::string &bytes)
Definition ui_app.hpp:182
LaunchText(const char *bytes)
Definition ui_app.hpp:188
std::size_t find(std::string_view text, std::size_t position=0) const noexcept
Definition ui_app.hpp:228
friend bool operator==(const LaunchText &left, const char *right) noexcept
Definition ui_app.hpp:260
friend bool operator==(const LaunchText &left, const std::string &right) noexcept
Definition ui_app.hpp:252
LaunchText(const LaunchText &other) noexcept=default
std::string_view view() const noexcept
Definition ui_app.hpp:220
const char * data() const noexcept
Definition ui_app.hpp:205
LaunchText & operator=(const LaunchText &other) noexcept=default
LaunchText(LaunchText &&other) noexcept=default
friend std::ostream & operator<<(std::ostream &stream, const LaunchText &text)
Definition ui_app.hpp:268
LaunchText() noexcept=default
Create empty text without allocating.
LaunchText & operator=(LaunchText &&other) noexcept=default
std::size_t size() const noexcept
Definition ui_app.hpp:210
friend bool operator==(const LaunchText &left, const LaunchText &right) noexcept
Definition ui_app.hpp:236
bool empty() const noexcept
Definition ui_app.hpp:215
friend bool operator==(const LaunchText &left, std::string_view right) noexcept
Definition ui_app.hpp:244
OptionsRef(T &value) noexcept
Definition ui_app.hpp:25
OptionsRef(T &&value)=delete
const T & get() const noexcept
Definition ui_app.hpp:32
Immutable owning argument snapshot, including argv[0]; copies share immutable storage.
Definition ui_app.hpp:66
friend std::expected< Arguments, ProcessContextError > args()
static std::expected< Arguments, ProcessContextError > capture(int argc, const char *const *argv)
Opaque immutable storage; no implementation borrow is exposed.
std::size_t size() const noexcept
const char *const * argv() const noexcept
int argc() const noexcept
std::span< conststd::string >::iterator begin() const noexcept
std::span< conststd::string >::iterator end() const noexcept
Immutable owning environment snapshot; missing and empty values remain distinct. Windows names compar...
Definition ui_app.hpp:104
static std::expected< Environment, ProcessContextError > fromEntries(std::span< const Entry > entries)
std::pair< std::string, std::string > Entry
Opaque immutable storage; no process-owned pointers escape.
Definition ui_app.hpp:107
std::expected< std::optional< std::string_view >, ProcessContextError > get(std::string_view name) const noexcept
friend std::expected< Environment, ProcessContextError > environment()
std::size_t size() const noexcept
Read-only process snapshots, independent of a current Scene or Application.
Definition ui_app.hpp:54
ProcessContextError
Process capture failures are separate from widget and parser failures.
Definition ui_app.hpp:57
@ ALREADY_INITIALIZED
Initialization conflicts with the previously published snapshot.
Definition ui_app.hpp:60
@ CAPTURE_FAILED
Process capture or snapshot query failed without changing stored state.
Definition ui_app.hpp:62
@ UNINITIALIZED
No process snapshot has been successfully published.
Definition ui_app.hpp:58
std::expected< Arguments, ProcessContextError > args()
std::expected< Environment, ProcessContextError > environment()
std::expected< Environment, ProcessContextError > env()
Definition ui_app.hpp:155
std::expected< void, ProcessContextError > initialize(int argc, const char *const *argv)
Capture argv and the environment once, before any concurrent foreign environment mutation....
@ CALLBACK_FAILED
A non-allocation exception escaped into a runner callback boundary.
Definition application.hpp:34
@ NO_MEMORY
Allocation failed at a runner exception boundary.
Definition application.hpp:33
@ INVALID_ARGUMENT
Empty task, callback or immutable data owner, or invalid policy.
Definition async.hpp:26
Owning parser diagnostic, safe after the parser's captures retire.
Definition ui_app.hpp:285
std::optional< app::ProcessContextError > processContext
Exact initialization cause, when applicable.
Definition ui_app.hpp:288
CommandLineErrorCode code
Original parser or boundary failure category.
Definition ui_app.hpp:286
LaunchText message
Immutable owning diagnostic; empty fallback and ownership transfers allocate nothing.
Definition ui_app.hpp:287
Successful preflight, optionally printing help/version before any application is created.
Definition ui_app.hpp:297
LaunchText output
Immutable owning help/version text, printed to stdout only for FINISH.
Definition ui_app.hpp:299
LaunchDisposition disposition
Successful preflight decision.
Definition ui_app.hpp:298