SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
Classes | Public Member Functions | List of all members
smtk::common::Factory< BaseType, InputTypes > Class Template Reference

A Factory is a class that constructs instances of registered classes which all inherit a common BaseType. More...

#include <Factory.h>

Public Member Functions

template<typename Type >
bool registerType ()
 Register a Type to the factory.
 
bool registerType (Metadata &&metadata)
 Register a Typeusing the Type's Metadata.
 
template<typename Tuple >
bool registerTypes ()
 Register a tuple of Types.
 
template<typename Type >
bool unregisterType ()
 Unregister a Type.
 
bool unregisterType (const std::string &typeName)
 Unregister a Type using its type name.
 
bool unregisterType (const std::size_t &typeIndex)
 Unregister a Type using its type index.
 
template<typename Tuple >
bool unregisterTypes ()
 Unregister a tuple of Types.
 
template<typename Type >
bool contains () const
 Determine whether or not a Type is available.
 
bool contains (const std::string &typeName) const
 Determine whether or not a Type is available using its type name.
 
bool contains (const std::size_t typeIndex) const
 Determine whether or not a Type is available using its type index.
 
template<typename Type , typename... Args>
std::unique_ptr< Type > create (Args &&... args) const
 Create an instance of a Type.
 
template<typename... Args>
std::unique_ptr< BaseType > createFromName (const std::string &typeName, Args &&... args) const
 Create an instance of a Type using its type name.
 
template<typename... Args>
std::unique_ptr< BaseType > createFromIndex (const std::size_t &typeIndex, Args &&... args) const
 Create an instance of a Type using its type name.
 
template<typename Type , typename... Args>
std::shared_ptr< Type > make (Args &&... args) const
 Make a shared instance of a Type.
 
template<typename... Args>
std::shared_ptr< BaseType > makeFromName (const std::string &typeName, Args &&... args) const
 Make a shared instance of a Type using its type name.
 
template<typename... Args>
std::shared_ptr< BaseType > makeFromIndex (const std::size_t &typeIndex, Args &&... args) const
 Create an instance of a Type using its type name.
 
template<typename TagType >
Interface< TagType, true > get () const
 Access a convenience Interface for creating objects that singles out one of the three modes of creation: by type, by type name and by type index.
 

Detailed Description

template<typename BaseType, typename... InputTypes>
class smtk::common::Factory< BaseType, InputTypes >

A Factory is a class that constructs instances of registered classes which all inherit a common BaseType.

A class can be created by keying off of its type (as a template parameter), its type-index (an integer hash of its type name), or its type name. Additionally, instance constructors with several input signatures (InputTypes) of arbitrary length can be provided to the Factory instance at compile-time.

The factory provides two sets of calls:

The latter is sometimes necessary as some compilers are unable to properly convert unique pointers to shared pointers (i.e., when an explicit default deleter is used).

Here is an example of its use:

{c++}
class Base
{
public:
Base() {...}
Base(int i) {...}
Base(double d, const std::string& s) {...}
virtual ~Base() {}
};
class Derived1 : public Base {...};
class Derived2 : public Base {...};
...
Factory<Base, void, int, smtk::common::factory::Inputs<double, const std::string&> > factory;
factory.registerType<Derived1>();
factory.registerType<Derived2>();
// Create via type
std::shared_ptr<Derived1> d_a = factory.make<Derived1>();
// – or –
std::unique_ptr<Derived1> d_a = factory.create<Derived1>();
// Create via index
std::shared_ptr<Base> d_b = factory.makeFromIndex(typeid(Derived1).hash_code());
// – or –
std::unique_ptr<Base> d_b = factory.createFromIndex(typeid(Derived1).hash_code());
// Create via name
std::shared_ptr<Base> d_c = factory.makeFromName("Derived1");
// – or –
std::unique_ptr<Base> d_c = factory.createFromName("Derived1");
// Create with arguments
std::unique_ptr<Base> d_d = factory.createFromName("Derived1", 3.14, std::string("hello"));
// Create using convenience interface:
std::unique_ptr<Base> d_e = factory.get<smtk::common::factory::Index>()
.create(typeid(Derived1).hash_code(), 14);

The documentation for this class was generated from the following file:
smtk::common::Factory::create
std::unique_ptr< Type > create(Args &&... args) const
Create an instance of a Type.
Definition: Factory.h:363
smtk::common::factory::Index
Definition: Factory.h:41