SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
Public Member Functions | Static Public Member Functions | List of all members
smtk::common::Extension Class Reference

Allow extension of operator functionality in separate libraries. More...

#include <Extension.h>

Inheritance diagram for smtk::common::Extension:
[legend]
Collaboration diagram for smtk::common::Extension:
[legend]

Public Member Functions

 smtkTypeMacroBase (Extension)
 

Static Public Member Functions

static bool registerExtension (const std::string &name, std::function< Extension::Ptr(void)> ctor, bool oneShot=true)
 Register an extension with a unique name. More...
 
static bool unregisterExtension (const std::string &name)
 Remove an extension from the registry.
 
static void visitAll (std::function< std::pair< bool, bool >(const std::string &, Extension::Ptr)> visitor)
 Call the given function on each registered extension. More...
 
template<typename T >
static void visit (std::function< std::pair< bool, bool >(const std::string &, T)> visitor)
 Iterate over all the extensions that are subclasses of the given type. More...
 
static Extension::Ptr find (const std::string &name, bool removeOneShot=true)
 Find an extension given a specific name.
 
template<typename T >
static T::Ptr findAs (const std::string &name)
 Find the first extension with a given name and type.
 

Detailed Description

Allow extension of operator functionality in separate libraries.

Extension subclasses allow an operator to present additional functionality when additional libraries are available beyond those required by the one containing the operator itself.

An example from ModelBuilder would be extending the auxiliary geometry operator to test loading files using VTK without the base operator depending on VTK. The base operator would call Extension::find() to discover whether any extensions were available for its particular instance. If so, then it could call methods on the extension to have it test files for compatibility. In this way, the base operator can be available even when VTK is not, but provide better feedback when VTK is present.

Extensions are registered with a unique name. If you wish to find all extensions (or the first extension) of a given type, use the visit() method rather than findAs().

Extensions may be one-shot (i.e., unregistered after they are used) or permanent (i.e., unregistered only when the executable exits or dynamic library is unloaded). Registrations using the smtkDeclareExtension() macro are always permanent

Member Function Documentation

◆ registerExtension()

bool smtk::common::Extension::registerExtension ( const std::string &  name,
std::function< Extension::Ptr(void)>  ctor,
bool  oneShot = true 
)
static

Register an extension with a unique name.

Any operator that asks for this name will be given the result of calling the passed function. If oneShot is true (the default), then the extension will be unregistered the first time it is used.

Note that the find method is templated, so the function may produce an instance of an extension which is immediately discarded because it cannot be cast to the type requested by the called of find().

◆ visit()

template<typename T >
static void smtk::common::Extension::visit ( std::function< std::pair< bool, bool >(const std::string &, T)>  visitor)
inlinestatic

Iterate over all the extensions that are subclasses of the given type.

See also
visitAll for more information on how visitor is used.

◆ visitAll()

void smtk::common::Extension::visitAll ( std::function< std::pair< bool, bool >(const std::string &, Extension::Ptr)>  visitor)
static

Call the given function on each registered extension.

The visitor function is passed the name of the extension and an instance of it. The visitor must return a pair of booleans: the first indicates whether or not the instance was used (so that one-shot extensions are unregistered properly). The second boolean indicates whether visitAll() should terminate early without invoking visitor on any more extensions.


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