ASL
Loading...
Searching...
No Matches
Factory class and macros

Detailed Description

A Factory allows creating objects given a class name as a String.

They are used through pointers to their base class and through virtual functions. A Factory is created for a given base class, and subclasses must be registered with ASL_FACTORY_REGISTER() before use.

For example, assuming there is a generic possibly abstract base class Animal, we can create a subclass Dog and register it for instantiation by name.

class Dog : public Animal
{
...
};
ASL_FACTORY_REGISTER( Animal, Dog );
#define ASL_FACTORY_REGISTER(Base, Class)
Registers class Class in the factory for class Base for instantiation by name (Class must be a subcla...
Definition Factory.h:100

Now an object of class Animal can be created anywhere only knowing the definition of the abstract class Animal.

#include <Animal.h>
Animal* animal = Factory<Animal>::create("Dog");
static T * create(const String &name)
Constructs an object given a class name previously registered.
Definition Factory.h:39

A Factory can be exported from a dynamic library for use with runtime dynamic loading. In this case the library must export its catalog of subclasses with a macro:

#define ASL_FACTORY_EXPORT(Base)
Exports all registered classes derived from Base for instantiation from a dynamic library.
Definition Factory.h:122

Now a client application can load the library at runtime with the Library class import its Animal factory and create objects.

Library lib("animals.dll");
ASL_FACTORY_IMPORT( Animal, lib );
auto cat = Factory<Animal>::create("Cat");
#define ASL_FACTORY_IMPORT(Base, lib)
Imports all registered classes derived from Base from the given dynamic library.
Definition Factory.h:132

Classes

class  Factory< T >
 A Factory allows creating objects given a class name as a String. More...
 

Macros

#define ASL_FACTORY_REGISTER(Base, Class)
 Registers class Class in the factory for class Base for instantiation by name (Class must be a subclass of Base).
 
#define ASL_FACTORY_REGISTER_AS(Base, Class, Name)
 Registers class Class in the factory for class Base for instantiation by the alternative name Name.
 
#define ASL_FACTORY_EXPORT(Base)
 Exports all registered classes derived from Base for instantiation from a dynamic library.
 
#define ASL_FACTORY_IMPORT(Base, lib)
 Imports all registered classes derived from Base from the given dynamic library.
 

Macro Definition Documentation

◆ ASL_FACTORY_IMPORT

#define ASL_FACTORY_IMPORT (   Base,
  lib 
)

Imports all registered classes derived from Base from the given dynamic library.

That library is a Library object that must be open and persistent (e.g. created in the heap so it exists while its classes are used).