Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
text_value.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3
4#pragma once
5
6#include <portals/text.hpp>
7#include <portals/text_resource.hpp>
8
9#include <concepts>
10#include <format>
11#include <functional>
12#include <new>
13#include <stdexcept>
14#include <type_traits>
15#include <utility>
16#include <variant>
17
18namespace portals {
19namespace detail {
21template <class T> inline constexpr bool IS_LIVE_TEXT_SOURCE = false;
23} // namespace detail
24
26using TextValueError = std::variant<TextError, TextResourceError>;
27
42class TextValue {
43public:
45 constexpr ~TextValue() = default;
46
50 constexpr TextValue(const TextValue &other) = default;
51
56 constexpr TextValue &operator=(const TextValue &other) = default;
57
58#if defined(_MSVC_STL_VERSION) && defined(_ITERATOR_DEBUG_LEVEL) && _ITERATOR_DEBUG_LEVEL != 0
63 constexpr TextValue(TextValue &&other) : TextValue(static_cast<const TextValue &>(other)) {
64 }
65
70 constexpr TextValue &operator=(TextValue &&other) {
71 return *this = static_cast<const TextValue &>(other);
72 }
73#else
77 constexpr TextValue(TextValue &&other) = default;
78
83 constexpr TextValue &operator=(TextValue &&other) = default;
84#endif
85
92 constexpr explicit TextValue(std::string_view bytes) : bytes_(bytes) {
93 }
94
100 [[nodiscard]] constexpr std::string_view resourceKey() const noexcept {
101 return resource_ ? std::string_view(bytes_) : std::string_view{};
102 }
103
105 [[nodiscard]] constexpr bool isEmptyLiteral() const noexcept {
106 return !resource_ && bytes_.empty();
107 }
108
117 [[nodiscard]] std::expected<GraphemeText, TextValueError> resolve(WidthPolicy policy, TextLimits limits = {}) const {
118 if (resource_) {
120 }
121
122 return validate(bytes_, policy, limits);
123 }
124
138 template <class Resolver>
139 requires std::invocable<Resolver, std::string_view> &&
140 std::same_as<std::invoke_result_t<Resolver, std::string_view>,
141 std::expected<std::string, TextResourceError>>
142 [[nodiscard]] std::expected<GraphemeText, TextValueError> resolve(WidthPolicy policy, Resolver &&resolver,
143 TextLimits limits = {}) const {
144 if (!resource_) {
145 return validate(bytes_, policy, limits);
146 }
147
148 const auto resolved = resolveResource(std::forward<Resolver>(resolver));
149
150 if (!resolved) {
151 return std::unexpected(TextValueError{resolved.error()});
152 }
153
154 return validate(*resolved, policy, limits);
155 }
156
157private:
158 friend class ManagedScene;
159 friend constexpr TextValue i18n(std::string_view key);
160
161 constexpr TextValue(std::string_view key, bool resource) : bytes_(key), resource_(resource) {
162 }
163
164 static std::expected<GraphemeText, TextValueError> validate(std::string_view bytes, WidthPolicy policy,
165 TextLimits limits) {
166 auto resolved = GraphemeText::from(bytes, policy, limits);
167
168 if (!resolved) {
169 return std::unexpected(TextValueError{resolved.error()});
170 }
171
172 return std::move(*resolved);
173 }
174
175 template <class Resolver> std::expected<std::string, TextResourceError> resolveResource(Resolver &&resolver) const {
176 try {
177 return std::invoke(std::forward<Resolver>(resolver), std::string_view(bytes_));
178 } catch (const std::bad_alloc &) {
179 throw;
180 } catch (const std::length_error &) {
181 throw;
182 } catch (...) {
183 return std::unexpected(TextResourceError::RESOLVER_FAILED);
184 }
185 }
186
187 std::string bytes_;
188 bool resource_{};
189};
190
199[[nodiscard]] constexpr TextValue text(std::string_view bytes) {
200 return TextValue(bytes);
201}
202
214template <class... Args>
215 requires(sizeof...(Args) > 0) && (!(detail::IS_LIVE_TEXT_SOURCE<std::remove_cvref_t<Args>> || ...))
216[[nodiscard]] TextValue text(std::format_string<Args...> pattern, Args &&...args) {
217 return text(std::format(pattern, std::forward<Args>(args)...));
218}
219
227[[nodiscard]] constexpr TextValue i18n(std::string_view key) {
228 return TextValue(key, true);
229}
230
231} // namespace portals
static std::expected< GraphemeText, TextError > from(std::string_view bytes, WidthPolicy policy={}, TextLimits limits={})
Validate, segment and measure a single logical UTF-8 buffer.
Owning literal bytes or a resource key, measured only at resolution.
Definition text_value.hpp:42
constexpr TextValue(const TextValue &other)=default
Copy expression bytes and kind into independent storage.
constexpr ~TextValue()=default
Release the independently owned expression bytes.
std::expected< GraphemeText, TextValueError > resolve(WidthPolicy policy, Resolver &&resolver, TextLimits limits={}) const
Resolve a key synchronously through the caller, or validate a literal directly.
Definition text_value.hpp:142
constexpr std::string_view resourceKey() const noexcept
Inspect the resource key without resolving or allocating.
Definition text_value.hpp:100
constexpr TextValue & operator=(const TextValue &other)=default
Replace expression bytes and kind with an owning copy.
constexpr TextValue & operator=(TextValue &&other)=default
Transfer expression storage using the standard library's default move assignment.
constexpr TextValue(TextValue &&other)=default
Transfer expression storage using the standard library's default move.
constexpr TextValue(std::string_view bytes)
Copy a literal buffer into independent expression storage.
Definition text_value.hpp:92
std::expected< GraphemeText, TextValueError > resolve(WidthPolicy policy, TextLimits limits={}) const
Resolve literal bytes without requiring a resource resolver.
Definition text_value.hpp:117
friend constexpr TextValue i18n(std::string_view key)
Copy a resource key for later resolution through an explicit caller.
Definition text_value.hpp:227
constexpr bool isEmptyLiteral() const noexcept
Definition text_value.hpp:105
Public Portals API.
Definition action.hpp:8
ui::ValueText text(std::format_string< const ui::detail::TextArgumentValue< Args > &... > pattern, Args &&...args)
Declare live formatted text over explicit scalar roots, fields or value properties.
Definition ui_value_text.hpp:567
@ RESOLVER_REQUIRED
Resource resolution was requested without a caller-owned resolver.
Definition text_resource.hpp:12
@ RESOLVER_FAILED
The resolver reported failure or threw a non-allocation exception.
Definition text_resource.hpp:14
std::variant< TextError, TextResourceError > TextValueError
Original text validation error or a distinct resource lookup error.
Definition text_value.hpp:26
Owning limits checked without silently truncating input or clusters.
Definition text.hpp:57
Owning deterministic width policy; no font or terminal matching promise.
Definition text.hpp:42