What are good coding patterns to avoid "conditional compile clutter" ( #ifdef Q_OS_ANDROID ) to support multiple platforms ?



  • Problem: There will always be a problem to solve that will require use of a native API.

    Example: I have a JNI interface that sends email with attachments on Android. I will need another implementation for iOS, another for OSX and Linux, another for Windows, etc.

    Solutions?
    What are good coding patterns to avoid "conditional compile clutter"? Can anyone point me to some code examples?

    An interface class with implementation for Android, iOS, Linux, OSX, and Windows?

    Thanks in advance for ideas and suggestions,

    -Ed



  • The bug report below is what got me started thinking about ways to avoid ifdef's:

    QTBUG-29477
    Remove Q_OS_ANDROID checks where possible

    There are a lot of #ifdef Q_OS_ANDROID around, e.g. in the style code, and that's not really the way platform integrations are done anymore. We need to see if they are necessary. In the style code it would make sense if they are inside the android style instead of spread around.

    https://bugreports.qt-project.org/browse/QTBUG-29477


  • Qt Champions 2016

    You may implement your code by using bridge design pattern. Some Qt classes also use this design pattern.

    "Bridge pattern - Wikipedia, the free encyclopedia":http://en.wikipedia.org/wiki/Bridge_pattern


  • Lifetime Qt Champion

    Hi,

    Depending on the amount of code (e.g. complete methods), you can create a yourcoolclass_nameofplatform.cpp file where you'll have the implementation details that you want to separate and you'll compile the correct file for a given platform.

    Hope it helps



  • Thank you both for the suggestions!

    -Ed


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.