DART 6.6.2
Loading...
Searching...
No Matches
Factory.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_FACTORY_HPP_
34#define DART_COMMON_FACTORY_HPP_
35
36#include <functional>
37#include <unordered_map>
38#include <memory>
39
42
43namespace dart {
44namespace common {
45
56template <typename KeyT,
57 typename BaseT,
58 typename HeldT = std::shared_ptr<BaseT>,
59 typename... Args>
61{
62public:
63 struct EnumClassHash;
64
66 using Creator = std::function<HeldT(Args...)>;
67 template <typename Key>
68 using HashType = typename std::conditional<
69 std::is_enum<Key>::value, EnumClassHash, std::hash<Key>>::type;
70 using CreatorMap = std::unordered_map<KeyT, Creator, HashType<KeyT>>;
71
73 Factory() = default;
74
76 virtual ~Factory() = default;
77
79 void registerCreator(const KeyT& key, Creator creator);
80
82 template <typename Derived>
83 void registerCreator(const KeyT& key);
84
87 void unregisterCreator(const KeyT& key);
88
91
94 bool canCreate(const KeyT& key);
95
98 HeldT create(const KeyT& key, Args&&... args);
99 // TODO(JS): Add create() for creating smart_pointers
100 // (see: https://github.com/dartsim/dart/pull/845)
101
102private:
103 template <typename Derived>
104 static HeldT defaultCreator(Args&&... args);
105
108};
109
111template <typename KeyT,
112 typename BaseT,
113 typename DerivedT,
114 typename HeldT = std::shared_ptr<BaseT>,
115 typename... Args>
117{
118public:
120 using FactoryType = Factory<KeyT, BaseT, HeldT, Args...>;
123
126 FactoryRegistrar(const KeyT& key, Creator creator);
127
130 explicit FactoryRegistrar(const KeyT& key);
131};
132
133} // namespace common
134} // namespace dart
135
137
138#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:117
typename FactoryType::Creator Creator
Definition Factory.hpp:122
Implementation of the Abstract Factory Pattern.
Definition Factory.hpp:61
std::unordered_map< KeyT, Creator, HashType< KeyT > > CreatorMap
Definition Factory.hpp:70
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
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:185
virtual ~Factory()=default
Destructor.
typename std::conditional< std::is_enum< Key >::value, EnumClassHash, std::hash< Key > >::type HashType
Definition Factory.hpp:69
void unregisterAllCreators()
Unregisters all the object creator functions.
Definition Factory-impl.hpp:138
CreatorMap mCreatorMap
Object creator function map.
Definition Factory.hpp:107
Singleton template class.
Definition Singleton.hpp:51
Definition BulletCollisionDetector.cpp:63
Definition Factory-impl.hpp:88