DART  6.10.1
NameManager.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2021, 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_NAMEMANAGER_HPP_
34 #define DART_COMMON_NAMEMANAGER_HPP_
35 
36 #include <map>
37 #include <string>
38 
39 namespace dart {
40 namespace common {
41 
60 template <typename T>
62 {
63 public:
66  const std::string& _managerName = "default",
67  const std::string& _defaultName = "default");
68 
70  virtual ~NameManager() = default;
71 
83  bool setPattern(const std::string& _newPattern);
84 
86  std::string issueNewName(const std::string& _name) const;
87 
89  std::string issueNewNameAndAdd(const std::string& _name, const T& _obj);
90 
92  bool addName(const std::string& _name, const T& _obj);
93 
95  bool removeName(const std::string& _name);
96 
98  bool removeObject(const T& _obj);
99 
102  void removeEntries(const std::string& _name, const T& _obj);
103 
105  void clear();
106 
108  bool hasName(const std::string& _name) const;
109 
111  bool hasObject(const T& _obj) const;
112 
114  std::size_t getCount() const;
115 
121  T getObject(const std::string& _name) const;
122 
125  std::string getName(const T& _obj) const;
126 
134  std::string changeObjectName(const T& _obj, const std::string& _newName);
135 
138  void setDefaultName(const std::string& _defaultName);
139 
142  const std::string& getDefaultName() const;
143 
146  void setManagerName(const std::string& _managerName);
147 
149  const std::string& getManagerName() const;
150 
151 protected:
153  std::string mManagerName;
154 
156  std::map<std::string, T> mMap;
157 
159  std::map<T, std::string> mReverseMap;
160 
163  std::string mDefaultName;
164 
167 
169  std::string mPrefix;
170 
173  std::string mInfix;
174 
176  std::string mAffix;
177 };
178 
179 } // namespace common
180 } // namespace dart
181 
183 
184 #endif // DART_COMMON_NAMEMANAGER_HPP_
class NameManager
Definition: NameManager.hpp:62
std::string mInfix
The chunk of text that comes between a duplicate name and its duplication number.
Definition: NameManager.hpp:173
virtual ~NameManager()=default
Destructor.
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
std::string mManagerName
Name of this NameManager. This is used to report errors.
Definition: NameManager.hpp:153
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 mDefaultName
String which will be used as a name for any object which is passed in with an empty string name.
Definition: NameManager.hpp:163
std::string mAffix
The chunk of text that gets appended to a duplicate name.
Definition: NameManager.hpp:176
std::map< T, std::string > mReverseMap
Reverse map of objects that have been added to the NameManager.
Definition: NameManager.hpp:159
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 mNameBeforeNumber
Internal variable used to arrange the text when resolving duplicate names.
Definition: NameManager.hpp:166
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
std::map< std::string, T > mMap
Map of objects that have been added to the NameManager.
Definition: NameManager.hpp:156
std::string mPrefix
The chunk of text that gets prepended to a duplicate name.
Definition: NameManager.hpp:169
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
Definition: BulletCollisionDetector.cpp:65