library of assembled shared sources

http://lass.cocamware.com

dictionary.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 /** @class lass::util::Dictionary
00046  *  @brief a wrapper around a std::map to translate keys in values and back.
00047  *  @author [Bramz]
00048  *
00049  *  This utility class has been written for the purpose of translating enumeration values to
00050  *  strings.  Enumerations are a handy concept for the programmer to enumerate different options,
00051  *  cases.  However, they only have names in code.  For the compiler these are just numbers, and if
00052  *  you want to use these enumerations in your user interface you again stumble on the fact that
00053  *  these are nothing but fancy numbers.
00054  *
00055  *  This dictionary has been made to make it possible to translate these numbers to text.  Typically
00056  *  you'd set up a dictionary that translates each enumeration to a string, and then you use this
00057  *  dictionary as an interface between your code and your user interface.  It is also possible to set
00058  *  a default key and value that must be returned in no match can be found during the lookup.  This
00059  *  default could indicate for an invalid state.
00060  *
00061  *  @code
00062  *  #include <lass/util/dictionary.h>
00063  *
00064  *  enum FooBar
00065  *  {
00066  *      fbInvalid,
00067  *      fbEggs,
00068  *      fbSpam,
00069  *      fbHam
00070  *  };
00071  *  typedef lass::util::Dictionary<std::string, FooBar> TFooBarDictionary;
00072  *
00073  *  TFooBarDictionary fooBarDictionary;
00074  *  fooBarDictionary.add("eggs", fbEggs);
00075  *  fooBarDictionary.add("spam", fbSpam);
00076  *  fooBarDictionary.add("ham", fbHam);
00077  *  fooBarDictionary.setDefault("invalid", fbInvalid);
00078  *
00079  *  // to user interface
00080  *  //
00081  *  FooBar fooBar = fbEggs;
00082  *  LASS_COUT << fooBarDictionary.key(fooBar);
00083  *
00084  *  // from user interface.
00085  *  //
00086  *  std::string input;
00087  *  LASS_CIN >> input;
00088  *  fooBar = fooBarDictionary[input];
00089  *  if (fooBar == fbInvalid)
00090  *  {
00091  *      doSomethingSpecial();
00092  *  }
00093  *  @endcode
00094  *
00095  *  To end this discussion, we have one more suggestion to make of this dictionary a singleton.
00096  *  You create a singleton accessor function and you init the dictionary by using
00097  *  LASS_EXECUTE_BEFORE_MAIN.
00098  *
00099  *  @code
00100  *  #include <lass/util/dictionary.h>
00101  *  #include <lass/util/singleton.h>
00102  *
00103  *  enum FooBar
00104  *  {
00105  *      fbInvalid,
00106  *      fbEggs,
00107  *      fbSpam,
00108  *      fbHam
00109  *  };
00110  *  typedef lass::util::Dictionary<std::string, FooBar> TFooBarDictionary;
00111  *
00112  *  TFooBarDictionary& fooBarDictionary()
00113  *  {
00114  *      return *lass::util::Singleton<TFooBarDictionary>::instance();
00115  *  }
00116  *
00117  *  LASS_EXECUTE_BEFORE_MAIN
00118  *  (
00119  *      fooBarDictionary().add("eggs", fbEggs);
00120  *      fooBarDictionary().add("spam", fbSpam);
00121  *      fooBarDictionary().add("ham", fbHam);
00122  *      fooBarDictionary().setDefault("invalid", fbInvalid);
00123  *  )
00124  *
00125  *  // ...
00126  *
00127  *  LASS_COUT << fooBarDictionary().key(fbEggs);
00128  *  @endcode
00129  */
00130 
00131 #ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_DICTIONARY_H
00132 #define LASS_GUARDIAN_OF_INCLUSION_UTIL_DICTIONARY_H
00133 
00134 #include "util_common.h"
00135 #include "call_traits.h"
00136 
00137 namespace lass
00138 {
00139 namespace util
00140 {
00141 
00142 template
00143 <
00144     typename KeyType,
00145     typename ValueType,
00146     typename KeyLess = std::less<KeyType>,
00147     typename ValueLess = std::less<ValueType>
00148 >
00149 class Dictionary
00150 {
00151 public:
00152 
00153     typedef KeyType TKey;
00154     typedef ValueType TValue;
00155     typedef KeyLess TKeyLess;
00156     typedef ValueLess TValueLess;
00157     typedef std::set<KeyType, KeyLess> TKeys;
00158     typedef std::set<ValueType, ValueLess> TValues;
00159     typedef typename CallTraits<KeyType>::TParam TKeyParam;
00160     typedef typename CallTraits<ValueType>::TParam TValueParam;
00161 
00162     Dictionary();
00163     explicit Dictionary(TKeyLess iKeyLess);
00164     Dictionary(TKeyLess iKeyLess, TValueLess iValueLess);
00165 
00166     void add(const TKey& iKey, const TValue& iValue);
00167     void remove(const TKey& iKey, const TValue& iValue);
00168     void setDefault(const TKey& iKey, const TValue& iValue);
00169     void clearDefault();
00170     bool hasDefault() const;
00171     void enableSuggestions(bool enable = true);
00172 
00173     const TValue& operator[](TKeyParam iKey) const;
00174     const TKey& key(TValueParam iValue) const;
00175 
00176     TKeys keys() const;
00177     TValues values() const;
00178 
00179     TKeys keys(TValueParam iValue) const;
00180     TValues values(TKeyParam iKey) const;
00181 
00182     bool isKey(TKeyParam iKey) const;
00183     bool isValue(TValueParam iValue) const;
00184 
00185 private:
00186 
00187     typedef std::multimap<TKey, TValue, KeyLess> TMap;
00188 
00189     bool equalValues(TValueParam iA, TValueParam iB) const;
00190 
00191     TMap map_;
00192     TKey defaultKey_;
00193     TValue defaultValue_;
00194     TKeyLess keyLess_;
00195     TValueLess valueLess_;
00196     bool hasDefault_;
00197     bool enableSuggestions_;
00198 };
00199 
00200 
00201 
00202 }
00203 
00204 }
00205 
00206 #include "dictionary.inl"
00207 
00208 #endif
00209 
00210 // EOF

Generated on Mon Nov 10 14:20:03 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo