Sign in to follow this  
Khatharr

Patterns for implementing public interfaces

Recommended Posts

Khatharr    8812
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.

Share this post


Link to post
Share on other sites
SiCrane    11839
I generally just use interface (abstract base) classes.
[quote name='Khatharr' timestamp='1355273755' post='5009620']
Would that result in clipping when it's deleted?
[/quote]
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().

Share this post


Link to post
Share on other sites
the_edd    2109
[quote name='Khatharr' timestamp='1355273755' post='5009620']
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.
[/quote]

I'm not 100% sure I'm interpreting your question correctly, but I do this alot:
[code]
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
};
[/code]

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. Edited by e?dd

Share this post


Link to post
Share on other sites
Hodgman    51222
Instead of a abstract base class, I often do something a bit more abhorrent:
[code]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(); }[/code]
[quote name='e?dd' timestamp='1355279824' post='5009659']but I do this alot[/quote]The only problem with that approach, is that anyone who wants to [i]use[/i] 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:
[code]#ifdef PLATFORM_BLAH
char members[48];
#else
...[/code]

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

Share this post


Link to post
Share on other sites
the_edd    2109
[quote name='Hodgman' timestamp='1355286495' post='5009694']
Include the windows-specific headers that define HANDLE and LARGE_INTEGER. Ideally, only your win32 cpp file should have to include those headers.
[/quote]
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. Edited by e?dd

Share this post


Link to post
Share on other sites
Shannon Barber    1681
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this