DynObj - C++ Cross platform plugin objectsThe DynObj LibraryDynObj is an open-source cross-platform library that uses the run-time plugin approach just decribed. Although the mechanisms used are generic and fairly simple, the library fills many gaps and make it straight-forward to use plugins inside a C++ application.The library provides:
VObj, DynI, DynObj and friendsAll classes discussed below are defined in DynObj.h. The library is based on some properties of objects with VTables:
The VObj class:
We see that VObj does not introduce any methods (it cannot since that would interfere with derived classes which use the first VTable slot). However VObj has a number of convenient inline functions to query for types (VObj::IsA, VObj::CanBeA, VObj::GetObj,...), asking about errors (VObj::Get/Set/ClearError) and more. To determine if a class is a VObj or not, these templates can be used:
bool has_vtable = IsVObj<SomeType>::v; The DynI class:
The DynI class provides a way to know its type (doGetType) and for asking about other types it supports (doGetObj). To ask if a DynI supports the DynStr interface:
DynI *pdi = /* Wherever pointer comes from */; DynStr *pds = do_cast<DynStr*>(pdi); The DynI class has an advantage over VObj:
Since DynI is used across DLL (and possibly compiler) boundaries, we cannot use C++ exceptions. To provide error handling, the methods doGetError and doClearError are introduced. They allow for an object specific error state, without burdening the class with member variables for this. SetError is not a member, since object errors are usually are not set from 'outside'. We see also that the DynI interface has no support for creation or destrcution. The same applies to VObj. The lifespan that can be assumed is that of the current method. If a reference to the object is to be kept, these are different ways to go about it:
The DynObj class:
The DynObj interface represents an object that can be created an destroyed. It represents an object owned from a single point. Usually the functions doDestroy and doDtor would be implemented like:
void MyClass::doDestroy(){ ::delete this; } Objects can be created in some different ways:
The DynSharedI class:
The DynSharedI interface represents an object with shared ownership. doAddRef and doRelease increases and decreases the ownership counter. DynSharedI derives from DynObj since it depends on a way of destroying itself (DynObj::doDestroy) when the lifetime counter reaches 0. To protect the object from being deleted before its actual end-of-life, a doDestroy method can check that the counter is actually zero:
virtual void docall doDestroy( ) { Documenation on building the DynObj library. |