Library of Assembled Shared Sources
|
a wrapper around a std::map to translate keys in values and back. More...
#include <dictionary.h>
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 | |
a wrapper around a std::map to translate keys in values and back.
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.
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.
Definition at line 149 of file dictionary.h.
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().
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().
const Dictionary< K, V, KL, VL >::TValue & lass::util::Dictionary< K, V, KL, VL >::operator[] | ( | TKeyParam | iKey | ) | const |
look up a value by key
values(iKey)
. if | no 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[]().
const Dictionary< K, V, KL, VL >::TKey & lass::util::Dictionary< K, V, KL, VL >::key | ( | TValueParam | iValue | ) | const |
look up a key by value
keys(iValue)
. if | no 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().