assoc_storage.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_ASSOC_STORAGE_H
8 #define NUKLEI_ASSOC_STORAGE_H
9 
10 
11 #include <nuklei/simple_map.h>
12 #include <nuklei/Definitions.h>
13 
14 namespace nuklei {
15 
16  template<typename T>
18  {
19  typedef simple_map<id_t, T> map_t;
20  typedef typename map_t::KeyValuePair KeyValuePair;
21 
22  assoc_storage() : counter_(minKey_) {}
23 
24  KeyValuePair& new_element(const id_t key)
25  {
26  NUKLEI_TRACE_BEGIN();
27  NUKLEI_ASSERT(key >= minKey_);
28  KeyValuePair v(key, T());
29  map_.insert(key, T());
30  return map_.find(key);
31  NUKLEI_TRACE_END();
32  }
33 
34  KeyValuePair& new_element()
35  {
36  NUKLEI_TRACE_BEGIN();
37  return new_element(counter_++);
38  NUKLEI_TRACE_END();
39  }
40 
41  bool has_key(const id_t key) const
42  {
43  NUKLEI_TRACE_BEGIN();
44  return map_.has_key(key);
45  NUKLEI_TRACE_END();
46  }
47 
48  T& operator[](const id_t key)
49  {
50  NUKLEI_TRACE_BEGIN();
51  return map_[key];
52  NUKLEI_TRACE_END();
53  }
54 
55  const T& operator[](const id_t key) const
56  {
57  NUKLEI_TRACE_BEGIN();
58  return map_[key];
59  NUKLEI_TRACE_END();
60  }
61 
62  void erase(const id_t key)
63  {
64  NUKLEI_TRACE_BEGIN();
65  map_.erase(key);
66  NUKLEI_TRACE_END();
67  }
68 
69  void clear()
70  {
71  map_.clear();
72  counter_ = minKey_;
73  }
74  private:
75  map_t map_;
76  id_t counter_;
77  static const id_t minKey_ = 1000;
78 
79  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
80  template<class Archive>
81  void serialize(Archive &ar, const unsigned int version)
82  {
83  ar & NUKLEI_SERIALIZATION_NVP(map_)
84  & NUKLEI_SERIALIZATION_NVP(counter_);
85  }
86  };
87 
88 }
89 
90 #endif
91 
Public namespace.
Definition: Color.cpp:9
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
Definition: assoc_storage.h:17
unsigned int id_t
Type for identifier label.
Definition: Definitions.h:35
© 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.