• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Khatharr

Patterns for implementing public interfaces

6 posts in this topic

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.
0

Share this post


Link to post
Share on other sites
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().
0

Share this post


Link to post
Share on other sites
[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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
[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
0

Share this post


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

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  
Followers 0