31#include "neml2/base/Option.h"
32#include "neml2/misc/errors.h"
33#include "neml2/misc/string_utils.h"
34#include "neml2/misc/types.h"
122 template <
typename T>
123 T
get(
const std::string &)
const;
138 template <
typename K,
typename V>
139 std::map<K, V>
get_map(
const std::string &,
const std::string &)
const;
162 template <
typename T, FType f = FType::NONE>
163 void add(
const std::string &
name,
const T & default_value, std::string
doc);
187 template <
typename T, FType f = FType::NONE>
188 void add(
const std::string &
name, std::string
doc);
212 template <
typename T, FType f = FType::NONE>
230 template <
typename T>
247 template <
typename T>
248 void set(
const std::string &
name,
const T & value);
264 template <
typename T>
277 template <
typename T>
279 template <
typename T>
282 template <
typename T>
284 template <
typename T>
285 void add_buffer(
const std::string &, std::string);
289 using map_type = std::map<std::string, std::unique_ptr<OptionBase>, std::less<>>;
384 throw NEMLException(
"ERROR: no option named \"" +
name +
"\" found.\n\nKnown options:\n" +
388 if (!opt_base->defined())
390 "\" is being accessed before it is defined.");
392 auto ptr =
dynamic_cast<Option<T> *
>(opt_base);
395 "\" is not of the requested type: " + opt_base->type());
399template <
typename K,
typename V>
405 if (keys.size() != values.size())
406 throw NEMLException(
"Trying to build a map from '" + key_option +
"' and '" + value_option +
407 "' with " + std::to_string(keys.size()) +
" keys and " +
408 std::to_string(values.size()) +
" values.");
409 std::map<K, V> result;
410 for (
size_t i = 0; i < keys.size(); i++)
412 if (result.find(keys[i]) != result.end())
413 throw NEMLException(
"Trying to build a map from '" + key_option +
"' and '" + value_option +
415 result[keys[i]] = values[i];
420template <
typename T, FType F>
426 "', but an option with the same name already exists.");
431 ptr->doc() = std::move(
doc);
435 ptr->set() = default_value;
438 ptr->required() =
false;
439 ptr->suppressed() =
false;
440 ptr->user_specified() =
false;
441 ptr->defined() =
true;
444template <
typename T, FType F>
450 "', but an option with the same name already exists.");
455 ptr->doc() = std::move(
doc);
459 ptr->required() =
true;
460 ptr->suppressed() =
false;
461 ptr->user_specified() =
false;
462 ptr->defined() =
false;
465template <
typename T, FType F>
471 "', but an option with the same name already exists.");
476 ptr->doc() = std::move(
doc);
480 ptr->required() =
false;
481 ptr->suppressed() =
false;
482 ptr->user_specified() =
false;
483 ptr->defined() =
false;
492 "', but an option with the same name already exists.");
497 ptr->set() = default_value;
500 ptr->required() =
false;
501 ptr->suppressed() =
true;
502 ptr->user_specified() =
false;
503 ptr->defined() =
true;
511 throw NEMLException(
"Trying to set option '" +
name +
"', but it does not exist.");
515 if (ptr->suppressed())
517 "', which is not allowed. Use set_private() instead.");
523 ptr->user_specified() =
false;
524 ptr->defined() =
true;
532 throw NEMLException(
"Trying to set private option '" +
name +
"', but it does not exist.");
536 if (!ptr->suppressed())
538 "', but it is not marked as private.");
544 ptr->user_specified() =
false;
545 ptr->defined() =
true;
Definition OptionBase.h:44
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:54
map_type::iterator iterator
Option map iterator.
Definition OptionSet.h:291
map_type _values
Data structure to map names with values.
Definition OptionSet.h:372
std::map< K, V > get_map(const std::string &, const std::string &) const
Get two options and bind them to form a map.
Definition OptionSet.h:401
void add_input(const std::string &, const VariableName &, std::string)
const std::string & name() const
A readonly reference to the option set's name.
Definition OptionSet.h:77
const_iterator begin() const
Iterator pointing to the beginning of the set of options.
const std::string & type() const
A readonly reference to the option set's type.
Definition OptionSet.h:81
void add_parameter(const std::string &, const TensorName< T > &, std::string)
Convenient methods to add parameter.
Definition OptionSet.h:550
bool defined(const std::string &name) const
OptionSet(const OptionSet &)
std::string to_str() const
Print the contents.
T get(const std::string &) const
Definition OptionSet.h:381
map_type::const_iterator const_iterator
Constant option map iterator.
Definition OptionSet.h:293
std::map< std::string, std::unique_ptr< OptionBase >, std::less<> > map_type
The type of the map that we store internally.
Definition OptionSet.h:289
std::string & name()
A writable reference to the option set's name.
Definition OptionSet.h:79
void add_output(const std::string &, const VariableName &, std::string)
Convenient methods to add output variable.
const OptionBase & get(const std::string &) const
Get a const reference to the specified option value.
std::string & type()
A writable reference to the option set's type.
Definition OptionSet.h:83
const std::string & doc() const
A readonly reference to the option set's docstring.
Definition OptionSet.h:89
void suppress(const std::string &name)
Suppress an option.
void add_optional(const std::string &name, std::string doc)
Create an optional option with its docstring, without a default value.
Definition OptionSet.h:467
const std::string & section() const
A readonly reference to the option set's section.
Definition OptionSet.h:93
void set(const std::string &name, const T &value)
Set an option.
Definition OptionSet.h:508
void add_private(const std::string &name, const T &default_value)
Create a private option with its default value.
Definition OptionSet.h:488
const std::string & path() const
A readonly reference to the option set's path.
Definition OptionSet.h:85
void add_buffer(const std::string &, const TensorName< T > &, std::string)
Convenient methods to add buffer.
Definition OptionSet.h:566
void set_private(const std::string &name, const T &value)
Set a private option.
Definition OptionSet.h:529
std::string & doc()
A writable reference to the option set's docstring.
Definition OptionSet.h:91
bool user_specified(const std::string &name) const
struct neml2::OptionSet::Metadata _metadata
void clear()
Clear internal data structures & frees any allocated memory.
iterator end()
Iterator pointing to the end of the set of options.
const_iterator end() const
Iterator pointing to the end of the set of options.
std::string & path()
A writable reference to the option set's path.
Definition OptionSet.h:87
iterator begin()
Iterator pointing to the beginning of the set of options.
void add(const std::string &name, const T &default_value, std::string doc)
Create an option with its default value and its docstring.
Definition OptionSet.h:422
std::string & section()
A writable reference to the option set's section.
Definition OptionSet.h:95
std::size_t size() const
Definition OptionSet.h:110
bool contains(const std::string &) const
void add_optional_input(const std::string &, std::string)
void add_output(const std::string &, std::string)
void add_input(const std::string &, std::string)
void add_optional_output(const std::string &, std::string)
OptionSet(OptionSet &&) noexcept
std::string stringify(const T &t)
Definition string_utils.h:70
Definition DiagnosticsInterface.h:31
std::ostream & operator<<(std::ostream &, const EnumSelection &)
bool options_compatible(const OptionSet &opts, const OptionSet &additional_opts)
std::string VariableName
Definition types.h:75
@ BUFFER
Definition types.h:95
@ PARAMETER
Definition types.h:94
The name of a tensor object that can be referenced in the input files.
Definition TensorName.h:47