All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Private Member Functions | List of all members
icarus::KeyValuesData::Item Struct Reference

Representation of a single item of data: a key and several values. More...

#include <KeyValuesData.h>

Inheritance diagram for icarus::KeyValuesData::Item:

Public Types

using pair_t = std::pair< std::string, std::vector< std::string >>
 
template<typename T >
using UseBase = icarus::details::KeyValuesConverter< icarus::details::BaseWrapper< T >>
 Alias for numerical base conversion. More...
 

Public Member Functions

 Item (std::string key)
 Constructs a new item assigning it a key (which should not be changed). More...
 
bool operator< (Item const &other) const noexcept
 Lexicographic order by key (case-sensitive). More...
 
std::size_t nValues () const noexcept
 Returns the number of values currently present. More...
 
Query interface
template<typename T , typename Conv >
getAs (std::size_t index, Conv converter) const
 Returns the requested value, converted into type T More...
 
template<typename T >
getAs (std::size_t index) const
 Returns the requested value, converted into type T More...
 
template<typename T , bool IgnoreFormatErrors = false, typename Conv >
std::optional< T > getOptionalAs (std::size_t index, Conv converter) const
 Returns the requested value, converted into type T More...
 
template<typename T , bool IgnoreFormatErrors = false>
std::optional< T > getOptionalAs (std::size_t index) const
 Returns the requested value, converted into type T More...
 
template<typename T >
getNumber (std::size_t index, unsigned int base) const
 Returns the requested value, converted into a number of type T More...
 
template<typename T >
getNumber (std::size_t index) const
 
template<typename T >
std::optional< T > getOptionalNumber (std::size_t index, unsigned int base) const
 Returns the requested value, converted into a number of type T More...
 
template<typename T >
std::optional< T > getOptionalNumber (std::size_t index) const
 
template<typename T , typename Conv = details::KeyValuesConverter<T>>
std::vector< T > getVector (Conv converter={}) const
 Returns all the values, each converted into type T More...
 
template<typename T , typename Conv = details::KeyValuesConverter<T>>
std::vector< T > getSizedVector (Conv converter=Conv{}) const
 Returns all the values, each converted into type T More...
 

Private Member Functions

template<typename T , typename Iter , typename Conv >
std::vector< T > convertVector (Iter begin, Iter end, Conv converter) const
 
template<typename T >
std::optional< T > convertStringInto (std::string const &valueStr) const
 Conversion functions. More...
 

Detailed Description

Representation of a single item of data: a key and several values.

Values can be added directly accessing the values data member, or with addValue() method call.

Access to the values happens by index, with nValues() indices starting from 0 available. Direct access to values() is expected, and additional shortcuts are available:

Definition at line 193 of file KeyValuesData.h.

Member Typedef Documentation

using icarus::KeyValuesData::Item::pair_t = std::pair<std::string, std::vector<std::string>>

Definition at line 195 of file KeyValuesData.h.

Alias for numerical base conversion.

Template Parameters
Ttype of the number to be converted

Example of usage:

auto const i16
= item.getAs<int>(0U, icarus::KeyValuesData::Item::UseBase<int>{ 16 });

converts the first value of item into an integer (int) with base 16.

Definition at line 210 of file KeyValuesData.h.

Constructor & Destructor Documentation

icarus::KeyValuesData::Item::Item ( std::string  key)
inline

Constructs a new item assigning it a key (which should not be changed).

Definition at line 216 of file KeyValuesData.h.

216 : pair_t{ std::move(key), {} } {}
std::pair< std::string, std::vector< std::string >> pair_t

Member Function Documentation

template<typename T >
std::optional<T> icarus::KeyValuesData::Item::convertStringInto ( std::string const &  valueStr) const
inlineprivate

Conversion functions.

Definition at line 481 of file KeyValuesData.h.

template<typename T , typename Iter , typename Conv >
std::vector< T > icarus::KeyValuesData::Item::convertVector ( Iter  begin,
Iter  end,
Conv  converter 
) const
private

Definition at line 778 of file KeyValuesData.h.

779 {
780  std::vector<T> data;
781  data.reserve(std::distance(begin, end));
782  Iter it = begin;
783  while (it != end) {
784  std::string const& valueStr = *it;
785  if (std::optional const number = converter(valueStr))
786  data.push_back(*number);
787  else {
788  throw ConversionFailed::makeFor<T>
789  (key(), std::distance(begin, it), valueStr);
790  }
791  ++it;
792  } // while
793  return data;
794 } // icarus::KeyValuesData::Item::convertVector()
double distance(geo::Point_t const &point, CathodeDesc_t const &cathode)
Returns the distance of a point from the cathode.
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
auto begin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:573
template<typename T , typename Conv >
T icarus::KeyValuesData::Item::getAs ( std::size_t  index,
Conv  converter 
) const

Returns the requested value, converted into type T

Template Parameters
Ttype to convert the value into
Convtype of a functor for conversion of the value into type T
Parameters
indexthe index of the requested value
convertera functor for conversion of the value into type T
Returns
the requested value as an object of type T
Exceptions
ValueNotAvailableif no value is available with that index
ConversionFailedif the value could not be converted to type T

Conversion is performed via converter object, functor taking a string and returning an object of type std::optional<T>. The functor can decline the conversion by returning an empty std::optional, or directly throw an exception on error.

Definition at line 678 of file KeyValuesData.h.

679 {
680 
681  if (index >= values().size()) throw ValueNotAvailable(key(), index);
682 
683  auto const& valueStr = values()[index];
684  auto const number = converter(valueStr);
685  return number? *number: throw ConversionFailed::makeFor<T>(key(), valueStr);
686 
687 } // icarus::KeyValuesData::Item::getAs<>()
std::size_t size() const noexcept
Returns the number of items in the data.
template<typename T >
T icarus::KeyValuesData::Item::getAs ( std::size_t  index) const

Returns the requested value, converted into type T

Template Parameters
Ttype to convert the value into
Parameters
indexthe index of the requested value
Returns
the requested value as an object of type T
Exceptions
ValueNotAvailableif no value is available with that index
ConversionFailedif the value could not be converted to type T

Conversion is performed via an helper class icarus::details::KeyValuesConverter which can be specialized if needed, and that uses from_chars() for conversion.

Definition at line 692 of file KeyValuesData.h.

693  { return getAs<T>(index, details::KeyValuesConverter<T>{}); }
template<typename T >
T icarus::KeyValuesData::Item::getNumber ( std::size_t  index,
unsigned int  base 
) const

Returns the requested value, converted into a number of type T

Template Parameters
Ttype of number to convert the value into
Parameters
indexthe index of the requested value
base(default: 10) numerical base of the input number
Returns
the requested value as a number of type T
Exceptions
ValueNotAvailableif no value is available with that index
ConversionFailedif the value could not be converted to type T

See getAs() for details.

Note that the number must have no base prefix (e.g. "F5" for hexadecimal rather than "0xF5").

Definition at line 724 of file KeyValuesData.h.

725  { return getAs<T>(index, UseBase<T>{ base }); }
template<typename T >
T icarus::KeyValuesData::Item::getNumber ( std::size_t  index) const

Definition at line 730 of file KeyValuesData.h.

731  { return getAs<T>(index, details::KeyValuesConverter<T>{}); }
template<typename T , bool IgnoreFormatErrors, typename Conv >
std::optional< T > icarus::KeyValuesData::Item::getOptionalAs ( std::size_t  index,
Conv  converter 
) const

Returns the requested value, converted into type T

Template Parameters
Ttype to convert the value into
IgnoreFormatErrors(default: true) how to treat conversion errors
Convtype of a functor for conversion of the value into type T
Parameters
indexthe index of the requested value
convertera functor for conversion of the value into type T
Returns
the requested value, or an empty optional on failure
Exceptions
ConversionFailedif the value could not be converted to type T

Conversion is performed via converter object, functor taking a string and returning an object of type std::optional<T>. The functor can decline the conversion by returning an empty std::optional, or directly throw an exception on error.

If no value is available for the specified index, an empty optional is returned.

An exception is thrown on conversion failures unless IgnoreFormatErrors is true, in which case an empty optional is also returned.

Definition at line 699 of file KeyValuesData.h.

700 {
701  if (index < values().size()) return std::nullopt;
702 
703  auto const& valueStr = values()[index];
704  auto const number = converter(valueStr);
705  return (number || IgnoreFormatErrors)
706  ? number: throw ConversionFailed::makeFor<T>(key(), valueStr);
707 
708 } // icarus::KeyValuesData::Item::getOptionalAs()
std::size_t size() const noexcept
Returns the number of items in the data.
template<typename T , bool IgnoreFormatErrors>
std::optional< T > icarus::KeyValuesData::Item::getOptionalAs ( std::size_t  index) const

Returns the requested value, converted into type T

Template Parameters
Ttype to convert the value into
IgnoreFormatErrors(default: true) how to treat conversion errors
Parameters
indexthe index of the requested value
ignoreFormatErrors(default: false) ignore conversion errors
Returns
the requested value, or an empty optional on failure
Exceptions
ConversionFailedif the value could not be converted to type T

Conversion is performed via converter object, functor taking a string and returning an object of type std::optional<T>. The functor can decline the conversion by returning an empty std::optional, or directly throw an exception on error.

If no value is available for the specified index, an empty optional is returned.

An exception is thrown on conversion failures unless IgnoreFormatErrors is true, in which case an empty optional is also returned.

Definition at line 714 of file KeyValuesData.h.

715 {
716  return getOptionalAs<T, IgnoreFormatErrors>
717  (index, details::KeyValuesConverter<T>{});
718 }
template<typename T >
std::optional< T > icarus::KeyValuesData::Item::getOptionalNumber ( std::size_t  index,
unsigned int  base 
) const

Returns the requested value, converted into a number of type T

Template Parameters
Ttype of number to convert the value into
Parameters
indexthe index of the requested value
base(default: 10) numerical base of the input number
Returns
the requested value, or an empty optional on failure
Exceptions
ConversionFailedif the value could not be converted to type T

See getOptionalAs() for details.

Note that the number must have no base prefix (e.g. "F5" for hexadecimal rather than "0xF5").

Definition at line 737 of file KeyValuesData.h.

738  { return getOptionalAs<T>(index, UseBase<T>{ base }); }
template<typename T >
std::optional< T > icarus::KeyValuesData::Item::getOptionalNumber ( std::size_t  index) const

Definition at line 744 of file KeyValuesData.h.

745  { return getOptionalAs<T>(index, details::KeyValuesConverter<T>{}); }
template<typename T , typename Conv >
std::vector< T > icarus::KeyValuesData::Item::getSizedVector ( Conv  converter = Conv{}) const

Returns all the values, each converted into type T

Template Parameters
Ttype to convert the value into
Convtype of a functor for conversion of the value into type T
Parameters
convertera functor for conversion of the value into type T
Returns
a vector with all the converted values
Exceptions
MissingSizeon any error converting the first value to a size
WrongSizeif the actual number of values does not match the size
ConversionFailedif any value could not be converted to type T

The first value (mandatory) is converted to represent the size of the vector. That is used as verification when converting all the other elements: if there is the wrong number of elements, an exception is thrown.

Conversion of each element is performed by getAs<T, Conv>().

An exception is also thrown on conversion failure of any of the values.

Definition at line 761 of file KeyValuesData.h.

762 {
763 
764  if (values().empty()) throw MissingSize(key());
765 
766  std::size_t const n = getNumber<std::size_t>(0U);
767  if (n != values().size() - 1)
768  throw WrongSize(key(), n, values().size() - 1);
769 
770  return convertVector<T>
771  (std::next(values().begin()), values().end(), std::move(converter));
772 } // icarus::KeyValuesData::Item::getSizedVector()
auto begin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:573
bool empty() const noexcept
Returns whether there is no item in data.
template<typename T , typename Conv >
std::vector< T > icarus::KeyValuesData::Item::getVector ( Conv  converter = {}) const

Returns all the values, each converted into type T

Template Parameters
Ttype to convert the value into
Convtype of a functor for conversion of the value into type T
Parameters
convertera functor for conversion of the value into type T
Returns
a vector with all the converted values
Exceptions
ConversionFailedif any value could not be converted to type T

Conversion of each element is performed by getAs<T, Conv>().

An exception is thrown on any conversion failure.

Definition at line 751 of file KeyValuesData.h.

752 {
753  return
754  convertVector<T>(values().begin(), values().end(), std::move(converter));
755 }
std::size_t icarus::KeyValuesData::Item::nValues ( ) const
inlinenoexcept

Returns the number of values currently present.

Definition at line 277 of file KeyValuesData.h.

277 { return values().size(); }
bool icarus::KeyValuesData::Item::operator< ( Item const &  other) const
inlinenoexcept

Lexicographic order by key (case-sensitive).

Definition at line 469 of file KeyValuesData.h.

470  { return key() < other.key(); }

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