Patterns for implementing public interfaces

Started by
5 comments, last by Shannon Barber 11 years, 4 months ago
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. Apart from forward declaring a struct for the private members (pimpl pattern) all I can think of (and this makes me very afraid) is to have the 'real' class inherit from a virtual base that only has the public members, then a factory function could instantiate the derived class and return a pointer to the base (Would that result in clipping when it's deleted? I really don't want to do it like this. :P ).

Anyway, I was just curious what methods other people use for this kind of thing. Any advice would be greatly appreciated.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Advertisement
I generally just use interface (abstract base) classes.

Would that result in clipping when it's deleted?

Not if you make the destructor virtual. You can also make the destructor non-public and require an explicit cleanup function instead of delete. This can be either a non-member function or a member function like COM's Release().

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.
Instead of a abstract base class, I often do something a bit more abhorrent:
class NoCreate { NoCreate(); ~NoCreate(); };
//foo.h
class Foo : NoCreate, NonCopyable
{
public:
static Foo* Create();
static void Destroy(Foo*);
void DoStuff();
};
//foo.cpp
class FooImpl
{
void DoStuff();
int member;
};
void FooImpl::DoStuff()
{
member++;
}

FooImpl* Cast(Foo* p) { return (FooImpl*)p; }
Foo* Cast(FooImpl* p) { return (Foo *)p; }
Foo* Foo::Create() { return Cast(new FooImpl); }
void Foo::Destroy(Foo* f) { delete Cast(f); }
void Foo::DoStuff() { Cast(this)->DoStuff(); }

but I do this alot
The only problem with that approach, is that anyone who wants to use a fish, has to include the windows-specific headers that define HANDLE and LARGE_INTEGER. Ideally, only your win32 cpp file should have to include those headers.

I've see one SDK get around this with crap like:
#ifdef PLATFORM_BLAH
char members[48];
#else
...


In the past I've worked around this by making [font=courier new,courier,monospace]foo.h[/font] only contain the public interface (to be included by people who want to use the class), then [font=courier new,courier,monospace]foo_impl.h[/font] contain the members (to be included by people who want to instantiate the class), and [font=courier new,courier,monospace]foo_[platform].cpp[/font] with the implementations.

Include the windows-specific headers that define HANDLE and LARGE_INTEGER. Ideally, only your win32 cpp file should have to include those headers.

Yes, practically speaking, I do sometimes end up pimpl-ing the windows implementation for that reason, or relying on the fact that I could use void* instead of HANDLE, __int64 instead of LARGE_INTEGER, etc.
Thank you all. smile.png
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
I actually have a class called 'interface' that overloads new/delete/new[]/etc... and makes the dtor virtual.
(In debug build my new operator takes the file & line of the allocation and keeps track of it in a map so at exit I instantly see leaks and where they came form.)

If it's OOD code anyway then you don't really need pImpl. If the code is high-level the OOD overhead is fine. If it's low-level you don't want the overhead of either.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement