LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
util::details::MultipleChoiceSelectionOption_t< Choices > Class Template Reference

Class representing one of the available options to be selected. More...

#include "MultipleChoiceSelection.h"

Public Types

using Choices_t = Choices
 
using Option_t = MultipleChoiceSelectionOption_t< Choices_t >
 

Public Member Functions

template<typename... Aliases>
 MultipleChoiceSelectionOption_t (Choices_t value, std::string name, Aliases...aliases)
 Constructor: assigns value, name and aliases. More...
 
template<typename... Aliases>
std::enable_if_t< AllConvertibleToStrings_v< Aliases... >, Option_t & > addAlias (std::string alias, Aliases...moreAliases)
 Adds aliases. More...
 
bool match (std::string const &label) const
 Returns whether this option matches the specified label (name or alias). More...
 
Choices_t value () const
 Returns a copy of the value of the option. More...
 
std::string name () const
 Returns the name of the option (i.e. the main label). More...
 
std::vector< std::string > const & labels () const
 Returns an iterable object with all the labels of the option. More...
 
 operator std::string () const
 Returns a string representing this option. More...
 
 operator Choices_t () const
 Returns the value of this option. More...
 
std::string value_as_string () const
 
std::string value_as_string (std::string const &defValue) const
 Returns a string represent the value of the option, or defValue. More...
 
std::string dump () const
 Returns in a string the name and all the aliases. More...
 
bool operator== (Option_t const &option) const
 Returns whether the two options are the same (same value and name). More...
 
bool operator!= (Option_t const &option) const
 Returns whether the two options are not the same. More...
 
template<typename... Aliases>
auto addAlias (std::string alias, Aliases...moreAliases) -> std::enable_if_t< AllConvertibleToStrings_v< Aliases... >, Option_t & >
 

Static Public Member Functions

static std::optional< std::string > value_as_string (Choices_t value)
 Converts a value of type Choices_t into a string, if possible. More...
 
static std::string value_as_string (Choices_t value, std::string const &defValue)
 Converts a value of type Choices_t into a string, if possible. More...
 

Private Types

using Comparer_t = CaseInsensitiveComparer
 

Static Private Member Functions

static bool equal (std::string const &a, std::string const &b)
 

Private Attributes

Choices_t fValue
 The value associated to the option. More...
 
std::vector< std::string > fLabels
 All the labels. More...
 

Detailed Description

template<typename Choices>
class util::details::MultipleChoiceSelectionOption_t< Choices >

Class representing one of the available options to be selected.

An option has a value (of type Choices_t) and a name as a string. It may also have aliases. The identity of the option is defined by the value: two option objects with the same value represent the same option.

Matching a label is encoded in this class: the option matches a label if its name or any of its aliases matches the proposed label in a case-insensitive comparison.

Definition at line 72 of file MultipleChoiceSelection.h.

Member Typedef Documentation

template<typename Choices>
using util::details::MultipleChoiceSelectionOption_t< Choices >::Choices_t = Choices

Definition at line 77 of file MultipleChoiceSelection.h.

template<typename Choices>
using util::details::MultipleChoiceSelectionOption_t< Choices >::Comparer_t = CaseInsensitiveComparer
private

Definition at line 74 of file MultipleChoiceSelection.h.

Definition at line 78 of file MultipleChoiceSelection.h.

Constructor & Destructor Documentation

template<typename Choices >
template<typename... Aliases>
util::details::MultipleChoiceSelectionOption_t< Choices >::MultipleChoiceSelectionOption_t ( Choices_t  value,
std::string  name,
Aliases...  aliases 
)

Constructor: assigns value, name and aliases.

Definition at line 580 of file MultipleChoiceSelection.h.

References util::details::MultipleChoiceSelectionOption_t< Choices >::addAlias(), and util::details::MultipleChoiceSelectionOption_t< Choices >::fLabels.

584  : fValue(value)
585 {
586  fLabels.reserve(1U + sizeof...(aliases));
587  addAlias(std::move(name), std::move(aliases)...);
588 } // util::details::MultipleChoiceSelectionOption_t<>::MultipleChoiceSelectionOption_t
std::enable_if_t< AllConvertibleToStrings_v< Aliases... >, Option_t & > addAlias(std::string alias, Aliases...moreAliases)
Adds aliases.
Choices_t fValue
The value associated to the option.
std::vector< std::string > fLabels
All the labels.
std::string name() const
Returns the name of the option (i.e. the main label).
Choices_t value() const
Returns a copy of the value of the option.

Member Function Documentation

template<typename Choices>
template<typename... Aliases>
std::enable_if_t<AllConvertibleToStrings_v<Aliases...>, Option_t&> util::details::MultipleChoiceSelectionOption_t< Choices >::addAlias ( std::string  alias,
Aliases...  moreAliases 
)
template<typename Choices>
template<typename... Aliases>
auto util::details::MultipleChoiceSelectionOption_t< Choices >::addAlias ( std::string  alias,
Aliases...  moreAliases 
) -> std::enable_if_t<AllConvertibleToStrings_v<Aliases...>, Option_t&>

Definition at line 593 of file MultipleChoiceSelection.h.

References util::details::MultipleChoiceSelectionOption_t< Choices >::addAlias(), and util::details::MultipleChoiceSelectionOption_t< Choices >::fLabels.

596 {
597  fLabels.push_back(std::move(alias));
598  if constexpr (sizeof...(moreAliases) > 0)
599  return addAlias(std::move(moreAliases)...);
600  else
601  return *this;
602 } // util::details::MultipleChoiceSelectionOption_t<>::addAlias()
std::enable_if_t< AllConvertibleToStrings_v< Aliases... >, Option_t & > addAlias(std::string alias, Aliases...moreAliases)
Adds aliases.
STL namespace.
std::vector< std::string > fLabels
All the labels.
template<typename Choices >
std::string util::details::MultipleChoiceSelectionOption_t< Choices >::dump ( ) const

Returns in a string the name and all the aliases.

Definition at line 647 of file MultipleChoiceSelection.h.

References util::details::MultipleChoiceSelectionOption_t< Choices >::fLabels, and util::details::MultipleChoiceSelectionOption_t< Choices >::value_as_string().

648 {
649  auto iLabel = fLabels.begin();
650  auto const lend = fLabels.end();
651  std::string s{'"'};
652  s += *iLabel;
653  s += '"';
654  auto const valueStr = value_as_string();
655  if (valueStr != *iLabel) {
656  s += " [=";
657  s += valueStr;
658  s += "]";
659  }
660  if (++iLabel != lend) {
661  s += " (aliases: \"";
662  s += *iLabel;
663  s += '"';
664  while (++iLabel != lend) {
665  s += " \"";
666  s += *iLabel;
667  s += '"';
668  } // while
669  s += ')';
670  } // if aliases
671 
672  return s;
673 } // util::details::MultipleChoiceSelectionOption_t<>::dump()
std::vector< std::string > fLabels
All the labels.
template<typename Choices>
static bool util::details::MultipleChoiceSelectionOption_t< Choices >::equal ( std::string const &  a,
std::string const &  b 
)
inlinestaticprivate

Definition at line 142 of file MultipleChoiceSelection.h.

Referenced by util::details::MultipleChoiceSelectionOption_t< Choices >::match().

143  {
144  return Comparer_t::equal(a, b);
145  }
static bool equal(std::string const &a, std::string const &b)
Returns whether strings a and b are equal.
template<typename Choices>
std::vector<std::string> const& util::details::MultipleChoiceSelectionOption_t< Choices >::labels ( ) const
inline

Returns an iterable object with all the labels of the option.

Definition at line 100 of file MultipleChoiceSelection.h.

Referenced by util::MultipleChoiceSelection< Choices >::addOption().

100 { return fLabels; }
std::vector< std::string > fLabels
All the labels.
template<typename Choices >
bool util::details::MultipleChoiceSelectionOption_t< Choices >::match ( std::string const &  label) const

Returns whether this option matches the specified label (name or alias).

Definition at line 606 of file MultipleChoiceSelection.h.

References util::details::MultipleChoiceSelectionOption_t< Choices >::equal(), and util::details::MultipleChoiceSelectionOption_t< Choices >::fLabels.

Referenced by util::details::operator!=(), and util::details::operator==().

607 {
608  return std::find_if(fLabels.begin(), fLabels.end(), [&label](std::string const& alias) {
609  return equal(label, alias);
610  }) != fLabels.end();
611 } // util::details::MultipleChoiceSelectionOption_t<>::match()
std::vector< std::string > fLabels
All the labels.
static bool equal(std::string const &a, std::string const &b)
template<typename Choices>
std::string util::details::MultipleChoiceSelectionOption_t< Choices >::name ( ) const
inline

Returns the name of the option (i.e. the main label).

Definition at line 97 of file MultipleChoiceSelection.h.

Referenced by util::details::MultipleChoiceSelectionOption_t< Choices >::operator!=(), util::details::operator<<(), util::details::MultipleChoiceSelectionOption_t< Choices >::operator==(), and util::details::MultipleChoiceSelectionOption_t< Choices >::value_as_string().

97 { return labels().front(); }
std::vector< std::string > const & labels() const
Returns an iterable object with all the labels of the option.
template<typename Choices>
util::details::MultipleChoiceSelectionOption_t< Choices >::operator Choices_t ( ) const
inline

Returns the value of this option.

Definition at line 106 of file MultipleChoiceSelection.h.

References value.

106 { return value(); }
Choices_t value() const
Returns a copy of the value of the option.
template<typename Choices>
util::details::MultipleChoiceSelectionOption_t< Choices >::operator std::string ( ) const
inline

Returns a string representing this option.

Definition at line 103 of file MultipleChoiceSelection.h.

103 { return name(); }
std::string name() const
Returns the name of the option (i.e. the main label).
template<typename Choices>
bool util::details::MultipleChoiceSelectionOption_t< Choices >::operator!= ( Option_t const &  option) const
inline

Returns whether the two options are not the same.

Definition at line 127 of file MultipleChoiceSelection.h.

References util::details::CaseInsensitiveComparer::equal(), util::details::MultipleChoiceSelectionOption_t< Choices >::name(), value, and util::details::MultipleChoiceSelectionOption_t< Choices >::value().

128  {
129  return (value() != option.value()) || !equal(name(), option.name());
130  }
std::string name() const
Returns the name of the option (i.e. the main label).
static bool equal(std::string const &a, std::string const &b)
Choices_t value() const
Returns a copy of the value of the option.
template<typename Choices>
bool util::details::MultipleChoiceSelectionOption_t< Choices >::operator== ( Option_t const &  option) const
inline

Returns whether the two options are the same (same value and name).

Definition at line 121 of file MultipleChoiceSelection.h.

References util::details::CaseInsensitiveComparer::equal(), util::details::MultipleChoiceSelectionOption_t< Choices >::name(), value, and util::details::MultipleChoiceSelectionOption_t< Choices >::value().

122  {
123  return (value() == option.value()) && equal(name(), option.name());
124  }
std::string name() const
Returns the name of the option (i.e. the main label).
static bool equal(std::string const &a, std::string const &b)
Choices_t value() const
Returns a copy of the value of the option.
template<typename Choices >
std::string util::details::MultipleChoiceSelectionOption_t< Choices >::value_as_string ( ) const

Returns a string representing the value of the option.

That will be the name() of the option if the value is not convertible to a string.

Definition at line 623 of file MultipleChoiceSelection.h.

References util::details::MultipleChoiceSelectionOption_t< Choices >::name().

Referenced by util::MultipleChoiceSelection< Choices >::addAlias(), util::details::MultipleChoiceSelectionOption_t< Choices >::dump(), util::MultipleChoiceSelection< Choices >::get(), and util::details::MultipleChoiceSelectionOption_t< Choices >::value_as_string().

624 {
625  return value_as_string(name());
626 }
std::string name() const
Returns the name of the option (i.e. the main label).
template<typename Choices >
std::string util::details::MultipleChoiceSelectionOption_t< Choices >::value_as_string ( std::string const &  defValue) const

Returns a string represent the value of the option, or defValue.

Definition at line 615 of file MultipleChoiceSelection.h.

References util::details::MultipleChoiceSelectionOption_t< Choices >::value(), and util::details::MultipleChoiceSelectionOption_t< Choices >::value_as_string().

617 {
618  return value_as_string(value(), defValue);
619 }
Choices_t value() const
Returns a copy of the value of the option.
template<typename Choices >
std::optional< std::string > util::details::MultipleChoiceSelectionOption_t< Choices >::value_as_string ( Choices_t  value)
static

Converts a value of type Choices_t into a string, if possible.

Definition at line 630 of file MultipleChoiceSelection.h.

632 {
633  return details::ValueToString<std::decay_t<Choices_t>>::convert(value);
634 } // util::details::MultipleChoiceSelectionOption_t<>::value_as_string()
Choices_t value() const
Returns a copy of the value of the option.
template<typename Choices >
std::string util::details::MultipleChoiceSelectionOption_t< Choices >::value_as_string ( Choices_t  value,
std::string const &  defValue 
)
static

Converts a value of type Choices_t into a string, if possible.

Definition at line 638 of file MultipleChoiceSelection.h.

References util::details::MultipleChoiceSelectionOption_t< Choices >::value_as_string().

641 {
642  return value_as_string(value).value_or(defValue);
643 } // util::details::MultipleChoiceSelectionOption_t<>::value_as_string()
Choices_t value() const
Returns a copy of the value of the option.

Member Data Documentation

template<typename Choices>
Choices_t util::details::MultipleChoiceSelectionOption_t< Choices >::fValue
private

The value associated to the option.

Definition at line 139 of file MultipleChoiceSelection.h.


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