DART  6.10.1
Factory.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_FACTORY_HPP_
34 #define DART_COMMON_FACTORY_HPP_
35 
36 #include <functional>
37 #include <memory>
38 #include <unordered_map>
39 #include <unordered_set>
40 
43 
44 namespace dart {
45 namespace common {
46 
57 template <
58  typename KeyT,
59  typename BaseT,
60  typename HeldT = std::shared_ptr<BaseT>,
61  typename... Args>
62 class Factory
63 {
64 public:
65  struct EnumClassHash;
66 
68  using Creator = std::function<HeldT(Args...)>;
69  template <typename Key>
70  using HashType = typename std::conditional<
71  std::is_enum<Key>::value,
73  std::hash<Key>>::type;
74  using CreatorMap = std::unordered_map<KeyT, Creator, HashType<KeyT>>;
75 
77  Factory() = default;
78 
80  virtual ~Factory() = default;
81 
83  void registerCreator(const KeyT& key, Creator creator);
84 
86  template <typename Derived>
87  void registerCreator(const KeyT& key);
88 
91  void unregisterCreator(const KeyT& key);
92 
94  void unregisterAllCreators();
95 
98  bool canCreate(const KeyT& key);
99 
102  HeldT create(const KeyT& key, Args&&... args);
103  // TODO(JS): Add create() for creating smart_pointers
104  // (see: https://github.com/dartsim/dart/pull/845)
105 
107  std::unordered_set<KeyT> getKeys() const;
108 
109 private:
110  template <typename Derived>
111  static HeldT defaultCreator(Args&&... args);
112 
115 };
116 
118 template <
119  typename KeyT,
120  typename BaseT,
121  typename DerivedT,
122  typename HeldT = std::shared_ptr<BaseT>,
123  typename... Args>
124 class FactoryRegistrar final
125 {
126 public:
128  using FactoryType = Factory<KeyT, BaseT, HeldT, Args...>;
130  using Creator = typename FactoryType::Creator;
131 
134  FactoryRegistrar(const KeyT& key, Creator creator);
135 
138  explicit FactoryRegistrar(const KeyT& key);
139 };
140 
141 } // namespace common
142 } // namespace dart
143 
145 
146 #endif // DART_COMMON_FACTORY_HPP_
std::string type
Definition: SdfParser.cpp:82
Helper class to register a object creator function to the Singleton.
Definition: Factory.hpp:125
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
std::unordered_map< KeyT, Creator, HashType< KeyT > > CreatorMap
Definition: Factory.hpp:74
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
Factory()=default
Default constructor.
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
virtual ~Factory()=default
Destructor.
typename std::conditional< std::is_enum< Key >::value, EnumClassHash, std::hash< Key > >::type HashType
Definition: Factory.hpp:73
void unregisterAllCreators()
Unregisters all the object creator functions.
Definition: Factory-impl.hpp:118
CreatorMap mCreatorMap
Object creator function map.
Definition: Factory.hpp:114
Singleton template class.
Definition: Singleton.hpp:51
Definition: BulletCollisionDetector.cpp:65
Definition: Factory-impl.hpp:85