TSDuck v3.40-4120
MPEG Transport Stream Toolkit
|
Representation of a set of translations between names for identifiers. More...
#include <tsNames.h>
Classes | |
struct | NameValue |
A structure used in the constructor of a Names instance. More... | |
class | RegisterExtensionFile |
A class to register additional names files to merge with the TSDuck names file. More... | |
class | Visitor |
A visitor interface class to be implemented by applications needing ranges of values. More... | |
Public Types | |
using | int_t = std::make_signed_t< uint_t > |
Signed integer type used in representations of values. | |
using | uint_t = std::uintmax_t |
Unsigned integer type used in representations of values. | |
Public Member Functions | |
Names ()=default | |
Default constructor. | |
Names (const Names &other) | |
Copy constructor. | |
Names (Names &&other) | |
Move constructor. | |
Names (std::initializer_list< NameValue > values) | |
Constructor from a variable list of string/value pairs. | |
template<typename T > requires ts::int_enum<T> | |
void | add (const UString &name, T value) |
Add a new translation. | |
template<typename T1 , typename T2 > requires ts::int_enum<T1> && ts::int_enum<T2> | |
void | add (const UString &name, T1 first, T2 last) |
Add a new translation. | |
int_t | addNewValue (const UString &name) |
Add a translation from a given name to a new unique value. | |
template<typename T > requires ts::int_enum<T> | |
UString | bitMaskNames (T value, const UString &separator=u", ", bool hexa=false, size_t hex_digit_count=0) const |
Get the names from a bit-mask value. | |
size_t | bits () const |
Get the number of significant bits in values, when specified using "Bits = nn" in a ".names" file. | |
bool | contains (const UString &name, bool case_sensitive=true, bool abbreviated=false) const |
Check if a name exists in the section. | |
template<typename T > requires ts::int_enum<T> | |
bool | contains (T value) const |
Check if a name exists for a given value. | |
bool | empty () const |
Check if the list of names is empty. | |
UString | error (const UString &name, bool case_sensitive=true, bool abbreviated=true, const UString &designator=u"name", const UString &prefix=UString()) const |
Get the error message about a name failing to match a value. | |
bool | freeRange (uint_t first, uint_t last) const |
Check if a range is free, ie no value is defined in the range. | |
template<class CONTAINER > | |
void | getAllNames (CONTAINER &names) const |
Get all possible names in a string container. | |
template<typename T > requires ts::int_enum<T> | |
bool | getValue (T &e, const UString &name, bool case_sensitive=true, bool abbreviated=true) const |
Get the value from a name. | |
bool | isSigned () const |
Check if the list of values contains negative values from a signed integral type. | |
template<typename T > requires ts::int_enum<T> | |
UString | name (T value, bool hexa=false, size_t hex_digit_count=0) const |
Get the name from a value. | |
template<typename T1 , typename T2 = uint_t> requires ts::int_enum<T1> && ts::int_enum<T2> | |
UString | name (T1 value, NamesFlags flags, T2 alternate_value=0, size_t bits=0) const |
Get a fully formatted name from a value. | |
UString | nameList (const UString &separator=u", ", const UString &in_quote=UString(), const UString &out_quote=UString()) const |
Return a comma-separated list of all possible names. | |
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. | |
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. | |
template<typename T1 , typename T2 , typename T3 = uint_t> requires ts::int_enum<T1> && ts::int_enum<T2> && ts::int_enum<T3> | |
UString | nameWithFallback (T1 value1, T2 value2, NamesFlags flags, T3 alternate_value=0, size_t bits=0) const |
Get a fully formatted name from a value, with alternate fallback value. | |
Names & | operator= (const Names &other) |
Copy assignment. | |
Names & | operator= (Names &&other) |
Move assignment. | |
const UString & | sectionName () const |
Get the section name of this instance when it was loaded from a ".names" file. | |
void | subscribe (Visitor *visitor) |
Subscribe to all new values which will be merged into this Names instance. | |
void | unsubscribe (Visitor *visitor) |
Unsubscribe from all new values which will be merged into this Names instance. | |
int_t | value (const UString &name, bool case_sensitive=true, bool abbreviated=true) const |
Get the signed value from a name. | |
size_t | visit (Visitor *visitor) const |
Get all values in this Names instance. | |
size_t | visit (Visitor *visitor, uint_t value) const |
Get all extended values of a specified value in this Names instance. | |
Static Public Member Functions | |
static UString | Format (uint_t value, const UString &name, NamesFlags flags, size_t bits, uint_t alternate_value=0) |
Format a name using flags. | |
static NamesPtr | GetSection (const UString &file_name, const UString §ion_name, bool create) |
Get the Names instance for a specified section of a ".names" file. | |
static bool | MergeFile (const UString &file_name) |
Load a ".names" file and merge its content into all loaded Names instances. | |
Static Public Attributes | |
static constexpr int_t | UNKNOWN = std::numeric_limits<int_t>::max() |
This value means "not found". | |
Representation of a set of translations between names for identifiers.
An instance of Names contains a consistent set of translations for one type of identifier. Identifiers are integer values of any integral or enumeration type. Translations can be performed in both directions, from name to identifier or from identifier to name.
When translating from name to identifier value:
When translating from identifier to name, various types of formatting are possible. See the enum class NamesFlags.
An instance of Names can be used to define values for command line arguments or XML elements and attributes.
An instance of Names can be manually constructed in the code, either using a list of name / identifier pairs in the constructor, or adding translations one by one, or both.
Example:
An instance of Names can also be constructed from a section of a ".names" file. See the method Names::MergeFile() for the designation method for ".names" files. All ".names" files are loaded in a global repository of sections.
If sections with the same name are loaded from different files, their content are merged. This is how "TSDuck extensions" can add their own identifiers to TSDuck-provided names. It is possible to query a section by name, returning a shared pointer to the corresponding instance of Names. It is also possible to directly translate a value into a name using the non-member functions NameFromSection() and NameFromSectionWithFallback().
The integer type for the identifier is the largest signed or unsigned integer in the system (in practice, this is 64 bits). To avoid making the whole class a template one, the internal storage of identifiers use the unsigned type but values can be added and retrieved in signed format. An instance is globally marked as "signed" or "unsigned". Initially, a Names instance is "unsigned". As soon as one negative value of a signed type is added, the Names instance is marked "signed" and stays so.
ts::Names::Names | ( | const Names & | other | ) |
Copy constructor.
[in] | other | Other instance to copy. |
ts::Names::Names | ( | Names && | other | ) |
Move constructor.
[in] | other | Other instance to move. |
ts::Names::Names | ( | std::initializer_list< NameValue > | values | ) |
Constructor from a variable list of string/value pairs.
[in] | values | Variable list of name/value pairs. |
Copy assignment.
[in] | other | Other instance to copy. |
Move assignment.
[in] | other | Other instance to move. |
|
inline |
Check if the list of names is empty.
|
inline |
Check if the list of values contains negative values from a signed integral type.
|
inline |
Get the number of significant bits in values, when specified using "Bits = nn" in a ".names" file.
|
inline |
Get the section name of this instance when it was loaded from a ".names" file.
|
inline |
Add a new translation.
[in] | name | A string for a symbol. |
[in] | value | The corresponding integer value. |
|
inline |
Add a new translation.
[in] | name | A string for a symbol. |
[in] | first | First value for the name. |
[in] | last | Last value for the name. |
Add a translation from a given name to a new unique value.
[in] | name | A string for a symbol. |
UNKNOWN
if no new value is available. Check if a range is free, ie no value is defined in the range.
[in] | first | First value in the range to check. |
[in] | last | Last value in the range to check. |
|
inline |
Check if a name exists in the section.
[in] | name | The string to search. |
[in] | case_sensitive | If 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. |
[in] | abbreviated | If true (the default), any non-ambiguous abbreviation is valid. If false, a full name string must be provided. |
int_t ts::Names::value | ( | const UString & | name, |
bool | case_sensitive = true , |
||
bool | abbreviated = true |
||
) | const |
Get the signed value from a name.
[in] | name | The 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] | case_sensitive | If 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. |
[in] | abbreviated | If true (the default), any non-ambiguous abbreviation is valid. If false, a full name string must be provided. |
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. bool ts::Names::getValue | ( | T & | e, |
const UString & | name, | ||
bool | case_sensitive = true , |
||
bool | abbreviated = true |
||
) | const |
Get the value from a name.
[out] | e | The value corresponding to name. 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] | name | The 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] | case_sensitive | If 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. |
[in] | abbreviated | If true (the default), any non-ambiguous abbreviation is valid. If false, a full name string must be provided. |
|
inline |
Check if a name exists for a given value.
[in] | value | Value to get the name for. |
|
inline |
Get the name from a value.
[in] | value | A value to search. |
[in] | hexa | If true and no name exists for value, return the value as an hexadecimal string with "0x" prefix instead of decimal. |
[in] | hex_digit_count | When an hexadecimal value is returned, specify the minimum number of digits. |
|
inline |
Get a fully formatted name from a value.
[in] | value | A value to search. |
[in] | flags | Presentation flags. |
[in] | alternate_value | Display this integer value if flags ALTERNATE is set. |
[in] | bits | Optional size in bits of the displayed data. Used in replacement of the "Bits=XX" directive in the .names file. |
|
inline |
Get a fully formatted name from a value, with alternate fallback value.
[in] | value1 | A value to search. |
[in] | value2 | Alternate value if no name is found for value1. |
[in] | flags | Presentation flags. |
[in] | alternate_value | Display this integer value if flags ALTERNATE is set. |
[in] | bits | Optional size in bits of the displayed data. Used in replacement of the "Bits=XX" directive in the .names file. |
|
inlinestatic |
Get the Names instance for a specified section of a ".names" file.
[in] | file_name | Name of the ".names" file. This is typically a short name. See MergeFile() for details. If empty, only search in already loaded sections. |
[in] | section_name | Name of section to search. Not case-sensitive. |
[in] | create | If true, create the section if it does not exist. |
|
inline |
Get the names from a bit-mask value.
The method is useful only when the integer values in the enumeration are bit-masks.
[in] | value | A bit-mask, built from integer values in the Names object. |
[in] | separator | The separator to be used between values, a comma by default. |
[in] | hexa | If true and no name exists for a value, insert the value as an hexadecimal string with "0x" prefix instead of decimal. |
[in] | hex_digit_count | When an hexadecimal value is returned, specify the minimum number of digits. |
|
inline |
Return a comma-separated list of all names for a list of integer values.
CONTAINER | A container class of integer values as defined by the C++ Standard Template Library (STL). |
[in] | container | A container of integer values. |
[in] | separator | The separator to be used between values, a comma by default. |
UString ts::Names::names | ( | ITERATOR | begin, |
ITERATOR | end, | ||
const UString & | separator = u", " |
||
) | const |
Return a comma-separated list of all names for a list of integer values.
The values are accessed through iterators in a container.
ITERATOR | An iterator class over integer values as defined by the C++ Standard Template Library (STL). |
[in] | begin | An iterator pointing to the first value. |
[in] | end | An iterator pointing after the last value. |
[in] | separator | The separator to be used between values, a comma by default. |
void ts::Names::getAllNames | ( | CONTAINER & | names | ) | const |
Get all possible names in a string container.
CONTAINER | A container class of strings as defined by the C++ Standard Template Library (STL). |
[out] | names | A container of strings. |
UString ts::Names::nameList | ( | const UString & | separator = u", " , |
const UString & | in_quote = UString() , |
||
const UString & | out_quote = UString() |
||
) | const |
Return a comma-separated list of all possible names.
[in] | separator | The separator to be used between values, a comma by default. |
[in] | in_quote | Opening quote for each name. |
[in] | out_quote | Closing quote for each name. |
UString ts::Names::error | ( | const UString & | name, |
bool | case_sensitive = true , |
||
bool | abbreviated = true , |
||
const UString & | designator = u"name" , |
||
const UString & | prefix = UString() |
||
) | const |
Get the error message about a name failing to match a value.
[in] | name | The string to search. |
[in] | case_sensitive | If false, the search is not case sensitive. |
[in] | abbreviated | If true, any non-ambiguous abbreviation is valid. |
[in] | designator | How to designate the name in the message (e.g. "name", "command", "option"). |
[in] | prefix | Prefix to prepend each candidate in case of ambiguous name. |
size_t ts::Names::visit | ( | Visitor * | visitor | ) | const |
Get all extended values of a specified value in this Names instance.
In a ".names" file, all sections shall have a nominal width, "Bits=8" for instance. However, when the section has "Extended=true", "extended" values can be provided. With "Bits=8", the value 0x00AA, 0x01AA, or 0xFFAA, are all extended values for the base 8-bit value 0xAA, as an example.
[in,out] | visitor | An instance of a subclass of Visitor which receives all extended values for value. |
[in] | value | The base value to get extended values for. |
void ts::Names::subscribe | ( | Visitor * | visitor | ) |
void ts::Names::unsubscribe | ( | Visitor * | visitor | ) |
|
static |
Format a name using flags.
[in] | value | Value for the name. |
[in] | name | Name for the value. If empty, there is no name for value. |
[in] | flags | Presentation flags. |
[in] | bits | Nominal size in bits of the data. |
[in] | alternate_value | Display this integer value instead of value if flag ALTERNATE is set. |
|
inlinestatic |
Load a ".names" file and merge its content into all loaded Names instances.
[in] | file_name | Configuration file name. Typically without directory name. Without directory, the file is automatically searched in the TSDuck configuration directory. If the file is not found, try with ".names" suffix and then "tsduck." prefix. For instance, when file_name is "foo", the configuration directories are searched for files "foo", "foo.names" and "tsduck.foo.names" until one is found. |
This value means "not found".
It is returned by methods which search a signed integer value.