c++ design: abstraction layer

Hi everybody,

This is my first post on the forum. I have been looking, and playing a little bit with openframeworks and I am really looking forward to play more with it.

Right now I need advice and teaching about a c++ aspect.

I am developping some librairies, that I would like to be able to port to other platform and or be crossp-platform. Even if I don’t port them right now Iwant to design them to do it easily afterwards.

I know that, to do it the best way is to make an abstraction layer that provide the services and at the back the implementation is platform specific. Now I have been searching the internet and the forum, and I didn’t found a simple enough example, so that I could see where I need to go.

So if someone here know where I could find an example of that, or post an example. Something like basic classes involved, how to include the right platform specific header file and a usage example. I know it looks alot. But I am kind of lost a little bit.

In the end I would say that i would qualify myself as an intermediate c++ programmer that is lacking a little bit of experience. :slight_smile:

Thanks alot


Hi, a very simple example that comes to mind is ofxAccelerometer. Platform independent API to read accelerometer data.

You need one class to determine the API, this is platform independent, doesn’t actually do anything other than bridge the class that does the hardware communication and the app. Then you can just have an addon for each platform which fills in relevant parts of the code.


is the generic APi header and implementation.

is the iphone specific implementation. It just fills in some of the functions.

This is a very very basic way of doing it, with only one class, and implementing certain methods of that class in hardware based addons. A more robust way of doing it would be to have a base class which defines the interface (API), and then for each platform you can extend the class with implementation specific code. This is what the openframeworks core uses for glut abstraction ofBaseAppWindow defines the API, and ofAppGlutWindow is a glut specific implementation. ofAppIphoneWindow is the iphone specific implementation. In your cross-platform app, you would do ofBaseAppWindow *myApp = new ofAppGlutWindow(); or = new ofAppIPhoneWindow();. Then you can carry on calling myApp->setupOpenGL() and it will know how to do it on that platform.

Hope that helps.

Thanks alot Memo,

Yes it help!

First sorry for the delay answer… It was really busy week :slight_smile:

Now instead of:
ofBaseAppWindow *myApp = new ofAppGlutWindow(); or = new ofAppIPhoneWindow();
I guess it would be good to use a kind of factory part of ofBaseAppWindow. And there it would instacited the right one. I also know this could be done with mixin… But I am a bit confuse on which one would be more for this task.

A related question is those specific class like ofAppGlutWindow which is for windows, shouldn’t be compile on osx. How do you prevent that?

I guess that a related question is how to do a good folder/files hierarchy…

I must say I am not to fond of the addon concept in onpeframworks, it just don’t feel right.
So far how I see my sctruture:



So far so good What I can’t seem to figure out, is how to make the platform related folder and include them right, I mean the structure of it. For example I would need 2 versions on my timer class one for mac, my main platform and an other one for windows and probably an other one for iphone or androide or what ever… And in a way to minimize the use of macros…

Is it possible?

Thanks alot!