Specifying platform independent interface without using virtual member functions

M

/M

Hi!
As you know, one way to seperate platform dependent code from platform
_independent_ code is to create an abstract base class which acts as an
interface towards all platform independent code and implement platform
specific code in the sub classes. Since the platform (in most cases I
quess :) ) can't vary during the execution of a program, using virtual
functions to specify the interface seams a bit odd. I'm thinking about
using templates instead in something like the following way.

template<class Impl>
class GenericSocket
{
//Interface
private:
Impl m_impl;
};

class Win32Impl
{
};

class UNIXImpl
{
};

#if defined WIN32
typedef GenericSocket<Win32Imp> Socket;
....
#endif

One downside of this approach is that the interface that must be
implemented by an implementation class isn't specified (at least not
fully) through the interface of GenericSocket which it would be when
using an abstract base class instead.

Any thoughts on this matter? How do you do it?

Regards
M
 
G

Gianni Mariani

/M said:
Hi!
As you know, one way to seperate platform dependent code from platform
_independent_ code is to create an abstract base class which acts as an
interface towards all platform independent code and implement platform
specific code in the sub classes. Since the platform (in most cases I
quess :) ) can't vary during the execution of a program, using virtual
functions to specify the interface seams a bit odd. I'm thinking about
using templates instead in something like the following way.

template<class Impl>
class GenericSocket
{
//Interface
private:
Impl m_impl;
};

class Win32Impl
{
};

class UNIXImpl
{
};

#if defined WIN32
typedef GenericSocket<Win32Imp> Socket;
...
#endif

One downside of this approach is that the interface that must be
implemented by an implementation class isn't specified (at least not
fully) through the interface of GenericSocket which it would be when
using an abstract base class instead.

Any thoughts on this matter? How do you do it?

Austria C++ uses a couple of ways. Firstly, none of the "public" headers
use any system specific header files. This is a big one because win32
headers are polluted with nasty macros and the *nix headers are not much
better.

Method 1:
The abstract interface classes are in the header and the implementation
classes are in platform specific files.

Method 2:
The non abstract class is in the header and contains a pointer to an
implementation class and the class methods are implementation defined.

Method 3:
This closely matches method 2 but there are no pointers but an opaque
data member that is initialized by a system specific
constructor/destructor in a system specific C++ file.

....

I try very very hard not to use any #ifdef / #if since it becomes
unmaintainable very quickly.

G
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top