Library of Assembled Shared Sources
lass::util::Dictionary< KeyType, ValueType, KeyLess, ValueLess > Class Template Reference

a wrapper around a std::map to translate keys in values and back. More...

#include <dictionary.h>

Inheritance diagram for lass::util::Dictionary< KeyType, ValueType, KeyLess, ValueLess >:

Public Member Functions

void add (const TKey &iKey, const TValue &iValue)
 add a key and value to dictionary as new entry.
 
void remove (const TKey &iKey, const TValue &iValue)
 remove a key and value from the dictionary.
 
void setDefault (const TKey &iKey, const TValue &iValue)
 set key and value to be used as default ones.
 
void clearDefault ()
 clear default key and value.
 
bool hasDefault () const
 return true if dictionary has value
 
void enableSuggestions (bool enable=true)
 if enabled, suggestions of valid keys or values will be included in the exception that is thrown in case a key or value is not found and there's no default.
 
const TValue & operator[] (TKeyParam iKey) const
 look up a value by key
 
const TKey & key (TValueParam iValue) const
 look up a key by value
 
TKeys keys () const
 return all keys in the dictionary
 
TValues values () const
 return all values in the dictionary
 
TKeys keys (TValueParam iValue) const
 return all keys that have value iValue.
 
TValues values (TKeyParam iKey) const
 return all values in the dictionary
 
bool isKey (TKeyParam iKey) const
 return true if iKey is a key of dictionary
 
bool isValue (TValueParam iValue) const
 return true if iValue is a value of dictionary
 

Detailed Description

template<typename KeyType, typename ValueType, typename KeyLess = std::less<KeyType>, typename ValueLess = std::less<ValueType>>
class lass::util::Dictionary< KeyType, ValueType, KeyLess, ValueLess >

a wrapper around a std::map to translate keys in values and back.

Author
[Bramz]

This utility class has been written for the purpose of translating enumeration values to strings. Enumerations are a handy concept for the programmer to enumerate different options, cases. However, they only have names in code. For the compiler these are just numbers, and if you want to use these enumerations in your user interface you again stumble on the fact that these are nothing but fancy numbers.

This dictionary has been made to make it possible to translate these numbers to text. Typically you'd set up a dictionary that translates each enumeration to a string, and then you use this dictionary as an interface between your code and your user interface. It is also possible to set a default key and value that must be returned in no match can be found during the lookup. This default could indicate for an invalid state.

enum FooBar
{
fbInvalid,
fbEggs,
fbSpam,
fbHam
};
TFooBarDictionary fooBarDictionary;
fooBarDictionary.add("eggs", fbEggs);
fooBarDictionary.add("spam", fbSpam);
fooBarDictionary.add("ham", fbHam);
fooBarDictionary.setDefault("invalid", fbInvalid);
// to user interface
//
FooBar fooBar = fbEggs;
LASS_COUT << fooBarDictionary.key(fooBar);
// from user interface.
//
std::string input;
LASS_CIN >> input;
fooBar = fooBarDictionary[input];
if (fooBar == fbInvalid)
{
doSomethingSpecial();
}
a wrapper around a std::map to translate keys in values and back.
Definition dictionary.h:150
void add(const TKey &iKey, const TValue &iValue)
add a key and value to dictionary as new entry.
#define LASS_COUT
return reference to 'cout' proxy stream of the lass::io::ProxyMan singleton.
Definition io_fwd.h:69
#define LASS_CIN
return reference to 'cin' proxy stream of the lass::io::ProxyMan singleton.
Definition io_fwd.h:81

To end this discussion, we have one more suggestion to make of this dictionary a singleton. You create a singleton accessor function and you init the dictionary by using LASS_EXECUTE_BEFORE_MAIN.

enum FooBar
{
fbInvalid,
fbEggs,
fbSpam,
fbHam
};
TFooBarDictionary& fooBarDictionary()
{
}
LASS_EXECUTE_BEFORE_MAIN
(
fooBarDictionary().add("eggs", fbEggs);
fooBarDictionary().add("spam", fbSpam);
fooBarDictionary().add("ham", fbHam);
fooBarDictionary().setDefault("invalid", fbInvalid);
)
// ...
LASS_COUT << fooBarDictionary().key(fbEggs);
static TInstance * instance()
Return pointer to singleton instance.
Definition singleton.inl:82

Definition at line 149 of file dictionary.h.

Member Function Documentation

◆ setDefault()

template<typename K, typename V, typename KL, typename VL>
void lass::util::Dictionary< K, V, KL, VL >::setDefault ( const TKey & iKey,
const TValue & iValue )

set key and value to be used as default ones.

Once default key and value are set, these will be returned on occasions you try to look up a key or value but it can't be found.

Definition at line 132 of file dictionary.inl.

References setDefault().

Referenced by setDefault().

◆ clearDefault()

template<typename K, typename V, typename KL, typename VL>
void lass::util::Dictionary< K, V, KL, VL >::clearDefault ( )

clear default key and value.

If no default key and value are set, and exception will be thrown on occasions you try to look up a key or value but it can't be found.

Definition at line 146 of file dictionary.inl.

References clearDefault().

Referenced by clearDefault().

◆ operator[]()

template<typename K, typename V, typename KL, typename VL>
const Dictionary< K, V, KL, VL >::TValue & lass::util::Dictionary< K, V, KL, VL >::operator[] ( TKeyParam iKey) const

look up a value by key

Returns
  • the value that belongs to the key.
  • if such a value can not be found and an default value is set, then this default will be returned.
Note
if multiple entries with the same key are found, only one is returned. It is not specified which one this will be. If you want all entries with this key, use values(iKey).
Exceptions
ifno value can be found that belongs to the key and no default value is set, then an exception will be thrown.

Definition at line 188 of file dictionary.inl.

References keys(), and operator[]().

Referenced by operator[]().

◆ key()

template<typename K, typename V, typename KL, typename VL>
const Dictionary< K, V, KL, VL >::TKey & lass::util::Dictionary< K, V, KL, VL >::key ( TValueParam iValue) const

look up a key by value

Returns
  • the key to which the value belongs
  • if such a key can not be found and an default key is set, then this default will be returned.
Note
if multiple entries with the same value are found, only one is returned. It is not specified which one this will be. If you want all entries with this value, use keys(iValue).
Exceptions
ifno key can be found to wich the value belongs and no default key is set, then an exception will be thrown.

Definition at line 225 of file dictionary.inl.

References key(), and values().

Referenced by key().


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