userver: utils::DefaultDict< ValueType > Class Template Reference
Loading...
Searching...
No Matches
utils::DefaultDict< ValueType > Class Template Referencefinal

#include <userver/utils/default_dict.hpp>

Detailed Description

template<typename ValueType>
class utils::DefaultDict< ValueType >

Dictionary that for missing keys falls back to a default value stored by key utils::kDefaultDictDefaultName.

Usually used by dynamic configs for providing a fallback or default value along with the specific values. For example:

{
".htm": "text/html",
".html": "text/html",
"__default__": "text/plain"
}

Definition at line 42 of file default_dict.hpp.

Public Types

using DictType = utils::impl::TransparentMap<std::string, ValueType>
 
using const_iterator = typename DictType::const_iterator
 
using iterator = const_iterator
 
using value_type = typename DictType::value_type
 
using key_type = std::string
 
using mapped_type = ValueType
 
using init_list = std::initializer_list<std::pair<std::string_view, ValueType>>
 

Public Member Functions

 DefaultDict (init_list contents)
 
 DefaultDict (DictType dict)
 
 DefaultDict (std::string name, init_list contents)
 
 DefaultDict (std::string name, DictType dict)
 
bool HasDefaultValue () const noexcept
 
bool HasValue (std::string_view key) const noexcept
 Returns true if *this has key, otherwise returns false.
 
const ValueType & GetDefaultValue () const
 
const ValueType & operator[] (std::string_view key) const
 
template<typename StringType >
const ValueType & operator[] (const std::optional< StringType > &key) const
 Returns key ? Get(*key) : GetDefaultValue()
 
const ValueType & Get (std::string_view key) const
 
template<typename StringType >
const ValueType & Get (const std::optional< StringType > &key) const
 Returns key ? Get(*key) : GetDefaultValue()
 
std::optional< ValueType > GetOptional (std::string_view key) const
 
void SetDefault (ValueType value)
 
template<typename StringType >
void Set (StringType &&key, ValueType value)
 
auto begin () const noexcept
 
auto end () const noexcept
 
const std::string & GetName () const noexcept
 
bool operator== (const DefaultDict &r) const noexcept
 
bool operator!= (const DefaultDict &r) const noexcept
 

Member Typedef Documentation

◆ const_iterator

template<typename ValueType >
using utils::DefaultDict< ValueType >::const_iterator = typename DictType::const_iterator

Definition at line 45 of file default_dict.hpp.

◆ DictType

template<typename ValueType >
using utils::DefaultDict< ValueType >::DictType = utils::impl::TransparentMap<std::string, ValueType>

Definition at line 44 of file default_dict.hpp.

◆ init_list

template<typename ValueType >
using utils::DefaultDict< ValueType >::init_list = std::initializer_list<std::pair<std::string_view, ValueType>>

Definition at line 50 of file default_dict.hpp.

◆ iterator

template<typename ValueType >
using utils::DefaultDict< ValueType >::iterator = const_iterator

Definition at line 46 of file default_dict.hpp.

◆ key_type

template<typename ValueType >
using utils::DefaultDict< ValueType >::key_type = std::string

Definition at line 48 of file default_dict.hpp.

◆ mapped_type

template<typename ValueType >
using utils::DefaultDict< ValueType >::mapped_type = ValueType

Definition at line 49 of file default_dict.hpp.

◆ value_type

template<typename ValueType >
using utils::DefaultDict< ValueType >::value_type = typename DictType::value_type

Definition at line 47 of file default_dict.hpp.

Constructor & Destructor Documentation

◆ DefaultDict() [1/4]

template<typename ValueType >
utils::DefaultDict< ValueType >::DefaultDict ( init_list contents)
inline

Definition at line 54 of file default_dict.hpp.

◆ DefaultDict() [2/4]

template<typename ValueType >
utils::DefaultDict< ValueType >::DefaultDict ( DictType dict)
inline

Definition at line 56 of file default_dict.hpp.

◆ DefaultDict() [3/4]

template<typename ValueType >
utils::DefaultDict< ValueType >::DefaultDict ( std::string name,
init_list contents )
inline

Definition at line 58 of file default_dict.hpp.

◆ DefaultDict() [4/4]

template<typename ValueType >
utils::DefaultDict< ValueType >::DefaultDict ( std::string name,
DictType dict )
inline

Definition at line 61 of file default_dict.hpp.

Member Function Documentation

◆ begin()

template<typename ValueType >
auto utils::DefaultDict< ValueType >::begin ( ) const
inlinenoexcept

Definition at line 139 of file default_dict.hpp.

◆ end()

template<typename ValueType >
auto utils::DefaultDict< ValueType >::end ( ) const
inlinenoexcept

Definition at line 141 of file default_dict.hpp.

◆ Get() [1/2]

template<typename ValueType >
template<typename StringType >
const ValueType & utils::DefaultDict< ValueType >::Get ( const std::optional< StringType > & key) const
inline

Returns key ? Get(*key) : GetDefaultValue()

Definition at line 107 of file default_dict.hpp.

◆ Get() [2/2]

template<typename ValueType >
const ValueType & utils::DefaultDict< ValueType >::Get ( std::string_view key) const
inline

Definition at line 103 of file default_dict.hpp.

◆ GetDefaultValue()

template<typename ValueType >
const ValueType & utils::DefaultDict< ValueType >::GetDefaultValue ( ) const
inline

Returns value by utils::kDefaultDictDefaultName key, throws a std::runtime_error exception.

Definition at line 74 of file default_dict.hpp.

◆ GetName()

template<typename ValueType >
const std::string & utils::DefaultDict< ValueType >::GetName ( ) const
inlinenoexcept

Definition at line 143 of file default_dict.hpp.

◆ GetOptional()

template<typename ValueType >
std::optional< ValueType > utils::DefaultDict< ValueType >::GetOptional ( std::string_view key) const
inline

Returns value by key if it is in *this, otherwise returns value by utils::kDefaultDictDefaultName if it is in *this, otherwise returns an empty optional.

Definition at line 114 of file default_dict.hpp.

◆ HasDefaultValue()

template<typename ValueType >
bool utils::DefaultDict< ValueType >::HasDefaultValue ( ) const
inlinenoexcept

Returns true if *this has a utils::kDefaultDictDefaultName key, otherwise returns false.

Definition at line 65 of file default_dict.hpp.

◆ HasValue()

template<typename ValueType >
bool utils::DefaultDict< ValueType >::HasValue ( std::string_view key) const
inlinenoexcept

Returns true if *this has key, otherwise returns false.

Definition at line 68 of file default_dict.hpp.

◆ operator!=()

template<typename ValueType >
bool utils::DefaultDict< ValueType >::operator!= ( const DefaultDict< ValueType > & r) const
inlinenoexcept

Definition at line 147 of file default_dict.hpp.

◆ operator==()

template<typename ValueType >
bool utils::DefaultDict< ValueType >::operator== ( const DefaultDict< ValueType > & r) const
inlinenoexcept

Definition at line 145 of file default_dict.hpp.

◆ operator[]() [1/2]

template<typename ValueType >
template<typename StringType >
const ValueType & utils::DefaultDict< ValueType >::operator[] ( const std::optional< StringType > & key) const
inline

Returns key ? Get(*key) : GetDefaultValue()

Definition at line 98 of file default_dict.hpp.

◆ operator[]() [2/2]

template<typename ValueType >
const ValueType & utils::DefaultDict< ValueType >::operator[] ( std::string_view key) const
inline

Returns value by key if it is in *this, otherwise returns value by utils::kDefaultDictDefaultName if it is in *this, otherwise throws a std::runtime_error exception.

Definition at line 85 of file default_dict.hpp.

◆ Set()

template<typename ValueType >
template<typename StringType >
void utils::DefaultDict< ValueType >::Set ( StringType && key,
ValueType value )
inline

Sets a mapping. key == utils::kDefaultDictDefaultName is allowed.

The function is primarily there for testing purposes - DefaultDict is normally obtained by parsing the config.

Definition at line 135 of file default_dict.hpp.

◆ SetDefault()

template<typename ValueType >
void utils::DefaultDict< ValueType >::SetDefault ( ValueType value)
inline

Sets the default value.

The function is primarily there for testing purposes - DefaultDict is normally obtained by parsing the config.

Definition at line 128 of file default_dict.hpp.


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