Library of Assembled Shared Sources
dictionary.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @class lass::util::Dictionary
46 * @brief a wrapper around a std::map to translate keys in values and back.
47 * @author [Bramz]
48 *
49 * This utility class has been written for the purpose of translating enumeration values to
50 * strings. Enumerations are a handy concept for the programmer to enumerate different options,
51 * cases. However, they only have names in code. For the compiler these are just numbers, and if
52 * you want to use these enumerations in your user interface you again stumble on the fact that
53 * these are nothing but fancy numbers.
54 *
55 * This dictionary has been made to make it possible to translate these numbers to text. Typically
56 * you'd set up a dictionary that translates each enumeration to a string, and then you use this
57 * dictionary as an interface between your code and your user interface. It is also possible to set
58 * a default key and value that must be returned in no match can be found during the lookup. This
59 * default could indicate for an invalid state.
60 *
61 * @code
62 * #include <lass/util/dictionary.h>
63 *
64 * enum FooBar
65 * {
66 * fbInvalid,
67 * fbEggs,
68 * fbSpam,
69 * fbHam
70 * };
71 * typedef lass::util::Dictionary<std::string, FooBar> TFooBarDictionary;
72 *
73 * TFooBarDictionary fooBarDictionary;
74 * fooBarDictionary.add("eggs", fbEggs);
75 * fooBarDictionary.add("spam", fbSpam);
76 * fooBarDictionary.add("ham", fbHam);
77 * fooBarDictionary.setDefault("invalid", fbInvalid);
78 *
79 * // to user interface
80 * //
81 * FooBar fooBar = fbEggs;
82 * LASS_COUT << fooBarDictionary.key(fooBar);
83 *
84 * // from user interface.
85 * //
86 * std::string input;
87 * LASS_CIN >> input;
88 * fooBar = fooBarDictionary[input];
89 * if (fooBar == fbInvalid)
90 * {
91 * doSomethingSpecial();
92 * }
93 * @endcode
94 *
95 * To end this discussion, we have one more suggestion to make of this dictionary a singleton.
96 * You create a singleton accessor function and you init the dictionary by using
97 * LASS_EXECUTE_BEFORE_MAIN.
98 *
99 * @code
100 * #include <lass/util/dictionary.h>
101 * #include <lass/util/singleton.h>
102 *
103 * enum FooBar
104 * {
105 * fbInvalid,
106 * fbEggs,
107 * fbSpam,
108 * fbHam
109 * };
110 * typedef lass::util::Dictionary<std::string, FooBar> TFooBarDictionary;
111 *
112 * TFooBarDictionary& fooBarDictionary()
113 * {
114 * return *lass::util::Singleton<TFooBarDictionary>::instance();
115 * }
116 *
117 * LASS_EXECUTE_BEFORE_MAIN
118 * (
119 * fooBarDictionary().add("eggs", fbEggs);
120 * fooBarDictionary().add("spam", fbSpam);
121 * fooBarDictionary().add("ham", fbHam);
122 * fooBarDictionary().setDefault("invalid", fbInvalid);
123 * )
124 *
125 * // ...
126 *
127 * LASS_COUT << fooBarDictionary().key(fbEggs);
128 * @endcode
129 */
130
131#ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_DICTIONARY_H
132#define LASS_GUARDIAN_OF_INCLUSION_UTIL_DICTIONARY_H
133
134#include "util_common.h"
135#include "call_traits.h"
136
137namespace lass
138{
139namespace util
140{
141
142template
143<
144 typename KeyType,
145 typename ValueType,
146 typename KeyLess = std::less<KeyType>,
147 typename ValueLess = std::less<ValueType>
148>
149class Dictionary
150{
151public:
152
153 typedef KeyType TKey;
154 typedef ValueType TValue;
155 typedef KeyLess TKeyLess;
156 typedef ValueLess TValueLess;
157 typedef std::set<KeyType, KeyLess> TKeys;
158 typedef std::set<ValueType, ValueLess> TValues;
159 typedef typename CallTraits<KeyType>::TParam TKeyParam;
160 typedef typename CallTraits<ValueType>::TParam TValueParam;
161
162 Dictionary();
163 explicit Dictionary(TKeyLess iKeyLess);
164 Dictionary(TKeyLess iKeyLess, TValueLess iValueLess);
165
166 void add(const TKey& iKey, const TValue& iValue);
167 void remove(const TKey& iKey, const TValue& iValue);
168 void setDefault(const TKey& iKey, const TValue& iValue);
170 bool hasDefault() const;
171 void enableSuggestions(bool enable = true);
172
173 const TValue& operator[](TKeyParam iKey) const;
174 const TKey& key(TValueParam iValue) const;
175
176 TKeys keys() const;
177 TValues values() const;
178
179 TKeys keys(TValueParam iValue) const;
180 TValues values(TKeyParam iKey) const;
181
182 bool isKey(TKeyParam iKey) const;
183 bool isValue(TValueParam iValue) const;
184
185private:
186
187 typedef std::multimap<TKey, TValue, KeyLess> TMap;
188
189 bool equalValues(TValueParam iA, TValueParam iB) const;
190
191 TMap map_;
192 TKey defaultKey_;
193 TValue defaultValue_;
194 TKeyLess keyLess_;
195 TValueLess valueLess_;
196 bool hasDefault_;
197 bool enableSuggestions_;
198};
199
200
201
202}
203
204}
205
206#include "dictionary.inl"
207
208#endif
209
210// EOF
void remove(const TKey &iKey, const TValue &iValue)
remove a key and value from the dictionary.
bool hasDefault() const
return true if dictionary has value
void setDefault(const TKey &iKey, const TValue &iValue)
set key and value to be used as default ones.
bool isValue(TValueParam iValue) const
return true if iValue is a value of dictionary
void clearDefault()
clear default key and value.
const TKey & key(TValueParam iValue) const
look up a key by value
TValues values(TKeyParam iKey) const
return all values in the dictionary
void add(const TKey &iKey, const TValue &iValue)
add a key and value to dictionary as new entry.
TValues values() const
return all values in the dictionary
const TValue & operator[](TKeyParam iKey) const
look up a value by key
TKeys keys(TValueParam iValue) const
return all keys that have value iValue.
void enableSuggestions(bool enable=true)
if enabled, suggestions of valid keys or values will be included in the exception that is thrown in c...
bool isKey(TKeyParam iKey) const
return true if iKey is a key of dictionary
TKeys keys() const
return all keys in the dictionary
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53