How do I enable WinAPI without polluting the rest of the code?

I am working on a C ++ library and part of it is an abstraction layer of several OS functions that I need. I started implementing this using the Windows API, but plan to add support for other C # ifdef platforms, etc.

However, the problem starts to arise, since Windows.h spreads to all other code where I don't need it, and especially since it is a library, it will also pollute other people's code that will use it. I wouldn't mind if the Windows API used a namespace or some neat way to distinguish its code, but instead they #define a lot of pretty common words like small, near, far (lowercase) and many function names are pretty general too ...

I would really like that only a specific part of my code has access to these codes, and it is not included anywhere. I know the obvious solution would be to include only Windows.h in the CPP files, but this is not always possible because some platform-specific data types or data structures are class member variables, such as:

class Window {

public:

    // ...

private:
    HWND handle;

};

      

So, is there a way to do this?

Thank.

+3


source to share


1 answer


Use the pimpl idiom ( http://en.wikipedia.org/wiki/Opaque_pointer ). The limitations of the C ++ programming language force you to use tricks like this to hide information.

One way to do this is to do the same as in C (where you don't have this problem at all, due to the following): Forward-declare struct in the header file and define its content in the implementation file.



Most people do this by extracting the entire private part of your example into their own structure, the contents of which are only defined in the implementation file, and only put the header in the header file, since now only the member is the private part of the class.

also, #define WIN32_LEAN_AND_MEAN before #include to separate what windows.h gives.

+6


source







All Articles