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... | |
Option_t & | get (Choices_t value) |
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 204 of file MultipleChoiceSelection.h.
using util::MultipleChoiceSelection< Choices >::Choices_t = Choices |
Type of the underlying choice.
Definition at line 222 of file MultipleChoiceSelection.h.
using util::MultipleChoiceSelection< Choices >::Option_t = details::MultipleChoiceSelectionOption_t<Choices_t> |
Definition at line 224 of file MultipleChoiceSelection.h.
|
private |
Type of label index (associative container: label to option index).
Definition at line 388 of file MultipleChoiceSelection.h.
|
private |
Type of collection of options.
— END ------------------------------------------------------------——
Definition at line 382 of file MultipleChoiceSelection.h.
|
private |
This type.
Definition at line 217 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 806 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 838 of file MultipleChoiceSelection.h.
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 854 of file MultipleChoiceSelection.h.
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 966 of file MultipleChoiceSelection.h.
auto util::MultipleChoiceSelection< Choices >::addOption | ( | Choices_t | value, |
std::string | label, | ||
Aliases... | aliases | ||
) | -> Option_t const& |
Definition at line 828 of file MultipleChoiceSelection.h.
bool util::MultipleChoiceSelection< Choices >::empty | ( | ) | const |
Returns whether there is no available option.
Definition at line 820 of file MultipleChoiceSelection.h.
|
private |
Returns an iterator to the option with label
, or npos
if none.
Definition at line 1052 of file MultipleChoiceSelection.h.
|
private |
Returns an iterator to the option with label
, or npos
if none.
Definition at line 1063 of file MultipleChoiceSelection.h.
|
private |
Returns the index of the option with label
, or npos
if none.
Definition at line 1075 of file MultipleChoiceSelection.h.
|
private |
Returns the index of the option with label
, or npos
if none.
Definition at line 1086 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 886 of file MultipleChoiceSelection.h.
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 898 of file MultipleChoiceSelection.h.
|
private |
Retrieves the option with the specified value
.
UnknownOptionError | if there is no available option with value |
Definition at line 1095 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 864 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 871 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 880 of file MultipleChoiceSelection.h.
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 943 of file MultipleChoiceSelection.h.
|
inline |
Returns a string with all the options, one per line.
Definition at line 372 of file MultipleChoiceSelection.h.
std::string util::MultipleChoiceSelection< Choices >::optionListString | ( | std::string const & | sep = ", " | ) | const |
Returns a string with the (main) name of all options.
Definition at line 922 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 913 of file MultipleChoiceSelection.h.
|
private |
Associates label
to the option at index
.
OptionAlreadyExistsError | if there is already an option with label |
Definition at line 1002 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 1028 of file MultipleChoiceSelection.h.
std::size_t util::MultipleChoiceSelection< Choices >::size | ( | void | ) | const |
Returns the number of available options.
Definition at line 814 of file MultipleChoiceSelection.h.
|
private |
Removes the specified label from the register.
Definition at line 1046 of file MultipleChoiceSelection.h.
|
private |
Map from labels to option index in fOptions
.
Definition at line 394 of file MultipleChoiceSelection.h.
|
private |
The list of registered objects.
Definition at line 391 of file MultipleChoiceSelection.h.
|
staticprivate |
Special value.
Definition at line 442 of file MultipleChoiceSelection.h.