DART  6.7.3
Factory.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_FACTORY_HPP_
34 #define DART_COMMON_FACTORY_HPP_
35 
36 #include <functional>
37 #include <unordered_map>
38 #include <memory>
39 #include <unordered_set>
40 
43 
44 namespace dart {
45 namespace common {
46 
57 template <typename KeyT,
58  typename BaseT,
59  typename HeldT = std::shared_ptr<BaseT>,
60  typename... Args>
61 class Factory
62 {
63 public:
64  struct EnumClassHash;
65 
67  using Creator = std::function<HeldT(Args...)>;
68  template <typename Key>
69  using HashType = typename std::conditional<
70  std::is_enum<Key>::value, EnumClassHash, std::hash<Key>>::type;
71  using CreatorMap = std::unordered_map<KeyT, Creator, HashType<KeyT>>;
72 
74  Factory() = default;
75 
77  virtual ~Factory() = default;
78 
80  void registerCreator(const KeyT& key, Creator creator);
81 
83  template <typename Derived>
84  void registerCreator(const KeyT& key);
85 
88  void unregisterCreator(const KeyT& key);
89 
91  void unregisterAllCreators();
92 
95  bool canCreate(const KeyT& key);
96 
99  HeldT create(const KeyT& key, Args&&... args);
100  // TODO(JS): Add create() for creating smart_pointers
101  // (see: https://github.com/dartsim/dart/pull/845)
102 
104  std::unordered_set<KeyT> getKeys() const;
105 
106 private:
107  template <typename Derived>
108  static HeldT defaultCreator(Args&&... args);
109 
112 };
113 
115 template <typename KeyT,
116  typename BaseT,
117  typename DerivedT,
118  typename HeldT = std::shared_ptr<BaseT>,
119  typename... Args>
120 class FactoryRegistrar final
121 {
122 public:
124  using FactoryType = Factory<KeyT, BaseT, HeldT, Args...>;
126  using Creator = typename FactoryType::Creator;
127 
130  FactoryRegistrar(const KeyT& key, Creator creator);
131 
134  explicit FactoryRegistrar(const KeyT& key);
135 };
136 
137 } // namespace common
138 } // namespace dart
139 
141 
142 #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:121
FactoryRegistrar(const KeyT &key, Creator creator)
Constructor.
Definition: Factory-impl.hpp:212
typename FactoryType::Creator Creator
Definition: Factory.hpp:126
Implementation of the Abstract Factory Pattern.
Definition: Factory.hpp:62
std::unordered_set< KeyT > getKeys() const
Get a set of the keys that are available for this Creator.
Definition: Factory-impl.hpp:184
std::unordered_map< KeyT, Creator, HashType< KeyT > > CreatorMap
Definition: Factory.hpp:71
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:67
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: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:199
virtual ~Factory()=default
Destructor.
typename std::conditional< std::is_enum< Key >::value, EnumClassHash, std::hash< Key > >::type HashType
Definition: Factory.hpp:70
void unregisterAllCreators()
Unregisters all the object creator functions.
Definition: Factory-impl.hpp:138
CreatorMap mCreatorMap
Object creator function map.
Definition: Factory.hpp:111
Singleton template class.
Definition: Singleton.hpp:51
Definition: BulletCollisionDetector.cpp:63
Definition: Factory-impl.hpp:88