dart
Classes | Public Types | Public Member Functions | List of all members
dart::common::Factory< KeyT, BaseT, HeldT, Args > Class Template Reference

Implementation of the Abstract Factory Pattern. More...

#include <Factory.hpp>

Classes

struct  EnumClassHash
 

Public Types

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 > >
 

Public Member Functions

 Factory ()=default
 Default constructor.
 
virtual ~Factory ()=default
 Destructor.
 
void registerCreator (const KeyT &key, Creator creator)
 Registers a object creator function with a key.
 
template<typename Derived >
void registerCreator (const KeyT &key)
 Registers the default object creator function with a key.
 
void unregisterCreator (const KeyT &key)
 Unregisters the object creator function that is registered with a key. More...
 
void unregisterAllCreators ()
 Unregisters all the object creator functions.
 
bool canCreate (const KeyT &key)
 Returns true if an object creator function is registered with the key. More...
 
HeldT create (const KeyT &key, Args &&... args)
 Creates an object of the class that is registered with a key. More...
 
std::unordered_set< KeyT > getKeys () const
 Get a set of the keys that are available for this Creator.
 

Detailed Description

template<typename KeyT, typename BaseT, typename HeldT = std::shared_ptr<BaseT>, typename... Args>
class dart::common::Factory< KeyT, BaseT, HeldT, Args >

Implementation of the Abstract Factory Pattern.

Example:

using CdFactory = Factory<std::string, CollisionDetector>;
auto factory = CdFactory();
factory.registerCreator<FclCollisionDetector>("fcl");
auto fclCd = CdFactory::create("fcl");

Member Function Documentation

◆ canCreate()

template<typename KeyT , typename BaseT , typename HeldT , typename... Args>
bool dart::common::Factory< KeyT, BaseT, HeldT, Args >::canCreate ( const KeyT &  key)

Returns true if an object creator function is registered with the key.

Otherwise, returns false.

◆ create()

template<typename KeyT , typename BaseT , typename HeldT , typename... Args>
HeldT dart::common::Factory< KeyT, BaseT, HeldT, Args >::create ( const KeyT &  key,
Args &&...  args 
)

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.

◆ unregisterCreator()

template<typename KeyT , typename BaseT , typename HeldT , typename... Args>
void dart::common::Factory< KeyT, BaseT, HeldT, Args >::unregisterCreator ( 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.


The documentation for this class was generated from the following files: