alpaqa pantr
Nonconvex constrained optimization
Loading...
Searching...
No Matches
params.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <alpaqa/export.hpp>
4
5#include <optional>
6#include <span>
7#include <stdexcept>
8#include <string_view>
9#include <tuple>
10
11namespace alpaqa::params {
12
13/// Represents a parameter value encoded as a string in the format
14/// `abc.def.key=value`.
15struct ALPAQA_EXPORT ParamString {
16 /// Full key string, used for diagnostics.
17 std::string_view full_key;
18 /// The subkey to resolve next.
19 std::string_view key;
20 /// The value of the parameter to store.
21 std::string_view value;
22};
23
24/// Custom parameter parsing exception.
25struct ALPAQA_EXPORT invalid_param : std::invalid_argument {
26 using std::invalid_argument::invalid_argument;
27};
28
29/// Split the string @p full on the first occurrence of @p tok.
30/// Returns (s, "") if tok was not found.
31inline auto split_key(std::string_view full, char tok = '.') {
32 auto tok_pos = full.find(tok);
33 if (tok_pos == full.npos)
34 return std::make_tuple(full, std::string_view{});
35 std::string_view key{full.begin(), full.begin() + tok_pos};
36 std::string_view rem{full.begin() + tok_pos + 1, full.end()};
37 return std::make_tuple(key, rem);
38}
39
40/// Update/overwrite the first argument based on the option in @p s.
41template <class T>
42void set_param(T &, ParamString); /* deliberately undefined */
43
44/// Overwrites @p t based on the @p options that start with @p prefix.
45/// If @p used is not `nullopt`, sets corresponding flag of the options that
46/// were used.
47template <class T>
48void ALPAQA_EXPORT set_params(
49 T &t, std::string_view prefix, std::span<const std::string_view> options,
50 std::optional<std::span<bool>> used = std::nullopt) {
51
52 size_t index = 0;
53 for (const auto &kv : options) {
54 auto [key, value] = split_key(kv, '=');
55 auto [pfx, remainder] = split_key(key);
56 auto curr_index = index++;
57 if (pfx != prefix)
58 continue;
59 if (used)
60 (*used)[curr_index] = true;
61 set_param(t, {.full_key = kv, .key = remainder, .value = value});
62 }
63}
64
65} // namespace alpaqa::params
void set_param(bool &b, ParamString s)
Definition: params.cpp:21
void set_params(T &t, std::string_view prefix, std::span< const std::string_view > options, std::optional< std::span< bool > > used=std::nullopt)
Overwrites t based on the options that start with prefix.
Definition: params.hpp:48
std::string_view key
The subkey to resolve next.
Definition: params.hpp:19
std::string_view full_key
Full key string, used for diagnostics.
Definition: params.hpp:17
auto split_key(std::string_view full, char tok='.')
Split the string full on the first occurrence of tok.
Definition: params.hpp:31
std::string_view value
The value of the parameter to store.
Definition: params.hpp:21
Represents a parameter value encoded as a string in the format abc.def.key=value.
Definition: params.hpp:15
Custom parameter parsing exception.
Definition: params.hpp:25