DART  6.7.3
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_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:
65  NameManager(const std::string& _managerName = "default",
66  const std::string& _defaultName = "default");
67 
69  virtual ~NameManager() = default;
70 
82  bool setPattern(const std::string& _newPattern);
83 
85  std::string issueNewName(const std::string& _name) const;
86 
88  std::string issueNewNameAndAdd(const std::string& _name, const T& _obj);
89 
91  bool addName(const std::string& _name, const T& _obj);
92 
94  bool removeName(const std::string& _name);
95 
97  bool removeObject(const T& _obj);
98 
101  void removeEntries(const std::string& _name, const T& _obj);
102 
104  void clear();
105 
107  bool hasName(const std::string& _name) const;
108 
110  bool hasObject(const T& _obj) const;
111 
113  std::size_t getCount() const;
114 
120  T getObject(const std::string& _name) const;
121 
124  std::string getName(const T& _obj) const;
125 
133  std::string changeObjectName(const T& _obj, const std::string& _newName);
134 
137  void setDefaultName(const std::string& _defaultName);
138 
141  const std::string& getDefaultName() const;
142 
144  void setManagerName(const std::string& _managerName);
145 
147  const std::string& getManagerName() const;
148 
149 protected:
151  std::string mManagerName;
152 
154  std::map<std::string, T> mMap;
155 
157  std::map<T, std::string> mReverseMap;
158 
161  std::string mDefaultName;
162 
165 
167  std::string mPrefix;
168 
171  std::string mInfix;
172 
174  std::string mAffix;
175 };
176 
177 } // namespace common
178 } // namespace dart
179 
181 
182 #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:171
virtual ~NameManager()=default
Destructor.
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
std::string mManagerName
Name of this NameManager. This is used to report errors.
Definition: NameManager.hpp:151
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 mDefaultName
String which will be used as a name for any object which is passed in with an empty string name.
Definition: NameManager.hpp:161
std::string mAffix
The chunk of text that gets appended to a duplicate name.
Definition: NameManager.hpp:174
std::map< T, std::string > mReverseMap
Reverse map of objects that have been added to the NameManager.
Definition: NameManager.hpp:157
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 mNameBeforeNumber
Internal variable used to arrange the text when resolving duplicate names.
Definition: NameManager.hpp:164
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
std::map< std::string, T > mMap
Map of objects that have been added to the NameManager.
Definition: NameManager.hpp:154
std::string mPrefix
The chunk of text that gets prepended to a duplicate name.
Definition: NameManager.hpp:167
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