TSDuck Version 3.16-1064 (TSDuck - The MPEG Transport Stream Toolkit)
ts::Enumeration Class Reference

Enumeration of int/string pairs. More...

Classes

struct  NameValue
 A structure used in the constructor of an Enumeration. More...
 

Public Types

typedef std::multimap< int, UString >::const_iterator const_iterator
 A constant iterator type for the content of the object. More...
 

Public Member Functions

 Enumeration ()
 Default constructor.
 
 Enumeration (const std::initializer_list< NameValue > values)
 Constructor from a variable list of string/int pairs. More...
 
virtual ~Enumeration ()
 Virtual destructor.
 
void add (const UString &name, int value)
 Add a new enumeration value. More...
 
const_iterator begin () const
 Return an iterator pointing to the first element of this object. More...
 
bool empty () const
 Check if the enumeration is empty. More...
 
const_iterator end () const
 Return an iterator pointing after the last element of this object. More...
 
template<typename ENUM , typename std::enable_if< std::is_enum< ENUM >::value >::type * = nullptr>
bool getValue (ENUM &e, const UString &name, bool caseSensitive=true) const
 Get the enumeration value from a name, abbreviation allowed. More...
 
UString name (int value, bool hexa=false, size_t hexDigitCount=0) const
 Get the name from a value. More...
 
UString nameList (const UString &separator=u", ", const UString &inQuote=UString(), const UString &outQuote=UString()) const
 Return a comma-separated list of all possible names. More...
 
template<class CONTAINER >
UString names (const CONTAINER &container, const UString &separator=u", ") const
 Return a comma-separated list of all names for a list of integer values. More...
 
template<class ITERATOR >
UString names (ITERATOR begin, ITERATOR end, const UString &separator=u", ") const
 Return a comma-separated list of all names for a list of integer values. More...
 
bool operator!= (const Enumeration &other) const
 Unequality operator. More...
 
bool operator== (const Enumeration &other) const
 Equality operator. More...
 
size_t size () const
 Get the number of entries in the enumeration. More...
 
int value (const UString &name, bool caseSensitive=true) const
 Get the value from a name, abbreviation allowed. More...
 

Static Public Attributes

static const int UNKNOWN
 This value means "not found". More...
 

Detailed Description

Enumeration of int/string pairs.

This class is used to manage enumeration values. Here, by enumeration, we mean an association between strings and integers. The strings are manipulated as external values (command line parameters, report output, etc.) and the integers are manipulated as internal values. This class performs the association between these internal and external values.

Some interesting features are:

  • When provided as input, the string values can be abbreviated up to the shortest unambiguous string.
  • The strings can be case sensitive or not.
  • Several strings may have the same value.

Member Typedef Documentation

◆ const_iterator

A constant iterator type for the content of the object.

An iterator points to a pair of integer and string representing an element of the enumeration or more formally to a std::pair <int, UString>.

Sample usage:

for (ts::Enumeration::const_iterator it = e.begin(); it != e.end(); ++it) {
const int value (it->first); // the int value
const UString name (it->second); // the corresponding name
....
}

Constructor & Destructor Documentation

◆ Enumeration()

ts::Enumeration::Enumeration ( const std::initializer_list< NameValue values)

Constructor from a variable list of string/int pairs.

Parameters
[in]valuesVariable list of name/value pairs.

Member Function Documentation

◆ operator==()

bool ts::Enumeration::operator== ( const Enumeration other) const
inline

Equality operator.

Parameters
[in]otherAnother instance to compare with.
Returns
True if this object has the same content as other, false otherwise.

◆ operator!=()

bool ts::Enumeration::operator!= ( const Enumeration other) const
inline

Unequality operator.

Parameters
[in]otherAnother instance to compare with.
Returns
True if this object does not have the same content as other, false otherwise.

◆ size()

size_t ts::Enumeration::size ( ) const
inline

Get the number of entries in the enumeration.

Returns
The number of entries in the enumeration.

◆ empty()

bool ts::Enumeration::empty ( ) const
inline

Check if the enumeration is empty.

Returns
True if the enumeration is empty.

◆ add()

void ts::Enumeration::add ( const UString name,
int  value 
)
inline

Add a new enumeration value.

Parameters
[in]nameA string for a symbol.
[in]valueThe corresponding integer value.

◆ value()

int ts::Enumeration::value ( const UString name,
bool  caseSensitive = true 
) const

Get the value from a name, abbreviation allowed.

Parameters
[in]nameThe string to search. This string may also contain an integer value in decimal or hexadecimal representation in which case this integer value is returned.
[in]caseSensitiveIf false, the search is not case sensitive and name may match an equivalent string with distinct letter case. If true (the default), an exact match is required.
Returns
The integer value corresponding to name or UNKNOWN if not found or ambiguous, unless name can be interpreted as an integer value. If multiple integer values were registered with the same name, one of them is returned but which one is returned is unspecified.

◆ getValue()

template<typename ENUM , typename std::enable_if< std::is_enum< ENUM >::value >::type * = nullptr>
bool ts::Enumeration::getValue ( ENUM &  e,
const UString name,
bool  caseSensitive = true 
) const
inline

Get the enumeration value from a name, abbreviation allowed.

Template Parameters
ENUMAn enumeration type.
Parameters
[out]eThe enumeration value. Unmodified if name is not valid. If multiple integer values were registered with the same name, one of them is returned but which one is returned is unspecified.
[in]nameThe string to search. This string may also contain an integer value in decimal or hexadecimal representation in which case this integer value is returned.
[in]caseSensitiveIf false, the search is not case sensitive and name may match an equivalent string with distinct letter case. If true (the default), an exact match is required.
Returns
True on success, false if name is not found or ambiguous, unless name can be interpreted as an integer value.

◆ name()

UString ts::Enumeration::name ( int  value,
bool  hexa = false,
size_t  hexDigitCount = 0 
) const

Get the name from a value.

Parameters
[in]valueAn integer value to search.
[in]hexaIf true and no name exists for value, return the value as an hexadecimal string with "0x" prefix instead of decimal.
[in]hexDigitCountWhen an hexadecimal value is returned, specify the minimum number of digits.
Returns
The corresponding string or a numeric representation of value if not found. If several names were registered with the same value, one of them is returned but which one is returned is unspecified.

◆ names() [1/2]

template<class CONTAINER >
UString ts::Enumeration::names ( const CONTAINER &  container,
const UString separator = u", " 
) const
inline

Return a comma-separated list of all names for a list of integer values.

Template Parameters
CONTAINERA container class of integer values as defined by the C++ Standard Template Library (STL).
Parameters
[in]containerA container of integer values.
[in]separatorThe separator to be used between values, a comma by default.
Returns
A comma-separated list of the names for the integer values in container. Each value is formatted according to name().

◆ names() [2/2]

template<class ITERATOR >
UString ts::Enumeration::names ( ITERATOR  begin,
ITERATOR  end,
const UString separator = u", " 
) const
inline

Return a comma-separated list of all names for a list of integer values.

The values are accessed through iterators in a container.

Template Parameters
ITERATORAn iterator class over integer values as defined by the C++ Standard Template Library (STL).
Parameters
[in]beginAn iterator pointing to the first value.
[in]endAn iterator pointing after the last value.
[in]separatorThe separator to be used between values, a comma by default.
Returns
A comma-separated list of the names for the integer values in container. Each value is formatted according to name().

◆ nameList()

UString ts::Enumeration::nameList ( const UString separator = u", ",
const UString inQuote = UString(),
const UString outQuote = UString() 
) const

Return a comma-separated list of all possible names.

Parameters
[in]separatorThe separator to be used between values, a comma by default.
[in]inQuoteOpening quote for each name.
[in]outQuoteClosing quote for each name.
Returns
A comma-separated list of all possible names.

◆ begin()

const_iterator ts::Enumeration::begin ( ) const
inline

Return an iterator pointing to the first element of this object.

Returns
An iterator pointing to the first element of this object.

◆ end()

const_iterator ts::Enumeration::end ( ) const
inline

Return an iterator pointing after the last element of this object.

Returns
An iterator pointing after the last element of this object.

Member Data Documentation

◆ UNKNOWN

const int ts::Enumeration::UNKNOWN
static

This value means "not found".

It is returned by methods which search an integer value.


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