33 #ifndef DART_COMMON_DETAIL_NAMEMANAGER_HPP_
34 #define DART_COMMON_DETAIL_NAMEMANAGER_HPP_
47 const std::string& _managerName,
const std::string& _defaultName)
48 : mManagerName(_managerName),
49 mDefaultName(_defaultName),
50 mNameBeforeNumber(true),
62 std::size_t name_start = _newPattern.find(
"%s");
63 std::size_t number_start = _newPattern.find(
"%d");
65 if (name_start == std::string::npos || number_start == std::string::npos)
68 if (name_start < number_start)
69 mNameBeforeNumber =
true;
71 mNameBeforeNumber =
false;
73 std::size_t prefix_end = std::min(name_start, number_start);
74 std::size_t infix_end = std::max(name_start, number_start);
76 mPrefix = _newPattern.substr(0, prefix_end);
77 mInfix = _newPattern.substr(prefix_end + 2, infix_end - prefix_end - 2);
78 mAffix = _newPattern.substr(infix_end + 2);
95 if (mNameBeforeNumber)
96 ss << mPrefix << _name << mInfix << count++ << mAffix;
98 ss << mPrefix << count++ << mInfix << _name << mAffix;
100 }
while (hasName(newName));
102 dtmsg <<
"[NameManager::issueNewName] (" << mManagerName <<
") The name ["
103 << _name <<
"] is a duplicate, so it has been renamed to [" << newName
112 const std::string& _name,
const T& _obj)
114 const std::string& checkEmpty = _name.empty() ? mDefaultName : _name;
115 const std::string& newName = issueNewName(checkEmpty);
116 addName(newName, _obj);
127 dtwarn <<
"[NameManager::addName] (" << mManagerName
128 <<
") Empty name is not allowed!\n";
134 dtwarn <<
"[NameManager::addName] (" << mManagerName <<
") The name ["
135 << _name <<
"] already exists!\n";
139 mMap.insert(std::pair<std::string, T>(_name, _obj));
140 mReverseMap.insert(std::pair<T, std::string>(_obj, _name));
142 assert(mReverseMap.size() == mMap.size());
151 assert(mReverseMap.size() == mMap.size());
155 if (it == mMap.end())
159 = mReverseMap.find(it->second);
161 if (rit != mReverseMap.end())
162 mReverseMap.erase(rit);
173 assert(mReverseMap.size() == mMap.size());
177 if (rit == mReverseMap.end())
181 if (it != mMap.end())
184 mReverseMap.erase(rit);
209 return (mMap.find(_name) != mMap.end());
216 return (mReverseMap.find(_obj) != mReverseMap.end());
230 typename std::map<std::string, T>::const_iterator
result = mMap.find(_name);
242 assert(mReverseMap.size() == mMap.size());
244 typename std::map<T, std::string>::const_iterator
result
245 = mReverseMap.find(_obj);
247 if (
result != mReverseMap.end())
256 const T& _obj,
const std::string& _newName)
258 assert(mReverseMap.size() == mMap.size());
261 if (rit == mReverseMap.end())
264 if (rit->second == _newName)
267 removeName(rit->second);
268 return issueNewNameAndAdd(_newName, _obj);
275 mDefaultName = _defaultName;
289 mManagerName = _managerName;
#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:160
void clear()
Clear all the objects.
Definition: NameManager.hpp:199
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:214
bool removeObject(const T &_obj)
Remove an object from the Manager based on reverse lookup.
Definition: NameManager.hpp:171
void setManagerName(const std::string &_managerName)
Set the name of this NameManager so that it can be printed in error reports.
Definition: NameManager.hpp:287
bool removeName(const std::string &_name)
Remove an object from the Manager based on its name.
Definition: NameManager.hpp:149
const std::string & getManagerName() const
Get the name of this NameManager.
Definition: NameManager.hpp:294
bool setPattern(const std::string &_newPattern)
Set a new pattern for name generation.
Definition: NameManager.hpp:60
T getObject(const std::string &_name) const
Get object by given name.
Definition: NameManager.hpp:228
void removeEntries(const std::string &_name, const T &_obj)
Remove _name using the forward lookup and _obj using the reverse lookup.
Definition: NameManager.hpp:191
std::string changeObjectName(const T &_obj, const std::string &_newName)
Change the name of a currently held object.
Definition: NameManager.hpp:255
bool addName(const std::string &_name, const T &_obj)
Add an object to the map.
Definition: NameManager.hpp:123
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:240
std::string issueNewName(const std::string &_name) const
Issue new unique combined name of given base name and number suffix.
Definition: NameManager.hpp:85
std::size_t getCount() const
Get the number of the objects currently stored by the NameManager.
Definition: NameManager.hpp:221
std::string issueNewNameAndAdd(const std::string &_name, const T &_obj)
Call issueNewName() and add the result to the map.
Definition: NameManager.hpp:111
bool hasName(const std::string &_name) const
Return true if the name is contained.
Definition: NameManager.hpp:207
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:273
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:280
std::multimap< dart::dynamics::Shape *, SimpleFrameShapeDnD * >::iterator iterator
Definition: Viewer.cpp:620
Definition: BulletCollisionDetector.cpp:65