Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
numeric.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <cstdint>
6#include <expected>
7
8namespace portals {
9
20
23 double minimum{};
24 double maximum{100};
25 double step{1};
26
28 bool operator==(const NumericRange &other) const = default;
29};
30
35public:
37 NumericValue() = default;
38
43 static std::expected<NumericValue, NumericError> create(double initial, NumericRange range = {});
44
46 double value() const noexcept {
47 return value_;
48 }
49
51 NumericRange range() const noexcept {
52 return range_;
53 }
54
58 std::expected<NumericValue, NumericError> withValue(double candidate) const;
59
61 bool operator==(const NumericValue &other) const = default;
62
63private:
64 NumericValue(double value, NumericRange range) : value_(value), range_(range) {
65 }
66
67 double value_{};
68 NumericRange range_{};
69};
70
71namespace detail {
76double nudgeNumeric(const NumericValue &value, int direction, std::uint64_t steps = 1) noexcept;
77
82double snapNumeric(NumericRange range, double candidate) noexcept;
83} // namespace detail
84} // namespace portals
std::expected< NumericValue, NumericError > withValue(double candidate) const
NumericValue()=default
Construct zero in the default 0..100 range with step one.
bool operator==(const NumericValue &other) const =default
NumericRange range() const noexcept
Definition numeric.hpp:51
static std::expected< NumericValue, NumericError > create(double initial, NumericRange range={})
double value() const noexcept
Definition numeric.hpp:46
Public Portals API.
Definition action.hpp:8
@ INVALID_RANGE
Window exceeds the fixed size, overflows, contradicts BOM extent or excludes the requested anchor.
Definition document_encoding.hpp:98
@ INVALID_TEXT
Projected row is not admitted UTF-8 under the scene text policy.
Definition find_bar.hpp:53
NumericError
Allocation-free numeric validation cause, preserved by managed diagnostics.
Definition numeric.hpp:11
@ NOT_FINITE
A value, bound or step is NaN or infinite.
Definition numeric.hpp:12
@ INITIAL_CONFLICT
An external model was combined with an explicit numeric override.
Definition numeric.hpp:17
@ OUT_OF_RANGE
An explicit value is outside the inclusive bounds.
Definition numeric.hpp:16
@ INVALID_STEP
The user increment is not positive.
Definition numeric.hpp:14
@ UNREPRESENTABLE_STEP
A sub-range increment cannot advance at one of the bounds.
Definition numeric.hpp:15
Owning inclusive bounds and user increment; validated together with a numeric value.
Definition numeric.hpp:22
double step
Positive representable user increment; external values need not lie on its grid.
Definition numeric.hpp:25
double maximum
Inclusive finite upper bound with a finite positive span.
Definition numeric.hpp:24
double minimum
Inclusive finite lower bound, default zero.
Definition numeric.hpp:23
bool operator==(const NumericRange &other) const =default