cisst-saw
Loading...
Searching...
No Matches
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-2026 Johns Hopkins University (JHU), All Rights Reserved.
10
11--- begin cisst license - do not edit ---
12
13This software is provided "as is" under an open source license, with
14no warranty. The complete license can be found in license.txt and
15http://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
31
36
37
38// forward declaration
40
53template <class _elementType>
55
56public:
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
68protected:
71
77
79 std::string MapName;
80
86
90 inline const cmnClassServicesBase * Services(void) const {
91 return this->OwnerServices;
92 }
93
97
98
99public:
102 cmnNamedMap(bool takesOwnership = false):
103 Map(),
104 TakesOwnership(takesOwnership),
105 MapName("undefined"),
107 {}
108
112 cmnNamedMap(const char * mapName, bool takesOwnership = false):
113 Map(),
114 TakesOwnership(takesOwnership),
115 MapName(mapName),
117 {}
118
119 cmnNamedMap(const std::string & mapName, bool takesOwnership = false):
120 Map(),
121 TakesOwnership(takesOwnership),
122 MapName(mapName),
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
179
182 const MapType & GetMap(void) const {
183 return this->Map;
184 }
185 MapType & GetMap(void) {
186 return this->Map;
187 }
188
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
231template <class _elementType>
232bool 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
250template <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
265template <class _elementType>
266bool 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
272template <class _elementType>
273bool 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
294template <class _elementType>
295void 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
307template <class _elementType>
308std::vector<std::string> cmnNamedMap<_elementType>::GetNames(void) const {
309 std::vector<std::string> names;
310 GetNames(names);
311 return names;
312}
313
314
315template <class _elementType>
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
326template <class _elementType>
327void 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
341template <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
Base class for class services.
Definition cmnClassServicesBase.h:46
Base class for high level objects.
Definition cmnGenericObject.h:53
virtual const cmnClassServicesBase * Services(void) const =0
static StreamBufType * GetMultiplexer(void)
Definition cmnLogger.h:420
cmnLODMultiplexerStreambuf< char > StreamBufType
Definition cmnLogger.h:246
void ToStream(std::ostream &outputStream) const
Definition cmnNamedMap.h:327
_elementType element_type
Definition cmnNamedMap.h:66
MapType::iterator iterator
Definition cmnNamedMap.h:60
bool AddItem(const std::string &name, _elementType *item, cmnLogLevel lod=CMN_LOG_LEVEL_RUN_ERROR)
Definition cmnNamedMap.h:232
iterator begin(void)
Definition cmnNamedMap.h:212
MapType & GetMap(void)
Definition cmnNamedMap.h:185
cmnLogger::StreamBufType * GetLogMultiplexer(void) const
Definition cmnNamedMap.h:94
cmnNamedMap(const std::string &mapName, const cmnGenericObject &owner, bool takesOwnership=false)
Definition cmnNamedMap.h:131
bool RemoveItem(const std::string &name, cmnLogLevel lod=CMN_LOG_LEVEL_RUN_ERROR)
Definition cmnNamedMap.h:273
size_type size(void) const
Definition cmnNamedMap.h:200
std::map< std::string, _elementType * > MapType
Definition cmnNamedMap.h:58
std::vector< std::string > GetNames(void) const
Definition cmnNamedMap.h:308
void(_elementType::*) VoidMethodPointer(void)
Definition cmnNamedMap.h:175
cmnNamedMap(bool takesOwnership=false)
Definition cmnNamedMap.h:102
void GetNames(std::vector< std::string > &placeHolder) const
Definition cmnNamedMap.h:295
const_iterator begin(void) const
Definition cmnNamedMap.h:216
iterator end(void)
Definition cmnNamedMap.h:220
std::string MapName
Definition cmnNamedMap.h:79
~cmnNamedMap()
Definition cmnNamedMap.h:140
bool FindItem(const std::string &itemName) const
Definition cmnNamedMap.h:266
const cmnClassServicesBase * OwnerServices
Definition cmnNamedMap.h:85
cmnNamedMap(const char *mapName, bool takesOwnership=false)
Definition cmnNamedMap.h:112
MapType::const_iterator const_iterator
Definition cmnNamedMap.h:61
_elementType * GetItem(const std::string &name, cmnLogLevel lod=CMN_LOG_LEVEL_RUN_DEBUG) const
Definition cmnNamedMap.h:251
const_iterator end(void) const
Definition cmnNamedMap.h:224
MapType Map
Definition cmnNamedMap.h:70
const cmnClassServicesBase * Services(void) const
Definition cmnNamedMap.h:90
MapType::size_type size_type
Definition cmnNamedMap.h:65
void clear(void)
Definition cmnNamedMap.h:208
cmnNamedMap(const std::string &mapName, bool takesOwnership=false)
Definition cmnNamedMap.h:119
MapType::reverse_iterator reverse_iterator
Definition cmnNamedMap.h:62
const MapType & GetMap(void) const
Definition cmnNamedMap.h:182
bool empty(void) const
Definition cmnNamedMap.h:204
MapType::const_reverse_iterator const_reverse_iterator
Definition cmnNamedMap.h:63
bool TakesOwnership
Definition cmnNamedMap.h:76
void SetOwner(const cmnGenericObject &owner)
Definition cmnNamedMap.h:145
void ForEachVoid(VoidMethodPointer method)
Definition cmnNamedMap.h:316
Defines cmnGenericObject.
#define CMN_LOG_LEVEL_RUN_DEBUG
Definition cmnLogLoD.h:65
short cmnLogLevel
Definition cmnLogLoD.h:55
#define CMN_LOG_LEVEL_RUN_ERROR
Definition cmnLogLoD.h:62
Declaration of cmnLogger amd macros for human readable logging.
#define CMN_LOG(lod)
Definition cmnLogger.h:150
#define CMN_LOG_CLASS(lod)
Definition cmnLogger.h:95