Skip to content

Coding Guidelines

Bernhard Fröhler edited this page Jul 13, 2016 · 17 revisions

Includes

There should be as few includes as possible in a header file (ideally none). You only need to include a header file here if you:

  • Derive from a class/struct
  • Use a class/struct as member (but not if you are only using a pointer, reference or smart pointer to that class!)

In all other cases (e.g. when declaring a pointer to a class member , forward-declare any referenced classes).

Example header file:

 #include "BaseClass.h"
 #include "Member.h"
 #include "MySmartPointer.h"
 
 // forward-declarations
 class Type1;
 class Type2;
 class Type3;
 class Type4;
 
 class TestIncludes: public BaseClass // BaseClass.h needs to be included
 {
     Type1* a; // fwd-declaration of PointerType is enough!
     MySmartPointer<Type2> b; // MySmartPointer needs to be included, but Type2 only fwd-declared!
     Member c; // directly using class Member as member, so we also need to include
     void testMethod(Type3 foo, Type4 & bar); // Type3 and Type4 only need to be fwd-declared
 };

In the cpp file, includes should be sorted from application-specific to the most generic headers (typically STL), in order of dependency (http://stackoverflow.com/questions/2762568/c-c-include-file-order-best-practices). The ideal order for our application code therefore is:

  • itk includes
  • vtk includes
  • Qt includes
  • STL & system/platform-specific includes

Include Guards / Pragma Once

Since all supported compilers (Visual C++, g++) support it, the simpler #pragma once should be used.

Precompiled headers

''Note:'' This is work in progress and may not fully work. Enable by marking iAnalyse_USE_PRECOMPILED_HEADERS in cmake. You'll have to use an adapted version of CMake for it to insert the required #include "pch.h" in the _automoc.cpp files. Ask BF for the adapted CMake version.

ITK vs. VTK image types

Rule of Thumb: Use ITK images whenever possible (rationale: I/O is much faster, more processing filters available). Only when you need to show an image, convert it to vtkImageData using an iAConnector. Use helper methods in iAITKIO.h and iATypedCallHelper.h to make filters applicable for multiple different image types.

Clone this wiki locally