LArSoft
v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
|
Helper to select one among multiple choices via strings. More...
#include "MultipleChoiceSelection.h"
Public Types | |
using | Choices_t = Choices |
Type of the underlying choice. More... | |
using | Option_t = details::MultipleChoiceSelectionOption_t< Choices_t > |
Public Member Functions | |
MultipleChoiceSelection ()=default | |
template<typename... Options> | |
MultipleChoiceSelection (std::initializer_list< Option_t > options) | |
Constructor: adds the specified options. More... | |
template<typename... Aliases> | |
auto | addOption (Choices_t value, std::string label, Aliases...aliases) -> Option_t const & |
template<typename... Aliases> | |
auto | addAlias (Choices_t value, Aliases...aliases) -> std::enable_if_t< details::AllConvertibleToStrings_v< Aliases... >, Option_t const & > |
template<typename... Aliases> | |
auto | addAlias (Option_t const &option, Aliases...aliases) -> std::enable_if_t< details::AllConvertibleToStrings_v< Aliases... >, Option_t const & > |
template<typename... Aliases> | |
auto | recordLabels (std::size_t index, std::string alias, Aliases...moreAliases) -> std::enable_if_t< details::AllConvertibleToStrings_v< Aliases... >> |
Option management | |
std::size_t | size () const |
Returns the number of available options. More... | |
bool | empty () const |
Returns whether there is no available option. More... | |
template<typename... Aliases> | |
Option_t const & | addOption (Choices_t value, std::string label, Aliases...aliases) |
Adds a new option to the selector. More... | |
template<typename... Aliases> | |
std::enable_if_t< details::AllConvertibleToStrings_v< Aliases... >, Option_t const & > | addAlias (Choices_t value, Aliases...aliases) |
Adds aliases to an existing option. More... | |
template<typename... Aliases> | |
std::enable_if_t< details::AllConvertibleToStrings_v< Aliases... >, Option_t const & > | addAlias (Option_t const &option, Aliases...aliases) |
Adds labels to an existing option. More... | |
bool | hasOption (Choices_t value) const |
Returns whether the selector has an option with the specified value . More... | |
bool | hasOption (std::string const &label) const |
Returns whether the selector has an option with the specified label . More... | |
bool | hasOption (Option_t const &option) const |
Returns if the specified option is present in the selector (by value). More... | |
Option access | |
Option_t const & | get (Choices_t value) const |
Returns the specified option. More... | |
Option_t const & | get (std::string const &label) const |
Returns the option with the specified label. More... | |
Option_t const & | parse (std::string const &label) const |
Returns the option matching the specified label. More... | |
Presentation and dumping | |
— BEGIN -------------------------------------------------------------— | |
std::string | optionListString (std::string const &sep=", ") const |
Returns a string with the (main) name of all options. More... | |
std::string | optionListDump (std::string const &indent, std::string const &firstIndent) const |
Returns a string with all the options, one per line. More... | |
std::string | optionListDump (std::string const &indent="") const |
Returns a string with all the options, one per line. More... | |
Private Types | |
using | Selector_t = MultipleChoiceSelection< Choices > |
This type. More... | |
using | OptionList_t = std::vector< Option_t > |
Type of collection of options. More... | |
using | OptionLabelMap_t = std::map< std::string, std::size_t, details::SorterFrom< details::CaseInsensitiveComparer >> |
Type of label index (associative container: label to option index). More... | |
Private Member Functions | |
Option_t const & | addOption (Option_t &&option) |
Moves the specified option into the list of registered options. More... | |
void | recordLabel (std::string &&label, std::size_t index) |
template<typename... Aliases> | |
std::enable_if_t< details::AllConvertibleToStrings_v< Aliases... > > | recordLabels (std::size_t index, std::string alias, Aliases...moreAliases) |
void | unregisterLabel (std::string const &label) |
Removes the specified label from the register. More... | |
OptionList_t::const_iterator | findOption (Choices_t value) const |
Returns an iterator to the option with label , or npos if none. More... | |
OptionList_t::iterator | findOption (Choices_t value) |
Returns an iterator to the option with label , or npos if none. More... | |
std::size_t | findOptionIndex (Choices_t value) const |
Returns the index of the option with label , or npos if none. More... | |
std::size_t | findOptionIndex (std::string const &label) const |
Returns the index of the option with label , or npos if none. More... | |
Private Attributes | |
OptionList_t | fOptions |
The list of registered objects. More... | |
OptionLabelMap_t | fLabelToOptionIndex |
Map from labels to option index in fOptions . More... | |
Static Private Attributes | |
static constexpr auto | npos = std::numeric_limits<std::size_t>::max() |
Special value. More... | |
Helper to select one among multiple choices via strings.
Choices | type describing the choices |
Definition at line 200 of file MultipleChoiceSelection.h.
using util::MultipleChoiceSelection< Choices >::Choices_t = Choices |
Type of the underlying choice.
Definition at line 217 of file MultipleChoiceSelection.h.
using util::MultipleChoiceSelection< Choices >::Option_t = details::MultipleChoiceSelectionOption_t<Choices_t> |
Definition at line 219 of file MultipleChoiceSelection.h.
|
private |
Type of label index (associative container: label to option index).
Definition at line 369 of file MultipleChoiceSelection.h.
|
private |
Type of collection of options.
— END ------------------------------------------------------------——
Definition at line 365 of file MultipleChoiceSelection.h.
|
private |
This type.
Definition at line 214 of file MultipleChoiceSelection.h.
|
default |
Default constructor: flags are set to DefaultFlags
, options may be added later.
util::MultipleChoiceSelection< Choices >::MultipleChoiceSelection | ( | std::initializer_list< Option_t > | options | ) |
Constructor: adds the specified options.
Options | a number of Option_t objects |
options | a list of options to add to the selector |
All specified options are added as with addOption()
.
Definition at line 749 of file MultipleChoiceSelection.h.
std::enable_if_t<details::AllConvertibleToStrings_v<Aliases...>, Option_t const&> util::MultipleChoiceSelection< Choices >::addAlias | ( | Choices_t | value, |
Aliases... | aliases | ||
) |
Adds aliases to an existing option.
Args | any number of std::string |
value | the value of the option to assign labels to |
aliases | the additional alias(es) to assign to this option |
UnknownOptionError | if no option with value is registered yet |
std::enable_if_t<details::AllConvertibleToStrings_v<Aliases...>, Option_t const&> util::MultipleChoiceSelection< Choices >::addAlias | ( | Option_t const & | option, |
Aliases... | aliases | ||
) |
Adds labels to an existing option.
Aliases | any number of std::string |
option | the option to assign labels to |
aliases | the additional alias(es) to assign to this option |
UnknownOptionError | if no option with value is registered yet |
The option with the same value as the option
argument is assigned the specified aliases.
auto util::MultipleChoiceSelection< Choices >::addAlias | ( | Choices_t | value, |
Aliases... | aliases | ||
) | -> std::enable_if_t<details::AllConvertibleToStrings_v<Aliases...>, Option_t const&> |
Definition at line 783 of file MultipleChoiceSelection.h.
References util::details::MultipleChoiceSelectionOption_t< Choices >::value(), and util::details::MultipleChoiceSelectionOption_t< Choices >::value_as_string().
auto util::MultipleChoiceSelection< Choices >::addAlias | ( | Option_t const & | option, |
Aliases... | aliases | ||
) | -> std::enable_if_t<details::AllConvertibleToStrings_v<Aliases...>, Option_t const&> |
Definition at line 795 of file MultipleChoiceSelection.h.
References util::details::MultipleChoiceSelectionOption_t< Choices >::addAlias().
Option_t const& util::MultipleChoiceSelection< Choices >::addOption | ( | Choices_t | value, |
std::string | label, | ||
Aliases... | aliases | ||
) |
Adds a new option to the selector.
Aliases | any number of std::string |
value | the value of the new option |
label | the first label to associate to the option |
aliases | additional aliases for this option |
OptionAlreadyExistsError | if there is already an option with value |
addAlias()
An option must always have a label; aliases are instead optional. There must be no existing option with the specified value
, otherwise an exception will be thrown with the label of the existing option. To add aliases to an existing option, use addAlias()
instead. Currently, it is not possible to change a label after having added it.
|
private |
Moves the specified option into the list of registered options.
option | the option to move |
OptionAlreadyExistsError | if there is already an option with the same value or any of the labels; in that case, label() method of the exception will report the offending label of option or its name if it is the value to be duplicated |
Definition at line 896 of file MultipleChoiceSelection.h.
References util::details::MultipleChoiceSelectionOption_t< Choices >::labels(), and util::size().
auto util::MultipleChoiceSelection< Choices >::addOption | ( | Choices_t | value, |
std::string | label, | ||
Aliases... | aliases | ||
) | -> Option_t const& |
Definition at line 773 of file MultipleChoiceSelection.h.
References util::details::MultipleChoiceSelectionOption_t< Choices >::value().
bool util::MultipleChoiceSelection< Choices >::empty | ( | ) | const |
Returns whether there is no available option.
Definition at line 765 of file MultipleChoiceSelection.h.
|
private |
Returns an iterator to the option with label
, or npos
if none.
Definition at line 976 of file MultipleChoiceSelection.h.
References util::details::MultipleChoiceSelectionOption_t< Choices >::value().
|
private |
Returns an iterator to the option with label
, or npos
if none.
Definition at line 985 of file MultipleChoiceSelection.h.
References util::details::MultipleChoiceSelectionOption_t< Choices >::value().
|
private |
Returns the index of the option with label
, or npos
if none.
Definition at line 994 of file MultipleChoiceSelection.h.
References d, and util::size().
|
private |
Returns the index of the option with label
, or npos
if none.
Definition at line 1002 of file MultipleChoiceSelection.h.
auto util::MultipleChoiceSelection< Choices >::get | ( | Choices_t | value | ) | const |
Returns the specified option.
value | value of the requested option |
UnknownOptionError | if there is no available option with value (the string of the exception is empty) |
Definition at line 824 of file MultipleChoiceSelection.h.
References util::details::MultipleChoiceSelectionOption_t< Choices >::value(), and util::details::MultipleChoiceSelectionOption_t< Choices >::value_as_string().
auto util::MultipleChoiceSelection< Choices >::get | ( | std::string const & | label | ) | const |
Returns the option with the specified label.
label | label of the requested option |
UnknownOptionError | if there is no available option with label (the string of the exception shows label value) |
Definition at line 834 of file MultipleChoiceSelection.h.
bool util::MultipleChoiceSelection< Choices >::hasOption | ( | Choices_t | value | ) | const |
Returns whether the selector has an option with the specified value
.
Definition at line 803 of file MultipleChoiceSelection.h.
bool util::MultipleChoiceSelection< Choices >::hasOption | ( | std::string const & | label | ) | const |
Returns whether the selector has an option with the specified label
.
Definition at line 810 of file MultipleChoiceSelection.h.
bool util::MultipleChoiceSelection< Choices >::hasOption | ( | Option_t const & | option | ) | const |
Returns if the specified option is present in the selector (by value).
Definition at line 817 of file MultipleChoiceSelection.h.
References util::details::MultipleChoiceSelectionOption_t< Choices >::value().
std::string util::MultipleChoiceSelection< Choices >::optionListDump | ( | std::string const & | indent, |
std::string const & | firstIndent | ||
) | const |
Returns a string with all the options, one per line.
Definition at line 872 of file MultipleChoiceSelection.h.
References art::detail::indent().
|
inline |
Returns a string with all the options, one per line.
Definition at line 355 of file MultipleChoiceSelection.h.
References art::detail::indent().
std::string util::MultipleChoiceSelection< Choices >::optionListString | ( | std::string const & | sep = ", " | ) | const |
Returns a string with the (main) name of all options.
Definition at line 852 of file MultipleChoiceSelection.h.
auto util::MultipleChoiceSelection< Choices >::parse | ( | std::string const & | label | ) | const |
Returns the option matching the specified label.
label | label of the requested option |
UnknownOptionError | if there is no available option with label (the string of the exception shows label value) |
Definition at line 844 of file MultipleChoiceSelection.h.
|
private |
Associates label
to the option at index
.
OptionAlreadyExistsError | if there is already an option with label |
Definition at line 929 of file MultipleChoiceSelection.h.
|
private |
Associates all labels
to the option at index
.
OptionAlreadyExistsError | if there is already an option with any of the aliases |
auto util::MultipleChoiceSelection< Choices >::recordLabels | ( | std::size_t | index, |
std::string | alias, | ||
Aliases... | moreAliases | ||
) | -> std::enable_if_t<details::AllConvertibleToStrings_v<Aliases...>> |
Definition at line 952 of file MultipleChoiceSelection.h.
std::size_t util::MultipleChoiceSelection< Choices >::size | ( | ) | const |
Returns the number of available options.
Definition at line 758 of file MultipleChoiceSelection.h.
|
private |
Removes the specified label from the register.
Definition at line 969 of file MultipleChoiceSelection.h.
|
private |
Map from labels to option index in fOptions
.
Definition at line 374 of file MultipleChoiceSelection.h.
|
private |
The list of registered objects.
Definition at line 371 of file MultipleChoiceSelection.h.
|
staticprivate |
Special value.
Definition at line 416 of file MultipleChoiceSelection.h.