simple_map.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #ifndef NUKLEI_MAP_H
8 #define NUKLEI_MAP_H
9 
10 #include <map>
11 
12 #include <nuklei/Common.h>
13 #include <nuklei/Definitions.h>
15 
16 namespace nuklei {
17 
18  template<typename KeyType, typename ValueType>
19  struct simple_map
20  {
21  typedef std::map<KeyType, ValueType> map_impl;
22  typedef typename map_impl::value_type KeyValuePair;
23 
24  virtual ~simple_map() {}
25 
26  void insert(const KeyType& key, const ValueType &v)
27  {
28  NUKLEI_TRACE_BEGIN();
29  bool newKey = map_.insert(std::make_pair(key, v)).second;
30  NUKLEI_ASSERT(newKey);
31  NUKLEI_TRACE_END();
32  }
33 
34  void insert(const KeyType& key)
35  {
36  NUKLEI_TRACE_BEGIN();
37  bool newKey = map_.insert(std::make_pair(key, ValueType())).second;
38  NUKLEI_ASSERT(newKey);
39  NUKLEI_TRACE_END();
40  }
41 
42  bool has_key(const KeyType& key) const
43  {
44  NUKLEI_TRACE_BEGIN();
45  typename map_impl::const_iterator i = map_.find(key);
46  return i != map_.end();
47  NUKLEI_TRACE_END();
48  }
49 
50  KeyValuePair& find(const KeyType& key)
51  {
52  NUKLEI_TRACE_BEGIN();
53  typename map_impl::iterator i = map_.find(key);
54  NUKLEI_ASSERT(i != map_.end());
55  return *i;
56  NUKLEI_TRACE_END();
57  }
58 
59  const KeyValuePair& find(const KeyType& key) const
60  {
61  NUKLEI_TRACE_BEGIN();
62  typename map_impl::const_iterator i = map_.find(key);
63  NUKLEI_ASSERT(i != map_.end());
64  return *i;
65  NUKLEI_TRACE_END();
66  }
67 
68  ValueType& operator[](const KeyType& key)
69  {
70  NUKLEI_TRACE_BEGIN();
71  return find(key).second;
72  NUKLEI_TRACE_END();
73  }
74 
75  const ValueType& operator[](const KeyType& key) const
76  {
77  NUKLEI_TRACE_BEGIN();
78  return find(key).second;
79  NUKLEI_TRACE_END();
80  }
81 
82  void erase(const KeyType& key)
83  {
84  NUKLEI_TRACE_BEGIN();
85  unsigned erased = map_.erase(key);
86  NUKLEI_ASSERT(erased == 1);
87  NUKLEI_TRACE_END();
88  }
89 
90  void clear()
91  {
92  map_.clear();
93  }
94 
95  private:
96  map_impl map_;
97 
98  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
99  template<class Archive>
100  void serialize(Archive &ar, const unsigned int version)
101  {
102  ar & NUKLEI_SERIALIZATION_NVP(map_);
103  }
104  };
105 
106 }
107 
108 #endif
Public namespace.
Definition: Color.cpp:9
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
Definition: simple_map.h:19
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.