Steppable 0.0.1
A CAS project written from scratch in C++
Loading...
Searching...
No Matches
parameter.hpp
1/**************************************************************************************************
2 * Copyright (c) 2023-2025 NWSOFT *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy *
5 * of this software and associated documentation files (the "Software"), to deal *
6 * in the Software without restriction, including without limitation the rights *
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
8 * copies of the Software, and to permit persons to whom the Software is *
9 * furnished to do so, subject to the following conditions: *
10 * *
11 * The above copyright notice and this permission notice shall be included in all *
12 * copies or substantial portions of the Software. *
13 * *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
20 * SOFTWARE. *
21 **************************************************************************************************/
22
23#pragma once
24
25#include "output.hpp"
26#include "platform.hpp"
27
28#include <algorithm>
29#include <any>
30#include <string>
31#include <utility>
32#include <vector>
33
34using namespace std::literals;
35
41{
47 {
48 std::string name;
49 std::any value;
50 };
51
53 using ValueMap = std::vector<ValuedParameter>;
54
59 struct Parameter // NOLINT(cppcoreguidelines-special-member-functions)
60 {
61 std::string name;
62
69 template<typename T>
70 ValuedParameter operator=(T value) // NOLINT(cppcoreguidelines-c-copy-assignment-signature)
71 {
72 return { .name = name, .value = std::move(value) };
73 }
74
75 Parameter& operator=(const Parameter&) = delete;
76
82 explicit Parameter(std::string name) : name(std::move(name)) {}
83 };
84
90 {
92
99
107 void checkParameterOrder(const std::vector<std::string>& names);
108
116 void checkParameterNameUnordered(const std::vector<std::string>& names);
117
124 template<typename ValueT>
125 ValueT getItem(const std::string& name)
126 {
127 ValuedParameter value;
128 for (const auto& obj : values)
129 if (obj.name == name)
130 value = obj;
131 if (value.name == "")
132 {
133 output::error("ParameterMap::getItem"s, "Name {0} not found."s, { name });
135 }
136 return std::any_cast<ValueT>(value.value);
137 }
138
146 template<typename ValueT>
147 ValueT getItem(const std::string& name, const ValueT& fallback)
148 {
149 ValuedParameter value;
150 for (const auto& obj : values)
151 if (obj.name == name)
152 value = obj;
153 if (value.name == "")
154 return fallback;
155 return std::any_cast<ValueT>(value.value);
156 }
157 };
158
165 inline Parameter operator""_p(const char* name, size_t /*unused*/) { return Parameter(name); }
166
174 template<typename... Params>
175 ParameterMap processParams(Params... params)
176 {
177 return { std::vector{ params... } };
178 }
179} // namespace steppable::__internals::parameter
180
182#define PARAM_GET(map, type, name) \
183 type name; \
184 do \
185 { \
186 (name) = (map).template getItem<type>(#name); \
187 } while (0)
188
190#define PARAM_GET_FALLBACK(map, type, name, fallback) \
191 type name; \
192 do \
193 { \
194 (name) = (map).template getItem<type>(#name, fallback); \
195 } while (0)
Contains the parameter utilities to allow named parameters to be passed into functions.
Definition parameter.cpp:26
ParameterMap processParams(Params... params)
Process parameters passed into a function.
Definition parameter.hpp:175
std::vector< ValuedParameter > ValueMap
A type containing all the parameters of a function, in calling order.
Definition parameter.hpp:53
void programSafeExit(const int status)
Exit the program safely.
Definition platform.hpp:61
void error(const std::string &name, const std::basic_string< T > &msg, const std::vector< std::string > &args={})
Prints an error message.
Definition output.hpp:80
An object containing all parameters and their values passed into a function.
Definition parameter.hpp:90
ValueMap values
All ValuedParameter objects passed to a function.
Definition parameter.hpp:91
ValueT getItem(const std::string &name, const ValueT &fallback)
Gets an item from the current parameter map. Returns the fallback the matching parameter if not found...
Definition parameter.hpp:147
ValueT getItem(const std::string &name)
Gets an item from the current parameter map.
Definition parameter.hpp:125
void checkParameterNameUnordered(const std::vector< std::string > &names)
Checks the presence of all the arguments, no matter which order they are specified....
Definition parameter.cpp:45
void checkParameterOrder(const std::vector< std::string > &names)
Checks the presence of all the arguments, in the order which they should be specified....
Definition parameter.cpp:27
ParameterMap(ValueMap values)
Initializes a new ParameterMap object.
Definition parameter.hpp:98
Stores the name of a parameter.
Definition parameter.hpp:60
ValuedParameter operator=(T value)
Shorthand function to facilitate setting values of parameters.
Definition parameter.hpp:70
Parameter & operator=(const Parameter &)=delete
std::string name
The name of the parameter.
Definition parameter.hpp:61
Parameter(std::string name)
Initializes a new Parameter object.
Definition parameter.hpp:82
A parameter with a name and value.
Definition parameter.hpp:47
std::string name
The name of the parameter.
Definition parameter.hpp:48
std::any value
The value of the parameter.
Definition parameter.hpp:49
Untitled