cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmnNamedMap.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Peter Kazanzides, Anton Deguet, Min Yang Jung
7  Created on: 2008-11-15
8 
9  (C) Copyright 2008-2013 Johns Hopkins University (JHU), All Rights Reserved.
10 
11 --- begin cisst license - do not edit ---
12 
13 This software is provided "as is" under an open source license, with
14 no warranty. The complete license can be found in license.txt and
15 http://www.cisst.org/cisst/license.txt.
16 
17 --- end cisst license ---
18 */
19 
20 #pragma once
21 
22 #ifndef _cmnNamedMap_h
23 #define _cmnNamedMap_h
24 
25 #include <map>
26 #include <vector>
27 #include <string>
28 
30 #include <cisstCommon/cmnLogger.h>
31 
38 // forward declaration
40 
53 template <class _elementType>
54 class cmnNamedMap {
55 
56 public:
58  typedef std::map<std::string, _elementType *> MapType;
59 
60  typedef typename MapType::iterator iterator;
61  typedef typename MapType::const_iterator const_iterator;
62  typedef typename MapType::reverse_iterator reverse_iterator;
63  typedef typename MapType::const_reverse_iterator const_reverse_iterator;
64 
65  typedef typename MapType::size_type size_type;
66  typedef _elementType element_type;
67 
68 protected:
71 
77 
79  std::string MapName;
80 
86 
90  inline const cmnClassServicesBase * Services(void) const {
91  return this->OwnerServices;
92  }
93 
96  }
98 
99 public:
102  cmnNamedMap(bool takesOwnership = false):
103  Map(),
104  TakesOwnership(takesOwnership),
105  MapName("undefined"),
106  OwnerServices(0)
107  {}
108 
112  cmnNamedMap(const char * mapName, bool takesOwnership = false):
113  Map(),
114  TakesOwnership(takesOwnership),
115  MapName(mapName),
116  OwnerServices(0)
117  {}
118 
119  cmnNamedMap(const std::string & mapName, bool takesOwnership = false):
120  Map(),
121  TakesOwnership(takesOwnership),
122  MapName(mapName),
123  OwnerServices(0)
124  {}
125 
131  cmnNamedMap(const std::string & mapName, const cmnGenericObject & owner,
132  bool takesOwnership = false):
133  Map(),
134  TakesOwnership(takesOwnership),
135  MapName(mapName),
136  OwnerServices(owner.Services())
137  {}
138 
141  //DeleteAll();
142  }
143 
145  inline void SetOwner(const cmnGenericObject & owner)
146  {
147  this->OwnerServices = owner.Services();
148  }
149 
153  bool AddItem(const std::string & name,
154  _elementType * item,
156 
158  _elementType * GetItem(const std::string & name, cmnLogLevel lod = CMN_LOG_LEVEL_RUN_DEBUG) const;
159 
161  bool FindItem(const std::string & itemName) const;
162 
166  bool RemoveItem(const std::string & name, cmnLogLevel lod = CMN_LOG_LEVEL_RUN_ERROR);
167 
170  void GetNames(std::vector<std::string> & placeHolder) const;
171  std::vector<std::string> GetNames(void) const;
173 
175  typedef void (_elementType::*VoidMethodPointer)(void);
176 
178  void ForEachVoid(VoidMethodPointer method);
179 
182  const MapType & GetMap(void) const {
183  return this->Map;
184  }
185  MapType & GetMap(void) {
186  return this->Map;
187  }
189 
191  void ToStream(std::ostream & outputStream) const;
192 
193  // void Cleanup(void) { ForEachVoid(&_elementType::Cleanup); } // needed?
194 
197  void DeleteAll(void);
198 
200  inline size_type size(void) const {
201  return this->Map.size();
202  }
203 
204  inline bool empty(void) const {
205  return this->Map.empty();
206  }
207 
208  inline void clear(void) {
209  this->Map.clear();
210  }
211 
212  inline iterator begin(void) {
213  return this->Map.begin();
214  }
215 
216  inline const_iterator begin(void) const {
217  return this->Map.begin();
218  }
219 
220  inline iterator end(void) {
221  return this->Map.end();
222  }
223 
224  inline const_iterator end(void) const {
225  return this->Map.end();
226  }
227 };
228 
229 
230 
231 template <class _elementType>
232 bool cmnNamedMap<_elementType>::AddItem(const std::string & name, _elementType *item, cmnLogLevel lod)
233 {
234  // check if this name already exists
235  const typename MapType::const_iterator iterator = Map.find(name);
236  if (iterator != Map.end()) {
237  if (this->Services()) {
238  CMN_LOG_CLASS(lod) << "AddItem: map \"" << MapName << "\" already contains an item named \""
239  << name << "\"." << std::endl;
240  } else {
241  CMN_LOG(lod) << "cmnNamedMap::AddItem: map \"" << MapName << "\" already contains an item named \""
242  << name << "\"." << std::endl;
243  }
244  return false;
245  }
246  Map[name] = item;
247  return true;
248 }
249 
250 template <class _elementType>
251 _elementType * cmnNamedMap<_elementType>::GetItem(const std::string & itemName, cmnLogLevel lod) const {
252  const typename MapType::const_iterator iter = Map.find(itemName);
253  if (iter != Map.end()) {
254  return iter->second;
255  } else {
256  if (this->Services()) {
257  CMN_LOG_CLASS(lod) << "GetItem: can't find \"" << itemName << "\" in map \"" << MapName << "\"" << std::endl;
258  } else {
259  CMN_LOG(lod) << "cmnNamedMap::GetItem: can't find \"" << itemName << "\" in map \"" << MapName << "\"" << std::endl;
260  }
261  return 0;
262  }
263 }
264 
265 template <class _elementType>
266 bool cmnNamedMap<_elementType>::FindItem(const std::string & itemName) const {
267  const typename MapType::const_iterator iter = Map.find(itemName);
268 
269  return (iter != Map.end());
270 }
271 
272 template <class _elementType>
273 bool cmnNamedMap<_elementType>::RemoveItem(const std::string & itemName, cmnLogLevel lod)
274 {
275  // check if this name already exists
276  const typename MapType::iterator iterator = Map.find(itemName);
277  if (iterator == Map.end()) {
278  if (this->Services()) {
279  CMN_LOG_CLASS(lod) << "RemoveItem: can't find \"" << itemName << "\" in map \"" << MapName << "\"" << std::endl;
280  } else {
281  CMN_LOG(lod) << "cmnNamedMap::RemoveItem: can't find \"" << itemName << "\" in map \"" << MapName << "\"" << std::endl;
282  }
283  return false;
284  }
285  // free memory if needed
286  if (this->TakesOwnership) {
287  delete iterator->second;
288  }
289  Map.erase(iterator);
290  return true;
291 }
292 
293 
294 template <class _elementType>
295 void cmnNamedMap<_elementType>::GetNames(std::vector<std::string> & placeHolder) const {
296  placeHolder.clear();
297  typename MapType::const_iterator iter = Map.begin();
298  const typename MapType::const_iterator end = Map.end();
299  for (;
300  iter != end;
301  ++iter) {
302  placeHolder.push_back(iter->first);
303  }
304 }
305 
306 
307 template <class _elementType>
308 std::vector<std::string> cmnNamedMap<_elementType>::GetNames(void) const {
309  std::vector<std::string> names;
310  GetNames(names);
311  return names;
312 }
313 
314 
315 template <class _elementType>
316 void cmnNamedMap<_elementType>::ForEachVoid(VoidMethodPointer method)
317 {
318  typename MapType::iterator iter;
319  const typename MapType::iterator end = Map.end();
320  for (iter = Map.begin(); iter != end; iter++) {
321  (iter->second->*method)();
322  }
323 }
324 
325 
326 template <class _elementType>
327 void cmnNamedMap<_elementType>::ToStream(std::ostream & outputStream) const
328 {
329  unsigned int counter = 0;
330  typename MapType::const_iterator iter = Map.begin();
331  const typename MapType::const_iterator end = Map.end();
332  for (;
333  iter != end;
334  ++iter, ++counter) {
335  outputStream << "- " << MapName << "[" << counter << "] (\""
336  << iter->first << "\"): " << *(iter->second) << std::endl;
337  }
338 }
339 
340 
341 template <class _elementType>
343  if (Map.empty()) return;
344  // free memory if needed
345  if (this->TakesOwnership) {
346  typename MapType::iterator iterator = Map.begin();
347  const typename MapType::iterator end = Map.end();
348  for (;
349  iterator != end;
350  ++iterator) {
351  delete iterator->second;
352  }
353  }
354  Map.clear();
355 }
356 
357 #endif // _cmnNamedMap_h
358 
cmnNamedMap(const std::string &mapName, const cmnGenericObject &owner, bool takesOwnership=false)
Definition: cmnNamedMap.h:131
#define CMN_LOG_LEVEL_RUN_ERROR
Definition: cmnLogLoD.h:62
void(_elementType::* VoidMethodPointer)(void)
Definition: cmnNamedMap.h:175
const MapType & GetMap(void) const
Definition: cmnNamedMap.h:182
void ForEachVoid(VoidMethodPointer method)
Definition: cmnNamedMap.h:316
bool RemoveItem(const std::string &name, cmnLogLevel lod=CMN_LOG_LEVEL_RUN_ERROR)
Definition: cmnNamedMap.h:273
size_t size_type
Definition: vctContainerTraits.h:35
MapType::const_reverse_iterator const_reverse_iterator
Definition: cmnNamedMap.h:63
cmnLogger::StreamBufType * GetLogMultiplexer(void) const
Definition: cmnNamedMap.h:94
Base class for high level objects.
Definition: cmnGenericObject.h:51
virtual const cmnClassServicesBase * Services(void) const =0
const cmnClassServicesBase * Services(void) const
Definition: cmnNamedMap.h:90
cmnNamedMap(const char *mapName, bool takesOwnership=false)
Definition: cmnNamedMap.h:112
bool empty(void) const
Definition: cmnNamedMap.h:204
short cmnLogLevel
Definition: cmnLogLoD.h:55
iterator begin(void)
Definition: cmnNamedMap.h:212
_elementType element_type
Definition: cmnNamedMap.h:66
std::string MapName
Definition: cmnNamedMap.h:79
Definition: cmnNamedMap.h:54
const_iterator end(void) const
Definition: cmnNamedMap.h:224
Declaration of cmnLogger amd macros for human readable logging.
std::map< std::string, _elementType * > MapType
Definition: cmnNamedMap.h:58
bool TakesOwnership
Definition: cmnNamedMap.h:76
void clear(void)
Definition: cmnNamedMap.h:208
bool AddItem(const std::string &name, _elementType *item, cmnLogLevel lod=CMN_LOG_LEVEL_RUN_ERROR)
Definition: cmnNamedMap.h:232
MapType Map
Definition: cmnNamedMap.h:70
const cmnClassServicesBase * OwnerServices
Definition: cmnNamedMap.h:85
MapType::size_type size_type
Definition: cmnNamedMap.h:65
#define CMN_LOG(lod)
Definition: cmnLogger.h:150
Defines cmnGenericObject.
size_type size(void) const
Definition: cmnNamedMap.h:200
const_iterator begin(void) const
Definition: cmnNamedMap.h:216
MapType::const_iterator const_iterator
Definition: cmnNamedMap.h:61
cmnNamedMap(const std::string &mapName, bool takesOwnership=false)
Definition: cmnNamedMap.h:119
static StreamBufType * GetMultiplexer(void)
Definition: cmnLogger.h:413
_elementType * GetItem(const std::string &name, cmnLogLevel lod=CMN_LOG_LEVEL_RUN_DEBUG) const
Definition: cmnNamedMap.h:251
bool FindItem(const std::string &itemName) const
Definition: cmnNamedMap.h:266
MapType & GetMap(void)
Definition: cmnNamedMap.h:185
MapType::reverse_iterator reverse_iterator
Definition: cmnNamedMap.h:62
Base class for class services.
Definition: cmnClassServicesBase.h:45
cmnNamedMap(bool takesOwnership=false)
Definition: cmnNamedMap.h:102
#define CMN_LOG_LEVEL_RUN_DEBUG
Definition: cmnLogLoD.h:65
~cmnNamedMap()
Definition: cmnNamedMap.h:140
MapType::iterator iterator
Definition: cmnNamedMap.h:60
#define CMN_LOG_CLASS(lod)
Definition: cmnLogger.h:95
std::vector< std::string > GetNames(void) const
Definition: cmnNamedMap.h:308
iterator end(void)
Definition: cmnNamedMap.h:220
void ToStream(std::ostream &outputStream) const
Definition: cmnNamedMap.h:327
void SetOwner(const cmnGenericObject &owner)
Definition: cmnNamedMap.h:145
void DeleteAll(void)
Definition: cmnNamedMap.h:342