Visual studio: hiding ifdef blocks

When working in a cross-platform project, most likely you will come across a lot of #ifdef blocks in the code. This is actually very annoying and makes it difficult to read code.

I wonder if there is a way to hide those unnecessary ifdef blocks? I am using Visual Studio 2005.

For example, if you have this code:

#ifdef _PC_ do a #else do b #endif 

If _PC_ defined, the tool / plugin should only show this:

 do A 
+2
source share
5 answers

Although my answer is not quite what you are looking for, it is the best I know.

In Visual Studio 2005, it may be possible to use the selection tool to collapse the unused portion of the #ifdef block.

Another idea that comes to mind is to split the code of a particular OS into its own files and use the main file containing #ifdef blocks and significant parts (i.e. full declarations) of code that do not change on different platforms.

Another thing that comes to mind is simply to group code for conditional blocks where possible. There are many situations where the order of the code may not matter much, and you can group this code together.

Another thing that could be done if you would like to use this file as reference material is to run the file through a preprocessor that processes only conditional blocks and ignores everything that is not required to process conditional blocks.

+3
source

The cleanest method I've seen (and used) is to use the Strategy Pattern , in which you can abstract from specific platform code into a separate strategy class.

eg.

 class MyAbstractClass { static MyAbstractClass Create() { #ifdef win32 return new WindowsClass(); // this could be a shared_ptr #else return new PosixClass(); #endif } virtual method MyMethod() = 0; } // windows_class.cpp class WindowsClass : public MyAbstractClass { virtual method MyMethod() { // platform specific code here } } // posix_class.cpp class PosixClass : public MyAbstractClass { virtual method MyMethod() { // platform specific code here } } 
+1
source

I suggest not doing what you asked for. Hiding the fact that your code is multi-platform can easily lead to you, or to someone else, not realizing that the code they relate to needs to be changed for each platform.

For example, what if you add a line immediately after the line do A? How do you know if it goes inside or outside the block? This new line that you add may also need a corresponding alternative line in another section. Or, if it goes beyond the block, you can break up another version without even knowing it.

It may seem uncomfortable, but it is a language. It’s best to get used to the language, displaying the way it is created and like everyone else, instead of trying to hack things into your preference.

+1
source

Just use the following preprocessor directive:

 #region IfDef #ifdef PC doSomethingCalledA(); #else doSomethingCalledB(); #endif #endregion 
0
source

Set up a solution for each platform you work on, with the corresponding macros defined in each. Include source files in all solutions. Then go through and collapse sections of code that do not match the platform for the current solution.

Visual Studio will save your aggregated section settings for each solution, so you will see different extended code depending on which project you open. I think this is stored in * .suo files.

0
source

Source: https://habr.com/ru/post/1388603/


All Articles