Mr. Boy
Mr. Boy

Reputation: 63728

Cross-platform C++... platform-specific code sections in one file, or separate classes?

Writing C++ stuff that's a bit low-level, you quite often have to write OS-specific stuff. Let's take a MessageBox() function as an example (not a brilliant example since frameworks do this but it's easy to follow).

Say you have a base App class, which each project provides a custom sub-class like MyApp. You could have a single App::MessageBox() method with several #ifdef blocks for each OS. Alternatively you could have a base class/interface, with per-OS sub-classes providing the implementation. e.g AppWin32 : public App

In one way the latter seems neater to me, but on the other it means your MyApp has to use some ugly code to make sure it subclasses the correct OS-specific base class.

What's the better approach?

Upvotes: 2

Views: 1370

Answers (4)

Jinhao
Jinhao

Reputation: 186

IMO, some classes is too simple, such as open/close, #ifdef is not bad for these situations. I don't think providing an abstract platform layer for a simple function is a good idea, it is over-engineering.

But cross-platform is complicated. If you want to provide a specific platform feature that does not exist in a target platform, you should create an abstract platform layer and implement it for each platform, e.g. Clipboard in Windows is big different with X Selections in X11, so you have to maintain different data structures and algorithms to make them unified.

Upvotes: 1

user396672
user396672

Reputation: 3166

#ifdef is definetely the worst.

I would put platform-specific code to separate source trees (one tree per platform). From the project location (for particilar platform) one may use 2 filesystem links - to the "universal" tree (say, named as "src") and to platform specific tree (say, named "specific", actually pointing to linux, windows, or other platform-specific source root). This guarantees that you always pick the proper version of your classees. Moreover, you may even give the same names for similar classes designed for different platforms.

Of course,this approach is impossible under Windows with FAT filesystem, but this is quite rare case now (as there are usually no reasons not to use NTFS under Windows).

Upvotes: 0

Malkocoglu
Malkocoglu

Reputation: 2601

As file/socket/memory/database/... functions are less different from platform to platform than say GUI functions, most of the code is shared/compiled for all architectures. I just use #ifdef blocks around the platform specific code inside these functions/classes.

For the completely different GUI (or any other complex subsystem) code, you should use different implementation (not header, maybe internal header) files under platform directories. (windows/window.cpp , xwin/window.cpp, macosx/window.cpp, ...)

Take a look at GUI toolkits for this scheme, wxwidgets or fltk or most of the others...

Upvotes: 2

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39370

You can use only one #ifdef block if you move implementation to several .cpp files. You could also combine this with the second aproach, however, if there should be need for it. Still, for simple use i guess multiple definition files will do the trick.

Upvotes: 0

Related Questions