-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Factory class and collision detector factory (#864)
- Loading branch information
Showing
23 changed files
with
942 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright (c) 2017, Graphics Lab, Georgia Tech Research Corporation | ||
* Copyright (c) 2017, Personal Robotics Lab, Carnegie Mellon University | ||
* All rights reserved. | ||
* | ||
* This file is provided under the following "BSD-style" License: | ||
* Redistribution and use in source and binary forms, with or | ||
* without modification, are permitted provided that the following | ||
* conditions are met: | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following | ||
* disclaimer in the documentation and/or other materials provided | ||
* with the distribution. | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND | ||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#ifndef DART_COMMON_FACTORY_HPP_ | ||
#define DART_COMMON_FACTORY_HPP_ | ||
|
||
#include <functional> | ||
#include <unordered_map> | ||
#include <memory> | ||
|
||
#include "dart/common/StlHelpers.hpp" | ||
#include "dart/common/Singleton.hpp" | ||
|
||
namespace dart { | ||
namespace common { | ||
|
||
/// Implementation of the Abstract Factory Pattern. | ||
/// | ||
/// Example: | ||
/// \code | ||
/// using CdFactory = Factory<std::string, CollisionDetector>; | ||
/// | ||
/// auto factory = CdFactory(); | ||
/// factory.registerCreator<FclCollisionDetector>("fcl"); | ||
/// auto fclCd = CdFactory::create("fcl"); | ||
/// \endcode | ||
template <typename KeyT, | ||
typename BaseT, | ||
typename HeldT = std::shared_ptr<BaseT>, | ||
typename... Args> | ||
class Factory | ||
{ | ||
public: | ||
struct EnumClassHash; | ||
|
||
using This = Factory<KeyT, BaseT, HeldT>; | ||
using Creator = std::function<HeldT(Args...)>; | ||
template <typename Key> | ||
using HashType = typename std::conditional< | ||
std::is_enum<Key>::value, EnumClassHash, std::hash<Key>>::type; | ||
using CreatorMap = std::unordered_map<KeyT, Creator, HashType<KeyT>>; | ||
|
||
/// Default constructor. | ||
Factory() = default; | ||
|
||
/// Destructor | ||
virtual ~Factory() = default; | ||
|
||
/// Registers a object creator function with a key. | ||
void registerCreator(const KeyT& key, Creator creator); | ||
|
||
/// Registers the default object creator function with a key. | ||
template <typename Derived> | ||
void registerCreator(const KeyT& key); | ||
|
||
/// Unregisters the object creator function that is registered with a key. Do | ||
/// nothing if there is no creator function associated with the key. | ||
void unregisterCreator(const KeyT& key); | ||
|
||
/// Unregisters all the object creator functions. | ||
void unregisterAllCreators(); | ||
|
||
/// Returns true if an object creator function is registered with the key. | ||
/// Otherwise, returns false. | ||
bool canCreate(const KeyT& key); | ||
|
||
/// Creates an object of the class that is registered with a key. Returns | ||
/// nullptr if there is no object creator function associated with the key. | ||
HeldT create(const KeyT& key, Args&&... args); | ||
// TODO(JS): Add create() for creating smart_pointers | ||
// (see: https://github.com/dartsim/dart/pull/845) | ||
|
||
private: | ||
/// Object creator function map. | ||
CreatorMap mCreatorMap; | ||
}; | ||
|
||
/// Helper class to register a object creator function to the Singleton. | ||
template <typename KeyT, | ||
typename BaseT, | ||
typename DerivedT, | ||
typename HeldT = std::shared_ptr<BaseT>, | ||
typename... Args> | ||
class FactoryRegistrar final | ||
{ | ||
public: | ||
using This = FactoryRegistrar<KeyT, BaseT, DerivedT, HeldT>; | ||
using FactoryType = Factory<KeyT, BaseT, HeldT, Args...>; | ||
using SingletonFactory = Singleton<FactoryType>; | ||
using Creator = typename FactoryType::Creator; | ||
|
||
/// Constructor. Interanlly, this constructor registers Derived class with | ||
/// the key and the default creator function. | ||
FactoryRegistrar(const KeyT& key, Creator creator); | ||
|
||
/// Constructor. Interanlly, this constructor registers Derived class with | ||
/// the key and the default creator function. | ||
explicit FactoryRegistrar(const KeyT& key); | ||
}; | ||
|
||
} // namespace common | ||
} // namespace dart | ||
|
||
#include "dart/common/detail/Factory-impl.hpp" | ||
|
||
#endif // DART_COMMON_FACTORY_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (c) 2017, Graphics Lab, Georgia Tech Research Corporation | ||
* Copyright (c) 2017, Personal Robotics Lab, Carnegie Mellon University | ||
* All rights reserved. | ||
* | ||
* This file is provided under the following "BSD-style" License: | ||
* Redistribution and use in source and binary forms, with or | ||
* without modification, are permitted provided that the following | ||
* conditions are met: | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following | ||
* disclaimer in the documentation and/or other materials provided | ||
* with the distribution. | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND | ||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#ifndef DART_COMMON_SINGLETON_HPP_ | ||
#define DART_COMMON_SINGLETON_HPP_ | ||
|
||
namespace dart { | ||
namespace common { | ||
|
||
/// Singleton template class | ||
/// | ||
/// \note This singleton is not thread safe. For use of thread safe singleton, | ||
/// use static initialization as: | ||
/// | ||
/// // Singletone class Engine | ||
/// class Engine : public Singleton<Engine> {}; | ||
/// | ||
/// // Call before main() and use theT only instead of calling getSingleton() | ||
/// static T& theT = T::getSingleton(); | ||
template<typename T> | ||
class Singleton | ||
{ | ||
public: | ||
/// Returns reference of the singleton | ||
template <typename... Args> | ||
static T& getSingleton(Args... _args); | ||
|
||
/// Returns pointer of the singleton | ||
template <typename ... Args> | ||
static T* getSingletonPtr(Args... _args); | ||
|
||
protected: | ||
/// Constructor | ||
Singleton() = default; | ||
|
||
/// Destructor | ||
virtual ~Singleton() = default; | ||
|
||
private: | ||
/// Don't implement copy constructor | ||
Singleton(const T&) = delete; | ||
|
||
/// Don't assignment operator | ||
const T& operator=(const T&) = delete; | ||
|
||
private: | ||
/// Singleton instance | ||
static T* mInstance; | ||
}; | ||
|
||
} // namespace common | ||
} // namespace dart | ||
|
||
#include "dart/common/detail/Singleton-impl.hpp" | ||
|
||
#endif // DART_COMMON_SINGLETON_HPP_ |
Oops, something went wrong.