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