npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
ConstSubscriptMap.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_CONSTSUBSCRIPTMAP_HH_
2 #define NPSTAT_CONSTSUBSCRIPTMAP_HH_
3 
4 /*!
5 // \file ConstSubscriptMap.hh
6 //
7 // \brief A variation of std::map with const subscripting operator
8 //
9 // Author: I. Volobouev
10 //
11 // August 2012
12 */
13 
14 #include <map>
15 #include <stdexcept>
16 
17 namespace npstat {
18  template <class Key, class T,
19  class Compare = std::less<Key>,
20  class Allocator = std::allocator<std::pair<const Key,T> > >
21  struct ConstSubscriptMap : public std::map<Key,T,Compare,Allocator>
22  {
23  inline T& operator[](const Key&);
24  inline const T& operator[](const Key&) const;
25  };
26 
27  template<class Key,class T,class Compare,class Allocator>
29  {
31  if (it == std::map<Key,T,Compare,Allocator>::end()) throw std::invalid_argument(
32  "In npstat::ConstSubscriptMap::operator[]: key not found");
33  return const_cast<T&>(it->second);
34  }
35 
36  template<class Key,class T,class Compare,class Allocator>
37  inline const T& ConstSubscriptMap<Key,T,Compare,Allocator>::operator[](const Key& key) const
38  {
39  typename ConstSubscriptMap<Key,T,Compare,Allocator>::const_iterator it = this->find(key);
40  if (it == std::map<Key,T,Compare,Allocator>::end()) throw std::invalid_argument(
41  "In npstat::ConstSubscriptMap::operator[]: key not found");
42  return it->second;
43  }
44 }
45 
46 #endif // NPSTAT_CONSTSUBSCRIPTMAP_HH_
Definition: AbsArrayProjector.hh:14
Definition: ConstSubscriptMap.hh:22