SpECTRE  v2024.06.05
Options::Auto< T, Label > Class Template Reference

A class indicating that a parsed value can be automatically computed instead of specified. More...

#include <Auto.hpp>

Public Types

using value_type = std::optional< T >
 

Public Member Functions

 Auto (T value)
 
 Auto (Auto &&)=default
 
Autooperator= (Auto &&)=default
 
 operator const value_type & () const
 

Detailed Description

template<typename T, typename Label = AutoLabel::Auto>
class Options::Auto< T, Label >

A class indicating that a parsed value can be automatically computed instead of specified.

When an Auto<T> is parsed from an input file, the value may be specified either as the AutoLabel (defaults to "Auto") or as a value of type T. When this class is passed to the constructor of the class taking it as an option, it can be implicitly converted to a std::optional<U>, for any type U implicitly creatable from a T.

class ExampleClass {
public:
ExampleClass() = default;
struct AutoArg {
using type = Options::Auto<int>;
static type suggested_value() { return {}; }
static constexpr Options::String help =
"Integer that can be automatically chosen";
};
struct OptionalArg {
static constexpr Options::String help = "Optional parameter";
};
struct AllArg {
static constexpr Options::String help = "Optional parameter all";
};
static constexpr Options::String help =
"A class that can automatically choose an argument";
using options = tmpl::list<AutoArg, OptionalArg, AllArg>;
explicit ExampleClass(std::optional<int> auto_arg,
: value(auto_arg ? *auto_arg : -12),
optional_value(opt_arg),
all_value(all_arg) {}
int value{};
std::optional<double> optional_value{};
};
A class indicating that a parsed value can be automatically computed instead of specified.
Definition: Auto.hpp:41
constexpr T & value(T &t)
Returns t.value() if t is a std::optional otherwise returns t.
Definition: OptionalHelpers.hpp:32
const char *const String
The string used in option structs.
Definition: String.hpp:8
'All' label
Definition: Auto.hpp:25
const auto example1 = TestHelpers::test_creation<ExampleClass>(
"AutoArg: 7\n"
"OptionalArg: 10.\n"
"AllArg: [0, 1, 2]");
CHECK(example1.value == 7);
CHECK(example1.optional_value == 10.);
CHECK(example1.all_value == std::vector<int>{{0, 1, 2}});
const auto example2 = TestHelpers::test_creation<ExampleClass>(
"AutoArg: Auto\n"
"OptionalArg: None\n"
"AllArg: [0, 1, 2]");
CHECK(example2.value == -12);
CHECK(example2.optional_value == std::nullopt);
CHECK(example2.all_value == std::vector<int>{{0, 1, 2}});
const auto example3 = TestHelpers::test_creation<ExampleClass>(
"AutoArg: 7\n"
"OptionalArg: 10.\n"
"AllArg: All");
CHECK(example3.value == 7);
CHECK(example3.optional_value == 10.);
CHECK(example3.all_value == std::nullopt);

The documentation for this class was generated from the following file: