Monday, May 28, 2012

Interface based programming in C++

This is a small example of the interface I had designed for exposing SkypeKit functions in a DLL .
In the below sample,  only the interface ISkypeClientLib is being exported.

// This interface is exported from the DLL
   1: class SKYPECLIENTLIB_API ISkypeClientLib
   2: {
   3: public:
   4:     virtual bool Connect(char* strAccountUID,char* strAccountPWD) = 0;
   5:     virtual void LogOut() = 0;
   6: };
   7:  
   8: // Forward declaration
   9: class CSkype;
  10:  
  11: class CSkypeClientLib: public ISkypeClientLib
  12: {
  13:     public:
  14:         CSkypeClientLib(void);
  15:         virtual bool Connect(char* strAccountUID,char* strAccountPWD);
  16:         virtual void LogOut();
  17:   
  18:     private :
  19:         CSkype *pSkype ;
  20: // more code
  21:  
  22: }
  23:  



The derived class CSkypeClientLib has the implementations for the functions, but is hidden from the user.


In the main application the user calls the function GetSkypeLibrary( ) , which returns a CSkypeClientLib pointer to the user.

   1: SKYPECLIENTLIB_API ISkypeClientLib* GetSkypeLibrary(void)
   2: {
   3:    ISkypeClientLib *pSkypeLib = new CSkypeClientLib();
   4:    return pSkypeLib;
   5: }

The user can now use the pointer to call the respective functions.

One can further extend the functionality by doing the following

   1: if(riid == __uuidof(CSkypeClientLib))
   2:     return new CSkypeClientLib;
The following figure shows the interface function in Visual Studio 2010 .


image

No comments :