0

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?

YSC
  • 38,212
  • 9
  • 96
  • 149
  • Why even bother compiling it on Windows if it only contains Android specific code anyway? – Jesper Juhl Mar 27 '18 at 10:34
  • I'm doing the whole game and it's just 50 times faster to do things on Windows. For instance whenever I change a Sprite or something, I can instantly see how it looks. The compile times are much faster and the compilation process is simpler which helps a lot with testing things and just generally writing code. The reason I want this particular is class to be available on Windows is because I want to call stuff from it from other places in code without polluting that (genuinely cross platform) code with macros. Just call empty functions basically. – user8145466 Mar 27 '18 at 10:43
  • Why are you wondering about C when writing C++ classes? C does not have classes. – MSalters Mar 27 '18 at 10:53
  • 1
    The question doesn't really have anything to do with classes, it just so happens I have a bunch of functions in a class but they could be global too. The question is about the C/C++ processor which I believe is the same in both languages. I just want to condionally empty out all the function bodies in a particular C/CPP file as painlessly as possible presumably using a macro. – user8145466 Mar 27 '18 at 10:58

2 Answers2

3

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({
    // ...
})
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
2

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.

GrahamS
  • 9,980
  • 9
  • 49
  • 63
  • Well, maybe. But I also need functions which deal with and return certain GPG:: objects (I can't include anything from GPG on windows) and my class will probably need some as well. So I was thinking to have all that internally and only expose functions which have objects available on both platforms in their signatures. Then I'd macro out all the things referring to those objects and the objects themselves from my class and any code referring to them in the bodies of my public functions. – user8145466 Mar 27 '18 at 15:58
  • Still sounds like more of an architecture issue. You just have to draw your dividing lines in the right place: abstract around platform-specific issues and place abstractions/interfaces in places that let you unit/module test as required. – GrahamS Mar 27 '18 at 16:16
  • I'll try to think more about it and see if I can accomplish something like that but I'm not very experienced when it comes to that sort of thing. As for unit tests, the GPG SDK is a complete C++ SDK by Google so there isn't anything to test. The only reason I'm even making this manager class is just to wrap it and forward its functionality so I can hide it from Visual Studio and its compiler. Otherwise, I'd just be calling its functions directly. – user8145466 Mar 27 '18 at 16:28