•      Sign In
• Create Account

## Patterns for implementing public interfaces

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1Khatharr  Members

7695
Like
0Likes
Like

Posted 11 December 2012 - 06:55 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. 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. ).

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.

### #2SiCrane  Moderators

11526
Like
0Likes
Like

Posted 11 December 2012 - 08:05 PM

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().

### #3e‍dd  Members

2109
Like
0Likes
Like

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.

Edited by e‍dd, 11 December 2012 - 08:37 PM.

### #4Hodgman  Moderators

49417
Like
0Likes
Like

Posted 11 December 2012 - 10:28 PM

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 foo.h only contain the public interface (to be included by people who want to use the class), then foo_impl.h contain the members (to be included by people who want to instantiate the class), and foo_[platform].cpp with the implementations.

Edited by Hodgman, 11 December 2012 - 10:42 PM.

### #5e‍dd  Members

2109
Like
0Likes
Like

Posted 11 December 2012 - 10:37 PM

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.

Edited by e‍dd, 11 December 2012 - 10:37 PM.

### #6Khatharr  Members

7695
Like
0Likes
Like

Posted 12 December 2012 - 01:25 AM

Thank you all.
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.

### #7Shannon Barber  Moderators

1657
Like
0Likes
Like

Posted 18 December 2012 - 09:12 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.