static std::unique_ptr<Base> create( key_type const& key );                     // (1)

template< typename Config >
static std::unique_ptr<Base> create( key_type const& key, Config const& config ); // (2)

Member of plugin::Plugin<Base>.

Creates an instance of the plugin registered under key, and returns it as std::unique_ptr<Base>.

  1. Uses the plugin's default factory (new T()). Available for every registered plugin.
  2. Uses the plugin's config factory. config is wrapped in std::any and forwarded to the catalog; the registered factory std::any_casts it back to the concrete type it was registered with and calls new T( cfg ). Only available for plugins registered with REGISTER_WITH_CONFIG.

Both simply forward to catalog().create(...).

Parameters

NameDescription
keyThe registered name. For REGISTER(T, Base) this is "T"; for REGISTERBYNAME(name, ...) it is "name".
configA config object whose type must match the Config in the plugin's REGISTER_WITH_CONFIG. Deduced as Config.

Return value

A std::unique_ptr<Base> owning the new instance, or nullptr if key is not registered.

Exceptions

ThrownWhen
std::runtime_error(2) used on a plugin registered without a config factory.
std::bad_any_cast(2) config's type differs from the registered Config.
anything T's constructor throwspropagated unchanged.

key not found is not an exception — the return is nullptr.

Examples

// (1) default
auto sq = Shape::create( "Square" );
if( sq ) sq->setSize( 15 );

// (2) config
auto c  = Shape::create( "Circle", Circle::Config{ .r = 42 } );

// unknown key
auto x  = Shape::create( "Hexagon" );   // x == nullptr

// misuse
auto s  = Shape::create( "Square", Circle::Config{ 1 } );  // throws std::runtime_error
auto e  = Shape::create( "Circle", Ellipse::Config{ 1,2 } ); // throws std::bad_any_cast

Notes

  • The Config template parameter is deduced from the argument; you never write it explicitly. create<Circle::Config>("Circle", cfg) also compiles but adds nothing.
  • A plugin registered with REGISTER_WITH_CONFIG responds to both overloads — create("Circle") calls Circle().
  • Thread-safe: create takes the catalog's shared (read) lock.