LibSWOC++ 1.5.14
Solid Wall of C++
Loading...
Searching...
No Matches
swoc::Lexicon< E > Class Template Reference

#include <Lexicon.h>

Collaboration diagram for swoc::Lexicon< E >:
Collaboration graph

Classes

class  base_iterator
 Common features of container iterators. More...
 
struct  Definition
 
struct  Item
 
class  name_iterator
 
struct  NameDefaultVisitor
 Visitor functor for handling NameDefault. More...
 
class  value_iterator
 
struct  ValueDefaultVisitor
 Visitor functor for handling ValueDefault. More...
 

Public Types

using Pair = detail::lexicon_pair_type<E>
 
using UnknownValueHandler = std::function<TextView(E)>
 
using UnknownNameHandler = std::function<E(TextView)>
 
using Default = std::variant<std::monostate, E, TextView, UnknownNameHandler, UnknownValueHandler>
 
using with = std::initializer_list<Pair> const &
 
using with_multi = std::initializer_list<Definition> const &
 
using const_iterator = value_iterator
 Iterator over values (each with a primary name).
 
using iterator = const_iterator
 

Public Member Functions

 Lexicon ()
 Construct empty instance.
 
 Lexicon (with_multi items, Default handler_1=Default{}, Default handler_2=Default{})
 
 Lexicon (with items, Default handler_1=Default{}, Default handler_2=Default{})
 
 Lexicon (Default handler_1, Default handler_2=Default{})
 
 Lexicon (self_type &&that)=default
 
TextView operator[] (E const &value) const
 
operator[] (TextView const &name) const
 
template<typename... Args>
self_typedefine (E value, Args &&...names)
 
self_typedefine (E value, const std::initializer_list< TextView > &names)
 
self_typedefine (const Pair &pair)
 
self_typedefine (const Definition &init)
 
self_typeset_default (Default const &handler)
 
size_t count () const
 Get the number of values with definitions.
 
const_iterator begin () const
 Iteration begin.
 
const_iterator end () const
 Iteration end.
 
name_iterator begin_names () const
 Iteration over names - every value/name pair.
 
name_iterator end_names () const
 Iteration over names - every value/name pair.
 
ByNameHelper by_names () const
 
template<typename... Args>
auto define (E value, Args &&...names) -> self_type &
 

Static Public Attributes

static constexpr auto VALUE_IDX = 0
 Index in Pair for the enumeration value.
 
static constexpr auto NAME_IDX = 1
 Index in Pair for name.
 

Protected Types

using NameDefault = std::variant<std::monostate, std::string_view, UnknownValueHandler>
 Handle providing a default name.
 
using ValueDefault = std::variant<std::monostate, E, UnknownNameHandler>
 Handle providing a default value.
 

Protected Member Functions

TextView localize (TextView const &name)
 Copy name in to local storage.
 

Protected Attributes

MemArena _arena {1024}
 Storage for names.
 
IntrusiveHashMap< typename Item::NameLinkage > _by_name
 Access by name.
 
IntrusiveHashMap< typename Item::ValueLinkage > _by_value
 Access by value.
 
NameDefault _name_default
 Name to return if no value not found.
 
ValueDefault _value_default
 Value to return if name not found.
 

Detailed Description

template<typename E>
class swoc::Lexicon< E >

A bidirectional mapping between names and enumeration values.

This is intended to be a support class to make interacting with enumerations easier for configuration and logging. Names and enumerations can then be easily and reliably interchanged. The names are case insensitive but preserving.

Each enumeration has a primary name and an arbitrary number of secondary names. When converting from an enumeration, the primary name is used. However, any of the names will be converted to the enumeration. For instance, a Lexicon for a boolean might have the primary name of TRUE be "true" with the secondary names "1", "yes", "enable". In that case converting TRUE would always be "true", while converting any of "true", "1", "yes", or "enable" would yield TRUE. This is convenient for parsing configurations to be more tolerant of input.

The constructors are a bit baroque, but this is necessary in order to be able to declare constant instances of the Lexicon. If this isn't necessary, everything that can be done via the constructor can be done with other methods. The implementation of the constructors consists entirely of calls to define and set_default, the only difference is these methods can be called on a const instance from there.

Note
All names and value must be unique across the Lexicon. All name comparisons are case insensitive.

Definition at line 88 of file Lexicon.h.

Member Typedef Documentation

◆ const_iterator

template<typename E>
using swoc::Lexicon< E >::const_iterator = value_iterator

Iterator over values (each with a primary name).

Definition at line 364 of file Lexicon.h.

◆ Default

template<typename E>
using swoc::Lexicon< E >::Default = std::variant<std::monostate, E, TextView, UnknownNameHandler, UnknownValueHandler>

A default handler.

This handles providing a default value or name for a missing name or value.

Definition at line 128 of file Lexicon.h.

◆ iterator

template<typename E>
using swoc::Lexicon< E >::iterator = const_iterator

Iterator over values.

Note
All iteration is over constant pairs, no modification is possible.

Definition at line 367 of file Lexicon.h.

◆ NameDefault

template<typename E>
using swoc::Lexicon< E >::NameDefault = std::variant<std::monostate, std::string_view, UnknownValueHandler>
protected

Handle providing a default name.

Definition at line 420 of file Lexicon.h.

◆ Pair

template<typename E>
using swoc::Lexicon< E >::Pair = detail::lexicon_pair_type<E>

An association of an enumeration value and a name. @ note Used for initializer lists that have just a primary value.

Definition at line 97 of file Lexicon.h.

◆ UnknownNameHandler

template<typename E>
using swoc::Lexicon< E >::UnknownNameHandler = std::function<E(TextView)>

A function to be called if a name is not found, to provide a default value.

Parameters
nameThe name
Returns
An enumeration value.

The name is provided and a value in the enumeration type is expected.

Definition at line 122 of file Lexicon.h.

◆ UnknownValueHandler

template<typename E>
using swoc::Lexicon< E >::UnknownValueHandler = std::function<TextView(E)>

A function to be called if a value is not found to provide a default name.

Parameters
valueThe value.
Returns
A name for the value.

The name is return by view and therefore managing the lifetime of the name is problematic. Generally it should be process lifetime, unless some other shorter lifetime can be managed without a destructor being called. Unfortunately this can't be done any better without imposing memory management costs on normal use.

Definition at line 114 of file Lexicon.h.

◆ ValueDefault

template<typename E>
using swoc::Lexicon< E >::ValueDefault = std::variant<std::monostate, E, UnknownNameHandler>
protected

Handle providing a default value.

Definition at line 422 of file Lexicon.h.

◆ with

template<typename E>
using swoc::Lexicon< E >::with = std::initializer_list<Pair> const &

Definition at line 140 of file Lexicon.h.

◆ with_multi

template<typename E>
using swoc::Lexicon< E >::with_multi = std::initializer_list<Definition> const &

Definition at line 141 of file Lexicon.h.

Constructor & Destructor Documentation

◆ Lexicon() [1/4]

template<typename E>
swoc::Lexicon< E >::Lexicon ( )

Construct empty instance.

Definition at line 596 of file Lexicon.h.

◆ Lexicon() [2/4]

template<typename E>
swoc::Lexicon< E >::Lexicon ( with_multi items,
Default handler_1 = Default{},
Default handler_2 = Default{} )
explicit

Construct with names, possible secondary values, and optional default handlers.

Parameters
itemsA list of initializers, each of which is a name and a list of values.
handler_1A default handler.
handler_2A default hander.

Each item in the intializers must be a Definition, that is a name and a list of values. The first value is the primary value and is required. Subsequent values are optional and become secondary values.

The default handlers are optional can be omitted. If so, exceptions are thrown when values or names not in the Lexicon are used. See set_default for more details.

See also
set_default.

Definition at line 598 of file Lexicon.h.

◆ Lexicon() [3/4]

template<typename E>
swoc::Lexicon< E >::Lexicon ( with items,
Default handler_1 = Default{},
Default handler_2 = Default{} )
explicit

Construct with names / value pairs, and optional default handlers.

Parameters
itemsA list of initializers, each of which is a name and a list of values.
handler_1A default handler.
handler_2A default handler.

Each item in the intializers must be a Pair, that is a name and a value.

The default handlers are optional can be omitted. If so, exceptions are thrown when values or names not in the Lexicon are used. See set_default for more details.

See also
set_default.

Definition at line 608 of file Lexicon.h.

◆ Lexicon() [4/4]

template<typename E>
swoc::Lexicon< E >::Lexicon ( Default handler_1,
Default handler_2 = Default{} )
explicit

Construct with only default values / handlers.

Parameters
handler_1A default handler.
handler_2A default handler.

handler_2 is optional can be omitted. The argument values are the same as for set_default.

See also
set_default.

Definition at line 618 of file Lexicon.h.

Member Function Documentation

◆ begin()

template<typename E>
auto swoc::Lexicon< E >::begin ( ) const

Iteration begin.

Definition at line 720 of file Lexicon.h.

◆ begin_names()

template<typename E>
name_iterator swoc::Lexicon< E >::begin_names ( ) const
inline

Iteration over names - every value/name pair.

Definition at line 377 of file Lexicon.h.

◆ by_names()

template<typename E>
ByNameHelper swoc::Lexicon< E >::by_names ( ) const
inline

Enable container iteration by name. The return value is a tempoary of indeterminate type that provides begin and end methods which return name based iterators for this. This is useful for container based iteration. E.g. to iterate over all of the value/name pairs,

for ( auto const & pair : lexicon.by_names()) {
// code
}
Returns
Temporary.

Definition at line 414 of file Lexicon.h.

◆ count()

template<typename E>
size_t swoc::Lexicon< E >::count ( ) const

Get the number of values with definitions.

Definition at line 714 of file Lexicon.h.

◆ define() [1/5]

template<typename E>
auto swoc::Lexicon< E >::define ( const Definition & init)

Define a name with a primary and secondary values.

Parameters
initThe Definition with the name and values.
Returns
this.

This defines the name, with the first value in the value list becoming the primary value and subsequent values (if any) being the secondary values. A primary value is required but secondary values are not. This is to make it possible to define all values in this style even if some do not have secondary values.

Definition at line 686 of file Lexicon.h.

◆ define() [2/5]

template<typename E>
auto swoc::Lexicon< E >::define ( const Pair & pair)

Define a name, value pair.

Parameters
pairA Pair of the name and value to define.
Returns
this.

Definition at line 680 of file Lexicon.h.

◆ define() [3/5]

template<typename E>
template<typename... Args>
self_type & swoc::Lexicon< E >::define ( E value,
Args &&... names )

Define the names for a value. The first name is the primary name. All names must be convertible to std::string_view. lexicon.define(Value, primary, [secondary, ... ]);

◆ define() [4/5]

template<typename E>
template<typename... Args>
auto swoc::Lexicon< E >::define ( E value,
Args &&... names ) -> self_type &

Definition at line 673 of file Lexicon.h.

◆ define() [5/5]

template<typename E>
auto swoc::Lexicon< E >::define ( E value,
const std::initializer_list< TextView > & names )

Define a value and names. lexicon.define(Value, { primary, [secondary, ...] });

Definition at line 652 of file Lexicon.h.

◆ end()

template<typename E>
auto swoc::Lexicon< E >::end ( ) const

Iteration end.

Definition at line 726 of file Lexicon.h.

◆ end_names()

template<typename E>
name_iterator swoc::Lexicon< E >::end_names ( ) const
inline

Iteration over names - every value/name pair.

Definition at line 382 of file Lexicon.h.

◆ localize()

template<typename E>
TextView swoc::Lexicon< E >::localize ( TextView const & name)
protected

Copy name in to local storage.

Definition at line 626 of file Lexicon.h.

◆ operator[]() [1/2]

template<typename E>
TextView swoc::Lexicon< E >::operator[] ( E const & value) const

Get the name for a value.

Parameters
valueValue to look up.
Returns
The name for value.

Definition at line 634 of file Lexicon.h.

◆ operator[]() [2/2]

template<typename E>
E swoc::Lexicon< E >::operator[] ( TextView const & name) const

Get the value for a name.

Parameters
nameName to look up.
Returns
The value for the name.

Definition at line 643 of file Lexicon.h.

◆ set_default()

template<typename E>
auto swoc::Lexicon< E >::set_default ( Default const & handler)

Set default handler.

Parameters
handlerThe handler.
Returns
this.

The handler can be of various types.

  • An enumeration value. This sets the default value handler to return that value for any name not found.
  • A string_view. The sets the default name handler to return the string_view as the name for any value not found.
  • A DefaultNameHandler. This is a functor that takes an enumeration value parameter and returns a string_view as the name for any value that is not found.
  • A DefaultValueHandler. This is a functor that takes a name as a string_view and returns an enumeration value as the value for any name that is not found.

Definition at line 692 of file Lexicon.h.

Member Data Documentation

◆ _arena

template<typename E>
MemArena swoc::Lexicon< E >::_arena {1024}
protected

Storage for names.

Definition at line 514 of file Lexicon.h.

◆ _by_name

template<typename E>
IntrusiveHashMap<typename Item::NameLinkage> swoc::Lexicon< E >::_by_name
protected

Access by name.

Definition at line 516 of file Lexicon.h.

◆ _by_value

template<typename E>
IntrusiveHashMap<typename Item::ValueLinkage> swoc::Lexicon< E >::_by_value
protected

Access by value.

Definition at line 518 of file Lexicon.h.

◆ _name_default

template<typename E>
NameDefault swoc::Lexicon< E >::_name_default
protected

Name to return if no value not found.

Definition at line 519 of file Lexicon.h.

◆ _value_default

template<typename E>
ValueDefault swoc::Lexicon< E >::_value_default
protected

Value to return if name not found.

Definition at line 520 of file Lexicon.h.

◆ NAME_IDX

template<typename E>
auto swoc::Lexicon< E >::NAME_IDX = 1
staticconstexpr

Index in Pair for name.

Definition at line 103 of file Lexicon.h.

◆ VALUE_IDX

template<typename E>
auto swoc::Lexicon< E >::VALUE_IDX = 0
staticconstexpr

Index in Pair for the enumeration value.

Definition at line 101 of file Lexicon.h.


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