Disabling the common interface from implementation details

I need to create a class Fontthat will have several implementations covering platforms or different libraries (for example, Win32 GDI or FreeType). Thus, essentially there will be one common header / interface file and several .cpp implementations (selected at build time). I would prefer to keep the public interface (header file) clean from any implementation details, but this is usually difficult to achieve. A font object must be dragging some particular state — for example, a handle in GDI or a FreeType face object inside.

In C ++ , what is the best way to track the details of a private implementation? Should I use static data in implementation files?

Edit: Found this wonderful article on the topic: Separation of the interface and implementation in C ++ .

ps I remember that in Objective-C there are private categories that allow you to define the class extension in your personal implementation file, making a rather elegant decision.

+5
source share
1 answer

You can use the PIMPL design pattern.

This is basically when your object contains a pointer to a valid platform-specific part and passes all platform-specific calls to that object.

Font.h

class FontPlatform;
class Font
{
   public:
       Font();
       void Stuff();
       void StuffPlatform();
   private:
       FontPlatform*  plat;
};

Font.cpp

#include "Font.h"
#if Win
#include "FontWindows.h"
#else
#error "Not implemented on non Win platforms"
#endif

Font::Font()
  : plat(new PLATFORM_SPCIFIC_FONT)  // DO this cleaner by using factory.
{}

void Font::Stuff()  { /* DoStuff */ }
void Font::StuffPlatform()
{
    plat->doStuffPlat();  // Call the platform specific code.
}
+7
source

All Articles