user8145466
user8145466

Reputation: 88

Use both { and #if in a C/C++ macro

I need to write a large class that I'd like to be able to edit in Visual Studio and compile for Windows for testing but the whole app targets Android in the end.

The class in question would only have Android specific code (it would be an interface for the gpg C++ SDK). Due to a series of complications I'm basically forced to do it myself and surround each function's content with something like this

GooglePlayManager::GooglePlayManager()
{
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID

    //code

#endif
}

since otherwise it doesn't compile for Windows. Ideally I would like to define something like this in just this one .cpp file

#define BEGIN_ANDRO { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#define END_ANDRO #endif }

but I can't get it to work. Is there a way to make this happen or a decent alternative I could consider?

Upvotes: 0

Views: 182

Answers (2)

GrahamS
GrahamS

Reputation: 10350

Seems to me that you have an architecture problem here which you are trying to solve with macros.

It looks like your GooglePlayManager has two different implementations, one for Android and one, (possibly empty?) for Windows.

So the code should reflect that. Define a common interface and provide different implementations of that interface for Windows and Android.

You may find your empty Windows implementation may instead form a useful Mock or Stub object that you could use in unit testing.

Upvotes: 2

HolyBlackCat
HolyBlackCat

Reputation: 96236

Preprocessor directives can't appear in macros, but how about following:

#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#  define FOO(...) __VA_ARGS__
#else
#  define FOO(...) {}
#endif

GooglePlayManager::GooglePlayManager() FOO({
    // ...
})

Upvotes: 3

Related Questions