storage.hh
Go to the documentation of this file.
1 
37 #ifndef LibMultiSense_details_storage_hh
38 #define LibMultiSense_details_storage_hh
39 
41 
42 #include <map>
43 #include <set>
44 
45 namespace crl {
46 namespace multisense {
47 namespace details {
48 
49  //
50  // A message storage interface
51  //
52  // Assumes a 1:1 relationship between template class and ID
53 
54  class MessageMap {
55  public:
56 
57  template<class T> void store(const T& msg) {
59 
60  Map::iterator it = m_map.find(MSG_ID(T::ID));
61  if (m_map.end() != it) {
62  it->second.destroy<T>();
63  m_map.erase(it);
64  }
65 
66  m_map[MSG_ID(T::ID)] = Holder::Create<T>(msg);
67  };
68 
69  template<class T> Status extract(T& msg) {
71 
72  Map::iterator it = m_map.find(MSG_ID(T::ID));
73  if (m_map.end() == it)
74  return Status_Error;
75 
76  it->second.extract(msg);
77  m_map.erase(it);
78 
79  return Status_Ok;
80  };
81 
82  private:
83 
84  class Holder {
85  public:
86 
87  Holder(void *r=NULL) : m_refP(r) {};
88 
89  template<class T> static Holder Create(const T& msg) {
90  return Holder(reinterpret_cast<void *>(new T(msg)));
91  };
92 
93  template<class T> void destroy() {
94  if (NULL == m_refP)
95  CRL_EXCEPTION("destroying NULL reference");
96  delete reinterpret_cast<T*>(m_refP);
97  };
98 
99  template<class T> void extract(T& msg) {
100  if (NULL == m_refP)
101  CRL_EXCEPTION("extracting NULL reference");
102  msg = *(reinterpret_cast<T*>(m_refP));
103  destroy<T>();
104  };
105 
106  private:
107  void *m_refP;
108  };
109 
110  typedef std::map<wire::IdType, Holder> Map;
111 
113  Map m_map;
114  };
115 
116  //
117  // A constant-depth cache.
118  //
119  // Up to [depth] entries will be cached. Oldest entries
120  // will be dropped on insertion once full.
121  //
122  // The age of an entry is determined by std::map<KEY,DATA>.lower_bound([min]).
123  //
124  // DATA objects are assumed to be allocated on the heap, and memory management
125  // of the object is delegated here upon insert().
126  //
127  // For *_nolock() variations, lock-management must be
128  // done by the user.
129 
130  template<class KEY, class DATA>
131  class DepthCache {
132  public:
133 
134  DepthCache(std::size_t depth, KEY min) :
135  m_depth(depth),
136  m_minimum(min) {};
137 
140 
141  typename MapType::iterator it = m_map.begin();
142  for(; it != m_map.end();) {
143  delete it->second;
144  m_map.erase(it++);
145  }
146  };
147 
149  return m_lock;
150  };
151 
152  DATA* find_nolock(KEY key) {
153  return find_(key);
154  };
155 
156  DATA* find(KEY key) {
158  return find_(key);
159  };
160 
161  void insert_nolock(KEY key, DATA* data) {
162  insert_(key, data);
163  };
164 
165  void insert(KEY key, DATA* data) {
167  insert_(key, data);
168  };
169 
170  void remove_nolock(KEY key) {
171  remove_(key);
172  };
173 
174  void remove(KEY key) {
176  remove_(key);
177  };
178 
179  private:
180 
181  typedef std::map<KEY,DATA*> MapType;
182 
183  DATA* find_(KEY key) {
184  typename MapType::iterator it = m_map.find(key);
185 
186  if (m_map.end() == it)
187  return NULL;
188  else
189  return it->second;
190  };
191 
192  void insert_(KEY key, DATA* data) {
193  if (m_map.size() == m_depth)
194  pop_oldest_();
195 
196  m_map[key] = data;
197  };
198 
199  void remove_(KEY key) {
200  typename MapType::iterator it2 = m_map.find(key);
201  if (m_map.end() != it2) {
202  delete it2->second;
203  m_map.erase(it2);
204  }
205  };
206 
207  void pop_oldest_() {
208  typename MapType::iterator it2 = m_map.lower_bound(m_minimum);
209  if (m_map.end() != it2) {
210  delete it2->second;
211  m_map.erase(it2);
212  }
213  };
214 
215  const std::size_t m_depth;
216  const KEY m_minimum; // for lower_bound()
217 
218  MapType m_map;
220  };
221 
222 }}}; // namespaces
223 
224 #endif // LibMultiSense_details_storage_hh
#define CRL_EXCEPTION(fmt,...)
Definition: Exception.hh:71
void insert(KEY key, DATA *data)
Definition: storage.hh:165
DepthCache(std::size_t depth, KEY min)
Definition: storage.hh:134
#define MSG_ID(x)
Definition: Protocol.h:248
std::map< KEY, DATA * > MapType
Definition: storage.hh:177
void insert_nolock(KEY key, DATA *data)
Definition: storage.hh:161
Definition: channel.cc:56
static CRL_CONSTEXPR Status Status_Ok
void insert_(KEY key, DATA *data)
Definition: storage.hh:192
std::map< wire::IdType, Holder > Map
Definition: storage.hh:110
static Holder Create(const T &msg)
Definition: storage.hh:89
static CRL_CONSTEXPR Status Status_Error


multisense_lib
Author(s):
autogenerated on Sat Apr 6 2019 02:16:46