Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
Map.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2013.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Stephan Aiche$
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_DATASTRUCTURES_MAP_H
36 #define OPENMS_DATASTRUCTURES_MAP_H
37 
39 
40 #include <map>
41 
42 namespace OpenMS
43 {
49  template <class Key, class T>
50  class Map :
51  public std::map<Key, T>
52  {
53 public:
54 
63  class IllegalKey :
65  {
66 public:
67  IllegalKey(const char * file, int line, const char * function) :
68  Exception::BaseException(file, line, function)
69  {
70  }
71 
72  };
73 
75 
76  typedef std::map<Key, T> Base;
77  typedef typename Base::value_type ValueType;
78  typedef Key KeyType;
79  typedef typename Base::value_type * PointerType;
80  typedef typename Base::iterator Iterator;
81  typedef typename Base::const_iterator ConstIterator;
82  typedef typename Base::reverse_iterator ReverseIterator;
83  typedef typename Base::const_reverse_iterator ConstReverseIterator;
85 
87  inline bool has(const Key & key) const
88  {
89  return Base::find(key) != Base::end();
90  }
91 
97  const T & operator[](const Key & key) const;
98 
100  T & operator[](const Key & key);
101  };
102 
103  //******************************************************************************************
104  // Implementations of template methods
105  //******************************************************************************************
106 
107  template <class Key, class T>
108  const T & Map<Key, T>::operator[](const Key & key) const
109  {
110  ConstIterator it = this->find(key);
111  if (it == Base::end())
112  {
113  throw IllegalKey(__FILE__, __LINE__, __PRETTY_FUNCTION__);
114  }
115  else
116  {
117  return it->second;
118  }
119  }
120 
121  template <class Key, class T>
122  T & Map<Key, T>::operator[](const Key & key)
123  {
124  Iterator it = this->find(key);
125  if (it == Base::end())
126  {
127  it = this->insert(ValueType(key, T())).first;
128  }
129  return it->second;
130  }
131 
132 } // namespace OPENMS
133 
134 #endif // OPENMS_DATASTRUCTURES_MAP_H
BaseException()
Default constructor.
Base::value_type * PointerType
Definition: Map.h:79
Base::iterator Iterator
Definition: Map.h:80
bool has(const Key &key) const
Test whether the map contains the given key.
Definition: Map.h:87
Base::const_reverse_iterator ConstReverseIterator
Definition: Map.h:83
Base::reverse_iterator ReverseIterator
Definition: Map.h:82
Map illegal key exception.
Definition: Map.h:63
Key KeyType
Definition: Map.h:78
const T & operator[](const Key &key) const
Return a constant reference to the element whose key is key.
Definition: Map.h:108
Exception base class.
Definition: Exception.h:90
Base::const_iterator ConstIterator
Definition: Map.h:81
std::map< Key, T > Base
Definition: Map.h:76
IllegalKey(const char *file, int line, const char *function)
Definition: Map.h:67
Map class based on the STL map (containing serveral convenience functions)
Definition: Map.h:50
Base::value_type ValueType
Definition: Map.h:77

OpenMS / TOPP release 1.11.1 Documentation generated on Thu Nov 14 2013 11:19:16 using doxygen 1.8.5