DART  6.6.2
Factory-impl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2018, 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 
36 #include "dart/common/Factory.hpp"
37 
38 #include "dart/common/Memory.hpp"
39 #include "dart/common/Console.hpp"
40 
41 namespace dart {
42 namespace common {
43 
44 namespace detail {
45 
46 //==============================================================================
49 template <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 //==============================================================================
59 template <typename T, typename... Args>
60 struct DefaultCreator<T, std::unique_ptr<T>, Args...>
61 {
62  static std::unique_ptr<T> run(Args&&... args)
63  {
64  return dart::common::make_unique<T>(std::forward<Args>(args)...);
65  }
66 };
67 
68 //==============================================================================
69 template <typename T, typename... Args>
70 struct 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
83 template <typename KeyT,
84  typename BaseT,
85  typename HeldT,
86  typename... Args>
87 struct Factory<KeyT, BaseT, HeldT, Args...>::EnumClassHash
88 {
89  template <typename T>
90  std::size_t operator()(T t) const
91  {
92  return static_cast<std::size_t>(t);
93  }
94 };
95 
96 //==============================================================================
97 template <typename KeyT,
98  typename BaseT,
99  typename HeldT,
100  typename... Args>
102  const KeyT& key, Creator creator)
103 {
104  mCreatorMap[key] = std::move(creator);
105 }
106 
107 //==============================================================================
108 template <typename KeyT,
109  typename BaseT,
110  typename HeldT,
111  typename... Args>
112 template <typename Derived>
114  const KeyT& key)
115 {
116  return registerCreator(
117  key,
118  &Factory::defaultCreator<Derived>
119  );
120 }
121 
122 //==============================================================================
123 template <typename KeyT,
124  typename BaseT,
125  typename HeldT,
126  typename... Args>
128  const KeyT& key)
129 {
130  mCreatorMap.erase(key);
131 }
132 
133 //==============================================================================
134 template <typename KeyT,
135  typename BaseT,
136  typename HeldT,
137  typename... Args>
139 {
140  mCreatorMap.clear();
141 }
142 
143 //==============================================================================
144 template <typename KeyT,
145  typename BaseT,
146  typename HeldT,
147  typename... Args>
149 {
150  const auto it = mCreatorMap.find(key);
151 
152  return (it != mCreatorMap.end());
153 }
154 
155 //==============================================================================
156 template <typename KeyT,
157  typename BaseT,
158  typename HeldT,
159  typename... Args>
161  const KeyT& key, Args&&... args)
162 {
163  const auto it = mCreatorMap.find(key);
164 
165  const auto found = (it != mCreatorMap.end());
166  if (!found)
167  {
168  dtwarn << "[Factory] Failed to create an object of '"
169  << typeid(BaseT).name() << "' class with the key (type: '"
170  << typeid(KeyT).name() << "'). Returning nullptr instead.\n";
171  // TODO(JS): Print the key if the << operator is defined.
172 
173  return nullptr;
174  }
175 
176  return it->second(std::forward<Args>(args)...);
177 }
178 
179 //==============================================================================
180 template <typename KeyT,
181  typename BaseT,
182  typename HeldT,
183  typename... Args>
184 template <typename Derived>
186 {
188  std::forward<Args>(args)...);
189 }
190 
191 //==============================================================================
192 template <typename KeyT,
193  typename BaseT,
194  typename DerivedT,
195  typename HeldT,
196  typename... Args>
198 FactoryRegistrar(const KeyT& key, Creator creator)
199 {
200  SingletonFactory::getSingleton().registerCreator(key, creator);
201 }
202 
203 //==============================================================================
204 template <typename KeyT,
205  typename BaseT,
206  typename DerivedT,
207  typename HeldT,
208  typename... Args>
210 FactoryRegistrar(const KeyT& key)
211 {
212  SingletonFactory::getSingleton().template registerCreator<DerivedT>(key);
213 }
214 
215 } // namespace common
216 } // namespace dart
217 
218 #endif // DART_COMMON_DETAIL_FACTORY_IMPL_HPP_
#define dtwarn
Output a warning message.
Definition: Console.hpp:46
std::string * name
Definition: SkelParser.cpp:1642
FactoryRegistrar(const KeyT &key, Creator creator)
Constructor.
Definition: Factory-impl.hpp:198
typename FactoryType::Creator Creator
Definition: Factory.hpp:122
Implementation of the Abstract Factory Pattern.
Definition: Factory.hpp:61
bool canCreate(const KeyT &key)
Returns true if an object creator function is registered with the key.
Definition: Factory-impl.hpp:148
std::function< HeldT(Args...)> Creator
Definition: Factory.hpp:66
HeldT create(const KeyT &key, Args &&... args)
Creates an object of the class that is registered with a key.
Definition: Factory-impl.hpp:160
void registerCreator(const KeyT &key, Creator creator)
Registers a object creator function with a key.
Definition: Factory-impl.hpp:101
void unregisterCreator(const KeyT &key)
Unregisters the object creator function that is registered with a key.
Definition: Factory-impl.hpp:127
static HeldT defaultCreator(Args &&... args)
Definition: Factory-impl.hpp:185
void unregisterAllCreators()
Unregisters all the object creator functions.
Definition: Factory-impl.hpp:138
Definition: BulletCollisionDetector.cpp:63
Definition: SharedLibraryManager.hpp:43
Definition: Factory-impl.hpp:88
std::size_t operator()(T t) const
Definition: Factory-impl.hpp:90
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