DART 6.12.2
Loading...
Searching...
No Matches
Factory-impl.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_DETAIL_FACTORY_IMPL_HPP_
34#define DART_COMMON_DETAIL_FACTORY_IMPL_HPP_
35
39
40namespace dart {
41namespace common {
42
43namespace detail {
44
45//==============================================================================
48template <typename T, typename HeldT, typename... Args>
50{
51 static HeldT run(Args&&... args)
52 {
53 return HeldT(new T(std::forward<Args>(args)...));
54 }
55};
56
57//==============================================================================
58template <typename T, typename... Args>
59struct DefaultCreator<T, std::unique_ptr<T>, Args...>
60{
61 static std::unique_ptr<T> run(Args&&... args)
62 {
63 return std::make_unique<T>(std::forward<Args>(args)...);
64 }
65};
66
67//==============================================================================
68template <typename T, typename... Args>
69struct DefaultCreator<T, std::shared_ptr<T>, Args...>
70{
71 static std::shared_ptr<T> run(Args&&... args)
72 {
73 return std::make_shared<T>(std::forward<Args>(args)...);
74 }
75};
76
77} // namespace detail
78
79//==============================================================================
80// std::hash doesn't support enum type. This is an workaround for it.
81// Reference: http://stackoverflow.com/a/24847480
82template <typename KeyT, typename BaseT, typename HeldT, typename... Args>
83struct Factory<KeyT, BaseT, HeldT, Args...>::EnumClassHash
84{
85 template <typename T>
86 std::size_t operator()(T t) const
87 {
88 return static_cast<std::size_t>(t);
89 }
90};
91
92//==============================================================================
93template <typename KeyT, typename BaseT, typename HeldT, typename... Args>
95 const KeyT& key, Creator creator)
96{
97 mCreatorMap[key] = std::move(creator);
98}
99
100//==============================================================================
101template <typename KeyT, typename BaseT, typename HeldT, typename... Args>
102template <typename Derived>
104{
105 return registerCreator(key, &Factory::defaultCreator<Derived>);
106}
107
108//==============================================================================
109template <typename KeyT, typename BaseT, typename HeldT, typename... Args>
111{
112 mCreatorMap.erase(key);
113}
114
115//==============================================================================
116template <typename KeyT, typename BaseT, typename HeldT, typename... Args>
118{
119 mCreatorMap.clear();
120}
121
122//==============================================================================
123template <typename KeyT, typename BaseT, typename HeldT, typename... Args>
125{
126 const auto it = mCreatorMap.find(key);
127
128 return (it != mCreatorMap.end());
129}
130
131//==============================================================================
132template <typename KeyT, typename BaseT, typename HeldT, typename... Args>
134 const KeyT& key, Args&&... args)
135{
136 const auto it = mCreatorMap.find(key);
137
138 const auto found = (it != mCreatorMap.end());
139 if (!found)
140 {
141 dtwarn << "[Factory] Failed to create an object of '"
142 << typeid(BaseT).name() << "' class with the key (type: '"
143 << typeid(KeyT).name() << "'). Returning nullptr instead.\n";
144 // TODO(JS): Print the key if the << operator is defined.
145
146 return nullptr;
147 }
148
149 return it->second(std::forward<Args>(args)...);
150}
151
152//==============================================================================
153template <typename KeyT, typename BaseT, typename HeldT, typename... Args>
154std::unordered_set<KeyT> Factory<KeyT, BaseT, HeldT, Args...>::getKeys() const
155{
156 std::unordered_set<KeyT> keys;
157 for (const auto& entry : mCreatorMap)
158 keys.insert(entry.first);
159
160 return keys;
161}
162
163//==============================================================================
164template <typename KeyT, typename BaseT, typename HeldT, typename... Args>
165template <typename Derived>
167{
169 std::forward<Args>(args)...);
170}
171
172//==============================================================================
173template <
174 typename KeyT,
175 typename BaseT,
176 typename DerivedT,
177 typename HeldT,
178 typename... Args>
180 const KeyT& key, Creator creator)
181{
182 SingletonFactory::getSingleton().registerCreator(key, creator);
183}
184
185//==============================================================================
186template <
187 typename KeyT,
188 typename BaseT,
189 typename DerivedT,
190 typename HeldT,
191 typename... Args>
193 const KeyT& key)
194{
195 SingletonFactory::getSingleton().template registerCreator<DerivedT>(key);
196}
197
198} // namespace common
199} // namespace dart
200
201#endif // DART_COMMON_DETAIL_FACTORY_IMPL_HPP_
#define dtwarn
Output a warning message.
Definition Console.hpp:46
std::string * name
Definition SkelParser.cpp:1697
FactoryRegistrar(const KeyT &key, Creator creator)
Constructor.
Definition Factory-impl.hpp:179
typename FactoryType::Creator Creator
Definition Factory.hpp:130
Implementation of the Abstract Factory Pattern.
Definition Factory.hpp:63
std::unordered_set< KeyT > getKeys() const
Get a set of the keys that are available for this Creator.
Definition Factory-impl.hpp:154
bool canCreate(const KeyT &key)
Returns true if an object creator function is registered with the key.
Definition Factory-impl.hpp:124
std::function< HeldT(Args...)> Creator
Definition Factory.hpp:68
HeldT create(const KeyT &key, Args &&... args)
Creates an object of the class that is registered with a key.
Definition Factory-impl.hpp:133
void registerCreator(const KeyT &key, Creator creator)
Registers a object creator function with a key.
Definition Factory-impl.hpp:94
void unregisterCreator(const KeyT &key)
Unregisters the object creator function that is registered with a key.
Definition Factory-impl.hpp:110
static HeldT defaultCreator(Args &&... args)
Definition Factory-impl.hpp:166
void unregisterAllCreators()
Unregisters all the object creator functions.
Definition Factory-impl.hpp:117
Definition BulletCollisionDetector.cpp:60
Definition SharedLibraryManager.hpp:46
Definition Factory-impl.hpp:84
std::size_t operator()(T t) const
Definition Factory-impl.hpp:86
static std::shared_ptr< T > run(Args &&... args)
Definition Factory-impl.hpp:71
static std::unique_ptr< T > run(Args &&... args)
Definition Factory-impl.hpp:61
The default creator.
Definition Factory-impl.hpp:50
static HeldT run(Args &&... args)
Definition Factory-impl.hpp:51