Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
parameter_text.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <pulp/format/detail/locale_independent_float.hpp>
6
7#include <charconv>
8#include <cmath>
9#include <optional>
10#include <string>
11#include <string_view>
12
13namespace pulp::format {
14
16inline std::string format_parameter_text(const state::ParamInfo& info,
17 float plain_value) {
18 const float value = state::constrain_param_value(info, plain_value);
19 if (info.to_string) {
20 PULP_TRY {
21 return info.to_string(value);
23 return {};
24 }
25 }
26
27 if (!info.value_labels.empty()) {
28 const float step = info.range.step > 0.0f ? info.range.step : 1.0f;
29 const auto raw = std::llround((value - info.range.min) / step);
30 const auto index = static_cast<std::size_t>(std::clamp<int64_t>(
31 raw, 0, static_cast<int64_t>(info.value_labels.size() - 1)));
32 return info.value_labels[index];
33 }
34
35 char number[128];
36 const int precision = info.kind == state::ParamKind::Continuous ? 2 : 0;
37 const auto converted = std::to_chars(number, number + sizeof(number), value,
38 std::chars_format::fixed, precision);
39 if (converted.ec != std::errc{}) return {};
40 std::string result(number, converted.ptr);
41 if (!info.unit.empty()) {
42 result += ' ';
43 result += info.unit;
44 }
45 return result;
46}
47
52inline std::optional<float> parse_parameter_text(const state::ParamInfo& info,
53 std::string_view text) {
54 if (info.from_string) {
55 PULP_TRY {
56 const float parsed = info.from_string(std::string(text));
57 if (std::isfinite(parsed)) return state::constrain_param_value(info, parsed);
59 return std::nullopt;
60 }
61 }
62
63 if (auto labeled = state::param_value_for_label(info, text)) return labeled;
64 if ((info.kind == state::ParamKind::Toggle || info.kind == state::ParamKind::Enum) &&
65 !info.value_labels.empty())
66 return std::nullopt;
67
68 double parsed = 0.0;
69 const auto result = detail::parse_double_c_locale(text, parsed);
70 if (result.consumed == 0 || result.range_error || !std::isfinite(parsed))
71 return std::nullopt;
72
73 auto suffix = text.substr(result.consumed);
74 while (!suffix.empty() && std::isspace(static_cast<unsigned char>(suffix.front())))
75 suffix.remove_prefix(1);
76 while (!suffix.empty() && std::isspace(static_cast<unsigned char>(suffix.back())))
77 suffix.remove_suffix(1);
78 if (!suffix.empty() && suffix != info.unit) return std::nullopt;
79
80 return state::constrain_param_value(info, static_cast<float>(parsed));
81}
82
83} // namespace pulp::format
Portable try/catch macros for source that must also compile with C++ exceptions disabled (-fno-except...
#define PULP_TRY
Definition exceptions.hpp:42
#define PULP_CATCH_ALL
Definition exceptions.hpp:43
What the product grants a device: the quota table indexed by capability tier and live thermal state.
Definition aax_editor.hpp:31
std::string format_parameter_text(const state::ParamInfo &info, float plain_value)
Canonical plain-value formatting shared by format adapters and framework UI.
Definition parameter_text.hpp:16
std::optional< float > parse_parameter_text(const state::ParamInfo &info, std::string_view text)
Parse host text into a constrained plain value.
Definition parameter_text.hpp:52
@ Continuous
Definition parameter.hpp:74
@ Toggle
Definition parameter.hpp:76
@ Enum
Definition parameter.hpp:77
float constrain_param_value(const ParamInfo &info, float value)
Definition parameter.hpp:475
std::optional< float > param_value_for_label(const ParamInfo &info, std::string_view text)
Definition parameter.hpp:488
Immutable parameter metadata, registered once at plugin initialization.
Definition parameter.hpp:316