Introduction to PDL

What Is PDL?

PDL (Portable Dynamic Loader) is a light, simple, and portable library designed especially for creating and using dynamically loaded class objects.

Why Do You Need to Load Classes Dynamically?

The main purpose of dynamically loadable class technology is creating plug-ins that extend the functionality of the main program. The main problem with dynamically loaded modules on many platforms is that they support a procedure programming paradigm only. When you try to load classes, many problems appear. The PDL library solves most of these problems (but not all of them, sadly).

On a Win32 platform, PDL is very simple alternative to COM technology, without reference counting, global class registration, and many other features. There are several similar libraries for Unix/Linux platforms; for example, C++ Dynamic Class Loader. The dynamic class loading support feature also is present in a large cross-platform library called WxWidgets.

The main goal of PDL development was to create a cross-platform library that could support a single dynamic class loading mechanism for both Win32 and Unix/Linux platforms (unlike COM and C++ Dynamic Class Loader). This library should also be lightweight and independent (unlike huge WxWidgets one).

Creating the Dynamically Loadable Class

Consider the creation of a dynamically loadable class using PDL library in detail. First of all, you need to declare an interface that will be used to work with the instance of a loadable class instance.

Indispensable condition: This interface must be inherited from PDL::DynamicClass.

Look at the DynamicClass declaration:


class DynamicClass
{
public:
/**
* @brief Get class name
* return class name
*/
virtual const char * GetClassName() const throw() = 0;

/**
* @brief Destroy class instance
*/
void Destroy() throw() { delete this; }

protected:
/**
* @brief Destructor
*/
virtual ~DynamicClass() throw() { ;; }
};

The pure virtual method GetClassName() returns the name of the class. You don’t have to worry about its definition, and later I’ll explain why.

The non-virtual method Destroy() destroys the class instance. The class destructor is declared as protected to prevent its direct call. I’ll describe later why you need such a trick.

You have to inherit your interface from PDL::DynamicClass and define protected virtual destructor (according to PDL ideology).

You also need to insert a DECLARE_DYNAMIC_CLASS macro inside of a class declaration with the class name as a parameter. If you look at the definition of this macro, you’ll see that it simply defines the virtual method GetClassName():


#define DECLARE_DYNAMIC_CLASS( className )
public:
virtual const char * GetClassName() const throw()
{ return #className; }

Finally, add to your interface pure virtual methods that implement useful functionality of dynamically loadable class. Let it be the DoSomething() method, for example. As a result, you have the following interface:


#include <DynamicClass.hpp>

class MyTestInterface : public PDL::DynamicClass
{
public:
/**
* @brief Test method
*/
virtual void DoSomething() throw() = 0;

/**
* @brief Declare this class dynamically loadable
*/
DECLARE_DYNAMIC_CLASS( MyTestInterface )
};

You should put this declaration into a separate header file, in your case MyTestInterface.hpp. For compatibility purposes, this file must be included both for building the dynamically loadable class and its direct usage.

Then, you should declare a class, MyTestInterface, inherited from your abstract interface, and define methods that implement its useful functionality. You also need to export this class using the EXPORT_DYNAMIC_CLASS macro. Please note that this macro should be placed outside of a class declaration:


#include <MyTestInterface.hpp>
#include <stdio.h>

class MyTestClass1 : public MyTestInterface
{
public:
/**
* @brief Test method
*/
void DoSomething() throw()
{
fprintf( stderr, “MyTestClass1::DoSomething()n” );
}
};
EXPORT_DYNAMIC_CLASS( MyTestClass1 )

Look at the definition of EXPORT_DYNAMIC_CLASS macro (DynamicClass.hpp file):


#define EXPORT_DYNAMIC_CLASS( className )
extern “C” PDL_DECL_EXPORT PDL::DynamicClass * Create##className()
{
try { return new className(); }
catch( … ) { ;; }
return NULL;
}

This macro defines and exports the function with the name Create<class_name> (a builder function), which creates an instance of the class, given as a parameter. The extern “C” modifier is required to prevent function name mangling. The PDL_DECL_EXPORT macro declares this function as exportable. Its definition in platform.h is specific for different platforms.

There are several important issues. The builder function (Create<class_name>) catches all the exceptions, thrown by class constructor, returning NULL in the case of an exception. That solves all problems with handling of exceptions thrown by the plug-in in the main program. In the case of instance creation, your builder function returns a pointer to the PDL::DynamicClass, so, if you forgot to inherit your interface from this class, the compiler will remind you, producing a casting error.

Now you can build your plug-in. A single plug-in can contain several different classes, but their names should be unique at the module level. Each class must be exported using the EXPORT_DYNAMIC_CLASS macro.

More by Author

Must Read