DART  6.6.2
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. More...
 
virtual ~Factory ()=default
 Destructor. More...
 
void registerCreator (const KeyT &key, Creator creator)
 Registers a object creator function with a key. More...
 
template<typename Derived >
void registerCreator (const KeyT &key)
 Registers the default object creator function with a key. More...
 
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. More...
 
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...
 

Static Private Member Functions

template<typename Derived >
static HeldT defaultCreator (Args &&... args)
 

Private Attributes

CreatorMap mCreatorMap
 Object creator function map. More...
 

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

◆ Creator

template<typename KeyT , typename BaseT , typename HeldT = std::shared_ptr<BaseT>, typename... Args>
using dart::common::Factory< KeyT, BaseT, HeldT, Args >::Creator = std::function<HeldT(Args...)>

◆ CreatorMap

template<typename KeyT , typename BaseT , typename HeldT = std::shared_ptr<BaseT>, typename... Args>
using dart::common::Factory< KeyT, BaseT, HeldT, Args >::CreatorMap = std::unordered_map<KeyT, Creator, HashType<KeyT> >

◆ HashType

template<typename KeyT , typename BaseT , typename HeldT = std::shared_ptr<BaseT>, typename... Args>
template<typename Key >
using dart::common::Factory< KeyT, BaseT, HeldT, Args >::HashType = typename std::conditional< std::is_enum<Key>::value, EnumClassHash, std::hash<Key> >::type

◆ This

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

Constructor & Destructor Documentation

◆ Factory()

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

Default constructor.

◆ ~Factory()

template<typename KeyT , typename BaseT , typename HeldT = std::shared_ptr<BaseT>, typename... Args>
virtual dart::common::Factory< KeyT, BaseT, HeldT, Args >::~Factory ( )
virtualdefault

Destructor.

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.

◆ defaultCreator()

template<typename KeyT , typename BaseT , typename HeldT , typename... Args>
template<typename Derived >
HeldT dart::common::Factory< KeyT, BaseT, HeldT, Args >::defaultCreator ( Args &&...  args)
staticprivate

◆ registerCreator() [1/2]

template<typename KeyT , typename BaseT , typename HeldT , typename... Args>
template<typename Derived >
void dart::common::Factory< KeyT, BaseT, HeldT, Args >::registerCreator ( const KeyT &  key)

Registers the default object creator function with a key.

◆ registerCreator() [2/2]

template<typename KeyT , typename BaseT , typename HeldT , typename... Args>
void dart::common::Factory< KeyT, BaseT, HeldT, Args >::registerCreator ( const KeyT &  key,
Creator  creator 
)

Registers a object creator function with a key.

◆ unregisterAllCreators()

template<typename KeyT , typename BaseT , typename HeldT , typename... Args>
void dart::common::Factory< KeyT, BaseT, HeldT, Args >::unregisterAllCreators

Unregisters all the object creator functions.

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

Member Data Documentation

◆ mCreatorMap

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

Object creator function map.