# Poco

# template < class Base >

# class ClassLoader

Library: Foundation
Package: SharedLibrary
Header: Poco/ClassLoader.h

## Description

The ClassLoader loads C++ classes from shared libraries at runtime. It must be instantiated with a root class of the loadable classes. For a class to be loadable from a library, the library must provide a Manifest of all the classes it contains. The Manifest for a shared library can be easily built with the help of the macros in the header file "Foundation/ClassLibrary.h".

Starting with POCO release 1.3, a class library can export multiple manifests. In addition to the default (unnamed) manifest, multiple named manifests can be exported, each having a different base class.

There is one important restriction: one instance of ClassLoader can only load one manifest from a class library.

## Member Summary

Member Functions: begin, canCreate, classFor, create, destroy, end, findClass, findManifest, instance, isAutoDelete, isLibraryLoaded, loadLibrary, manifestFor, unloadLibrary

## Nested Classes

### class Iterator

The ClassLoader's very own iterator class.

### struct LibraryInfo

## Types

### BuildManifestFunc

typedef bool (* BuildManifestFunc)(ManifestBase *);

### InitializeLibraryFunc

typedef void (* InitializeLibraryFunc)();

### LibraryMap

typedef std::map < std::string, LibraryInfo > LibraryMap;

### Manif

typedef Manifest < Base > Manif;

### Meta

typedef AbstractMetaObject < Base > Meta;

### UninitializeLibraryFunc

typedef void (* UninitializeLibraryFunc)();

## Constructors

### ClassLoader

ClassLoader();

Creates the ClassLoader.

## Destructor

### ~ClassLoader

virtual ~ClassLoader();

Destroys the ClassLoader.

## Member Functions

### begin

Iterator begin() const;

### canCreate

bool canCreate(
const std::string & className
) const;

Returns true if create() can create new instances of the class.

### classFor

const Meta & classFor(
const std::string & className
) const;

Returns a reference to the MetaObject for the given class. Throws a NotFoundException if the class is not known.

### create

Base * create(
const std::string & className
) const;

Creates an instance of the given class. Throws a NotFoundException if the class is not known.

### destroy

void destroy(
const std::string & className,
Base * pObject
) const;

Destroys the object pObject points to. Does nothing if object is not found.

### end

Iterator end() const;

### findClass

const Meta * findClass(
const std::string & className
) const;

Returns a pointer to the MetaObject for the given class, or a null pointer if the class is not known.

### findManifest

const Manif * findManifest(
const std::string & path
) const;

Returns a pointer to the Manifest for the given library, or a null pointer if the library has not been loaded.

### instance

Base & instance(
const std::string & className
) const;

Returns a reference to the sole instance of the given class. The class must be a singleton, otherwise an InvalidAccessException will be thrown. Throws a NotFoundException if the class is not known.

### isAutoDelete

bool isAutoDelete(
const std::string & className,
Base * pObject
) const;

Returns true if the object is automatically deleted by its meta object.

### isLibraryLoaded

bool isLibraryLoaded(
const std::string & path
) const;

Returns true if the library with the given name has already been loaded.

### loadLibrary

void loadLibrary(
const std::string & path,
const std::string & manifest
);

Loads a library from the given path, using the given manifest. Does nothing if the library is already loaded. Throws a LibraryLoadException if the library cannot be loaded or does not have a Manifest. If the library exports a function named "pocoInitializeLibrary", this function is executed. If called multiple times for the same library, the number of calls to unloadLibrary() must be the same for the library to become unloaded.

### loadLibrary

void loadLibrary(
const std::string & path
);

Loads a library from the given path. Does nothing if the library is already loaded. Throws a LibraryLoadException if the library cannot be loaded or does not have a Manifest. If the library exports a function named "pocoInitializeLibrary", this function is executed. If called multiple times for the same library, the number of calls to unloadLibrary() must be the same for the library to become unloaded.

Equivalent to loadLibrary(path, "").

### manifestFor

const Manif & manifestFor(
const std::string & path
) const;

Returns a reference to the Manifest for the given library Throws a NotFoundException if the library has not been loaded.

### unloadLibrary

void unloadLibrary(
const std::string & path
);

Unloads the given library. Be extremely cautious when unloading shared libraries. If objects from the library are still referenced somewhere, a total crash is very likely. If the library exports a function named "pocoUninitializeLibrary", this function is executed before it is unloaded. If loadLibrary() has been called multiple times for the same library, the number of calls to unloadLibrary() must be the same for the library to become unloaded.