• Create Account

### #Actuale‍dd

Posted 11 December 2012 - 08:37 PM

How do people deal with the problem of creating portable public interfaces?

So far the best solution I've found is the "pimpl"/"cheshire cat" pattern, but it's cumbersome and sometimes problematic. Isn't there a better way of doing this or is just one of those neat little things that we all get to struggle with?

I want to have a sort of 'wall of implementation' that separates the portable side of my code from the system-dependent side.

I'm not 100% sure I'm interpreting your question correctly, but I do this alot:
class fish
{
// ...

private:
#if defined(_WIN32)
HANDLE handle;
LARGE_INTEGER file_offset;
#elif defined(__APPLE__)
int fd;
offset_t file_offset;
#else
// and so on...
#endif
};


Then have one .cpp file for each platform. Each platform will typically have some macro whose existence you can rely on to differentiate it from others. For example _WIN32 is defined supported by every Windows compiler I've ever encountered.

Your Linux code is "protected" from your Windows code (for example), because the Linux side won't even know the Windows stuff is there, and vice versa.

You can still use pimpl if you want, but if you wouldn't have used pimpl if there was only one platform to consider, then there's little point in doing so here.

### #1e‍dd

Posted 11 December 2012 - 08:37 PM

How do people deal with the problem of creating portable public interfaces?

So far the best solution I've found is the "pimpl"/"cheshire cat" pattern, but it's cumbersome and sometimes problematic. Isn't there a better way of doing this or is just one of those neat little things that we all get to struggle with?

I want to have a sort of 'wall of implementation' that separates the portable side of my code from the system-dependent side.

I'm not 100% sure I'm interpreting your question correctly, but I do this alot:
class fish
{
// ...

private:
#if defined(_WIN32)
HANDLE handle;
DWORD file_offset;
#elif defined(__APPLE__)
int fd;
offset_t file_offset;
#else
// and so on...
#endif
};


Then have one .cpp file for each platform. Each platform will typically have some macro whose existence you can rely on to differentiate it from others. For example _WIN32 is defined supported by every Windows compiler I've ever encountered.

Your Linux code is "protected" from your Windows code (for example), because the Linux side won't even know the Windows stuff is there, and vice versa.

You can still use pimpl if you want, but if you wouldn't have used pimpl if there was only one platform to consider, then there's little point in doing so here.

PARTNERS