DART 6.7.3
Loading...
Searching...
No Matches
NameManager.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2019, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef DART_COMMON_DETAIL_NAMEMANAGER_HPP_
34#define DART_COMMON_DETAIL_NAMEMANAGER_HPP_
35
36#include <sstream>
37#include <cassert>
40
41namespace dart {
42namespace common {
43
44//==============================================================================
45template <class T>
46NameManager<T>::NameManager(const std::string& _managerName,
47 const std::string& _defaultName)
48 : mManagerName(_managerName),
49 mDefaultName(_defaultName),
50 mNameBeforeNumber(true),
51 mPrefix(""), mInfix("("), mAffix(")")
52{
53 // Do nothing
54}
55
56//==============================================================================
57template <class T>
58bool NameManager<T>::setPattern(const std::string& _newPattern)
59{
60 std::size_t name_start = _newPattern.find("%s");
61 std::size_t number_start = _newPattern.find("%d");
62
63 if(name_start == std::string::npos || number_start == std::string::npos)
64 return false;
66 if(name_start < number_start)
67 mNameBeforeNumber = true;
68 else
69 mNameBeforeNumber = false;
70
71 std::size_t prefix_end = std::min(name_start, number_start);
72 std::size_t infix_end = std::max(name_start, number_start);
73
74 mPrefix = _newPattern.substr(0, prefix_end);
75 mInfix = _newPattern.substr(prefix_end+2, infix_end-prefix_end-2);
76 mAffix = _newPattern.substr(infix_end+2);
77
78 return true;
79}
80
81//==============================================================================
82template <class T>
83std::string NameManager<T>::issueNewName(const std::string& _name) const
84{
85 if(!hasName(_name))
86 return _name;
87
88 int count = 1;
89 std::string newName;
90 do
91 {
92 std::stringstream ss;
93 if(mNameBeforeNumber)
94 ss << mPrefix << _name << mInfix << count++ << mAffix;
95 else
96 ss << mPrefix << count++ << mInfix << _name << mAffix;
97 newName = ss.str();
98 } while (hasName(newName));
99
100 dtmsg << "[NameManager::issueNewName] (" << mManagerName << ") The name ["
101 << _name << "] is a duplicate, so it has been renamed to ["
102 << newName << "]\n";
103
104 return newName;
105}
106
107//==============================================================================
108template <class T>
109std::string NameManager<T>::issueNewNameAndAdd(const std::string& _name,
110 const T& _obj)
111{
112 const std::string& checkEmpty = _name.empty()? mDefaultName : _name;
113 const std::string& newName = issueNewName(checkEmpty);
114 addName(newName, _obj);
115
116 return newName;
117}
118
119//==============================================================================
120template <class T>
121bool NameManager<T>::addName(const std::string& _name, const T& _obj)
122{
123 if (_name.empty())
125 dtwarn << "[NameManager::addName] (" << mManagerName
126 << ") Empty name is not allowed!\n";
127 return false;
128 }
129
130 if (hasName(_name))
131 {
132 dtwarn << "[NameManager::addName] (" << mManagerName << ") The name ["
133 << _name << "] already exists!\n";
134 return false;
135 }
136
137 mMap.insert(std::pair<std::string, T>(_name, _obj));
138 mReverseMap.insert(std::pair<T, std::string>(_obj, _name));
139
140 assert(mReverseMap.size() == mMap.size());
142 return true;
143}
145//==============================================================================
146template <class T>
147bool NameManager<T>::removeName(const std::string& _name)
148{
149 assert(mReverseMap.size() == mMap.size());
150
151 typename std::map<std::string, T>::iterator it = mMap.find(_name);
152
153 if (it == mMap.end())
154 return false;
155
156 typename std::map<T, std::string>::iterator rit =
157 mReverseMap.find(it->second);
158
159 if (rit != mReverseMap.end())
160 mReverseMap.erase(rit);
161
162 mMap.erase(it);
163
164 return true;
165}
166
167//==============================================================================
168template <class T>
170{
171 assert(mReverseMap.size() == mMap.size());
172
173 typename std::map<T, std::string>::iterator rit = mReverseMap.find(_obj);
174
175 if (rit == mReverseMap.end())
176 return false;
177
178 typename std::map<std::string, T>::iterator it = mMap.find(rit->second);
179 if (it != mMap.end())
180 mMap.erase(it);
181
182 mReverseMap.erase(rit);
183
184 return true;
185}
186
187//==============================================================================
188template <class T>
189void NameManager<T>::removeEntries(const std::string& _name, const T& _obj)
190{
191 removeObject(_obj);
192 removeName(_name);
193}
194
195//==============================================================================
196template <class T>
198{
199 mMap.clear();
200 mReverseMap.clear();
201}
202
203//==============================================================================
204template <class T>
205bool NameManager<T>::hasName(const std::string& _name) const
206{
207 return (mMap.find(_name) != mMap.end());
208}
209
210//==============================================================================
211template <class T>
212bool NameManager<T>::hasObject(const T& _obj) const
213{
214 return (mReverseMap.find(_obj) != mReverseMap.end());
215}
216
217//==============================================================================
218template <class T>
219std::size_t NameManager<T>::getCount() const
220{
221 return mMap.size();
222}
223
224//==============================================================================
225template <class T>
226T NameManager<T>::getObject(const std::string& _name) const
227{
228 typename std::map<std::string, T>::const_iterator result =
229 mMap.find(_name);
230
231 if (result != mMap.end())
232 return result->second;
233 else
234 return nullptr;
235}
236
237//==============================================================================
238template <class T>
239std::string NameManager<T>::getName(const T& _obj) const
240{
241 assert(mReverseMap.size() == mMap.size());
242
243 typename std::map<T, std::string>::const_iterator result =
244 mReverseMap.find(_obj);
245
246 if (result != mReverseMap.end())
247 return result->second;
248 else
249 return "";
250}
251
252//==============================================================================
253template <class T>
254std::string NameManager<T>::changeObjectName(const T& _obj,
255 const std::string& _newName)
256{
257 assert(mReverseMap.size() == mMap.size());
258
259 typename std::map<T, std::string>::iterator rit = mReverseMap.find(_obj);
260 if(rit == mReverseMap.end())
261 return _newName;
262
263 if(rit->second == _newName)
264 return rit->second;
265
266 removeName(rit->second);
267 return issueNewNameAndAdd(_newName, _obj);
268}
269
270//==============================================================================
271template <class T>
272void NameManager<T>::setDefaultName(const std::string& _defaultName)
273{
274 mDefaultName = _defaultName;
275}
276
277//==============================================================================
278template <class T>
279const std::string& NameManager<T>::getDefaultName() const
280{
281 return mDefaultName;
282}
283
284//==============================================================================
285template <class T>
286void NameManager<T>::setManagerName(const std::string& _managerName)
287{
288 mManagerName = _managerName;
289}
290
291//==============================================================================
292template <class T>
293const std::string& NameManager<T>::getManagerName() const
294{
295 return mManagerName;
296}
297
298} // namespace common
299} // namespace dart
300
301#endif // DART_COMMON_DETAIL_NAMEMANAGER_HPP_
#define dtmsg
Output a message.
Definition Console.hpp:40
#define dtwarn
Output a warning message.
Definition Console.hpp:46
CollisionResult * result
Collision result of DART.
Definition FCLCollisionDetector.cpp:157
void clear()
Clear all the objects.
Definition NameManager.hpp:197
NameManager(const std::string &_managerName="default", const std::string &_defaultName="default")
Constructor.
Definition NameManager.hpp:46
bool hasObject(const T &_obj) const
Return true if the object is contained.
Definition NameManager.hpp:212
bool removeObject(const T &_obj)
Remove an object from the Manager based on reverse lookup.
Definition NameManager.hpp:169
void setManagerName(const std::string &_managerName)
Set the name of this NameManager so that it can be printed in error reports.
Definition NameManager.hpp:286
bool removeName(const std::string &_name)
Remove an object from the Manager based on its name.
Definition NameManager.hpp:147
const std::string & getManagerName() const
Get the name of this NameManager.
Definition NameManager.hpp:293
bool setPattern(const std::string &_newPattern)
Set a new pattern for name generation.
Definition NameManager.hpp:58
T getObject(const std::string &_name) const
Get object by given name.
Definition NameManager.hpp:226
void removeEntries(const std::string &_name, const T &_obj)
Remove _name using the forward lookup and _obj using the reverse lookup.
Definition NameManager.hpp:189
std::string changeObjectName(const T &_obj, const std::string &_newName)
Change the name of a currently held object.
Definition NameManager.hpp:254
bool addName(const std::string &_name, const T &_obj)
Add an object to the map.
Definition NameManager.hpp:121
std::string getName(const T &_obj) const
Use a reverse lookup to get the name that the manager has _obj listed under.
Definition NameManager.hpp:239
std::string issueNewName(const std::string &_name) const
Issue new unique combined name of given base name and number suffix.
Definition NameManager.hpp:83
std::size_t getCount() const
Get the number of the objects currently stored by the NameManager.
Definition NameManager.hpp:219
std::string issueNewNameAndAdd(const std::string &_name, const T &_obj)
Call issueNewName() and add the result to the map.
Definition NameManager.hpp:109
bool hasName(const std::string &_name) const
Return true if the name is contained.
Definition NameManager.hpp:205
void setDefaultName(const std::string &_defaultName)
Set the name that will be provided to objects passed in with an empty string for a name.
Definition NameManager.hpp:272
const std::string & getDefaultName() const
Get the name that will be provided to objects passed in with an empty string for a name.
Definition NameManager.hpp:279
Definition BulletCollisionDetector.cpp:63