#include <mcllib/MCInterface.h>
Inheritance diagram for MCInterface:
Public Member Functions | |
MCInterface * | getInterface (const MCIID &iid) |
Obtain an interface pointer for an interface identifier. | |
const MCInterface * | getInterface (const MCIID &iid) const |
Obtain a const interface pointer for an interface identifier. | |
virtual bool | isa (const MCIID &iid) const =0 |
Check what interface is implemented. | |
virtual | ~MCInterface () |
Allows virtual deletes. | |
Static Public Member Functions | |
MCInterface * | getInterface (const MCIID &iid, const MCVariantType &vt) |
Find a specific implementation of an interface. | |
const MCIID & | getInterfaceId (const MCString &name) |
Get the interface identifier for a named interface. | |
const MCString & | getInterfaceName (const MCIID &iid) |
Get an interface name for an interface identifier. | |
void | registerInterface (const MCIID &iid, const MCVariantType &vt, MCInterface *pInterface) |
Register an interface implementation to an implementation key. | |
void | registerName (const MCString &name, const MCIID &iid) |
Register a name for an interface identifier. |
Each interface is bound to an interface identifier MCIID. Registering implementations of an interface is a two stage process Firstly, the interface must be registered with its interface identifier. Secondly, one or more impelentations must be registered against the interface identifier with keys which are unique to the interface. Different interfaces (with different interface identifiers) may register the same name for one of their implementations.
Example interface definition class
-- MyIf.h -- class MyIf : public mcllib::MCInterface { public: // An interface identifier defined elsewhere static const mcllib::MCIID IID; // contract defined by MCInterface virtual ~MyIf(); virtual bool isa(const mcllib::MCIID& iid) const; // the methods defined by this MyIf virtual void doMyThing() = 0; };
-- MyIf.cpp -- const mcllib::MCIID MyIf::ID = some_iid; bool MyIf::isa(const mcllib::MCIID& iid) const { return(iid == IID); }
Once an interface definition class has been created, one or more implementation classes can be written which derive from it and implement its pure virtual methods.
Example implementation class
-- MyImpl.h -- class MyImpl : public MyIf { public: MyImpl(); virtual ~MyImpl(); virtual bool isa(const mcllib::MCIID& iid) const; // contract defined by MyIf virtual void doMyThing(); };
-- MyImpl.cpp -- void MyImpl::doMyThing() const { ... }
After interface definition and implementation classes have been written, the interface and its implementation(s) can be registered. This is an optional process, since there may only be one implementation of an interface.
Example registration
// Register a name for the interface. Ties together name and identifier mcllib::MCInterface::registerName("myif.mydomain.com", MyIf::IID); // Construct an implementation object MyImpl* pImpl = new MyImpl; // Get a name for this particular implementation mcllib::MCVTString implKey("myimpl1"); mcllib::MCInterface::registerInterface(MyIf::IID, implKey, pImpl);
|
Find a specific implementation of an interface. The implementation must have been previously registered with registerName().
|
|
Obtain an interface pointer for an interface identifier. Throws an exception if the interface identifier specifies an interface which is not implemented.
|
|
Obtain a const interface pointer for an interface identifier. Throws an exception if the interface identifier specifies an interface which is not implemented.
|
|
Get the interface identifier for a named interface.
|
|
Get an interface name for an interface identifier.
|
|
Check what interface is implemented.
|
|
Register an interface implementation to an implementation key. Subsequent calls to getInterface() which specify the iid and vt will return pInterface.
|
|
Register a name for an interface identifier. Subsequent calls to getInterfaceId() or getInterfaceName() that specifiy the appropriate parameters will succeed.
|