Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to abstract your code from platform specific header file
    text
    copied!<p>Hi I am trying to find a way to prevent the inclusion of platform specific header file for example windows.h. Curiously none of the solution I found are not satisfactory to me. Maybe it can't be achieved.</p> <p>I think to achieve this goal several technique need to be used. And there is a lot of example on internet but I couldn't found any about one aspect. Something has to talk/create to your abstraction. Here is an example:</p> <p>This is a really simplified version of a render window render target. </p> <pre><code>//D3D11RenderWindow.h #include &lt;d3d11.h&gt; class D3D11RenderWindow: public GfxRenderWindow { public: bool initialize(HWND windowHandle); private: HWND windowHandle_; /// Win32 window handle }; </code></pre> <p>That is not so much the problem, this is a platform specific code that get included only by platform specific code. But we need to actually instanciate this type so an "entry point" need to know about the platform specific code too.</p> <p>For example a factory class:</p> <pre><code>//GfxRenderWindowFactory.h #define WIN32_LEAN_AND_MEAN #define NOMINMAX #include &lt;windows.h&gt; class GfxRenderWindow; class GfxRenderWindowFactory { public: static std::unique_ptr&lt;GfxRenderWindow&gt; make(HWND windowHandle); }; </code></pre> <p>Now this factory class need to be included by the "client" of the library (here a renderer). What I don't like is that #include "windows.h", because it is too error prone to me, anybody that include it, even if they don't need it will have all the world and windows.... Precompile header is not a solution because now it is enforced by the compiler that all cpp have include it (it is a valuable tool to speed compile time but not a tool to separate platform specific code from the portable code)</p> <p>What I thought is to put the #include in the cpp before the include of its header instead of in the header file like this:</p> <pre><code>//GfxRenderWindowFactory.cpp #define WIN32_LEAN_AND_MEAN #define NOMINMAX #include &lt;windows.h&gt; #include "GfxRenderWindowFactory.h" /// Implementation of GfxRenderWindowFactory goed here .... </code></pre> <p>This way it force anybody that want to use this class to include the relevant platform specific header and they will be in better position to judge if they are including this header in a bad place like in one of their own header file.</p> <p>What are your solution for this? What do you think of my solution?, crazy?</p> <p>I want to point out that to me it is of the uberimportance to do portable code right! Answer like just include windows.h and don't sweat about it is not a valid answer. It is not a good coding practice to me.</p> <p>I hope i made my question clear. If not tell me i'll clarify</p> <p>Thanks a lot!</p> <p>## Edit ## From a small conversation with hmjd I would like to keep the inclusion of windows.h in the header file since, i agree, this make it way more usable. So it would be nice to have a way to prevent the inclusion in a header file and this way enforce that the file can only be included in a cpp. Is this possible?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload