OpenMS  2.8.0
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-2021.
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: Timo Sachsenberg$
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
38 #include <OpenMS/config.h>
39 
40 #include <map>
41 
42 namespace OpenMS
43 {
49  template <class Key, class T>
50  class Map :
51  private 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 
88  using Base::erase;
89  using Base::size;
90  using Base::begin;
91  using Base::rbegin;
92  using Base::end;
93  using Base::rend;
94  using Base::clear;
95  using Base::insert;
96  using Base::find;
97  using Base::empty;
98  using Base::count;
99 
100  using typename Base::iterator;
101  using typename Base::const_iterator;
102 
103  using typename Base::mapped_type;
104  using typename Base::value_type;
106 
108  inline bool has(const Key& key) const
109  {
110  return Base::find(key) != Base::end();
111  }
112 
118  const T& operator[](const Key& key) const;
119 
121  T& operator[](const Key& key);
122 
123  inline bool equals(const Map<Key, T>& other) const
124  {
125  return operator==(
126  static_cast<typename Map<Key, T>::Base>(*this),
127  static_cast<typename Map<Key, T>::Base>(other)
128  );
129  }
130 
131  };
132 
133  //******************************************************************************************
134  // Implementations of template methods
135  //******************************************************************************************
136 
137  template <class Key, class T>
138  const T& Map<Key, T>::operator[](const Key& key) const
139  {
140  ConstIterator it = this->find(key);
141  if (it == Base::end())
142  {
143  throw IllegalKey(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
144  }
145  else
146  {
147  return it->second;
148  }
149  }
150 
151  template <class Key, class T>
152  T& Map<Key, T>::operator[](const Key& key)
153  {
154  return this->try_emplace(key).first->second;
155  }
156 
157 } // namespace OPENMS
158 
Exception base class.
Definition: Exception.h:91
BaseException() noexcept
Default constructor.
Map illegal key exception.
Definition: Map.h:65
IllegalKey(const char *file, int line, const char *function)
Definition: Map.h:67
Map class based on the STL map (containing several convenience functions)
Definition: Map.h:52
bool has(const Key &key) const
Test whether the map contains the given key.
Definition: Map.h:108
Key KeyType
Definition: Map.h:78
std::map< Key, T > Base
Definition: Map.h:76
bool equals(const Map< Key, T > &other) const
Definition: Map.h:123
Base::const_reverse_iterator ConstReverseIterator
Definition: Map.h:83
Base::const_iterator ConstIterator
Definition: Map.h:81
Base::reverse_iterator ReverseIterator
Definition: Map.h:82
Base::iterator Iterator
Definition: Map.h:80
const T & operator[](const Key &key) const
Return a constant reference to the element whose key is key.
Definition: Map.h:138
Base::value_type * PointerType
Definition: Map.h:79
Base::value_type ValueType
Definition: Map.h:77
T & operator[](const Key &key)
Return a mutable reference to the element whose key is key. If an element with the key key does not e...
Definition: Map.h:152
bool operator==(const IDBoostGraph::ProteinGroup &lhs, const IDBoostGraph::ProteinGroup &rhs)
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47