Skip to content

Commit

Permalink
ENH: Use ITK_<factory_name>_FACTORY_REGISTER_MANAGER
Browse files Browse the repository at this point in the history
Add specific CMake variables and compile defines for each type for
factory.

This splits the CMake variable ITK_NO_IO_FACTORY_REGISTER_MANAGER into

- ITK_NO_IMAGEIO_FACTORY_REGISTER_MANAGER
- ITK_NO_TRANSFORMIO_FACTORY_REGISTER_MANAGER
- ITK_NO_MESHIO_FACTORY_REGISTER_MANAGER

To give finer grain control and follow the FFT option:
- ITK_NO_FFT_FACTORY_REGISTER_MANAGER
  • Loading branch information
blowekamp committed Jan 11, 2022
1 parent 52c7d74 commit 099b562
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 35 deletions.
56 changes: 35 additions & 21 deletions CMake/ITKFactoryRegistration.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Introduction
# ------------
#
# ITK IO factories can be registered automatically either "statically" or "dynamically".
# ITK factories can be registered automatically either "statically" or "dynamically".
#
# "statically" : This is the mechanism described below and supported by UseITK.
#
Expand All @@ -11,26 +11,31 @@
# associated with the `ITK_AUTOLOAD_PATH` environment variable.
#
#
# Overview: Static registration of ITK IO factories
# -------------------------------------------------
# Overview: Static registration of ITK factories
# ----------------------------------------------
#
# For each factory type (Image, Transform, Mesh, ...), a registration manager header
# named `itk<factory_type>IOFactoryRegisterManager.h` is configured.
# For each factory type (Image, Transform, Mesh, FFT, ...), a
# registration manager header named
# `itk<factory_type>FactoryRegisterManager.h` is configured.
#
# The registration manager header is itself included at the end of the Reader and
# Writer header of each factory types. It will ensure all the IO factories
# The registration manager header is itself included at the end of the Reader,
# Writer and FFT headers for each factory types. It will ensure all the factories
# associated with the different file formats are registered only once by
# relying on static initialization of a global variable properly initialized
# by the loader across all translation units.
#
# By including either `itk<factory_type>FileReader.h` or `itk<factory_type>FileWriter.h`
# By including `itk<factory_type>FileReader.h` or `itk<factory_type>FileWriter.h`
# header in user code, the corresponding IO factories will be ensured to be
# registered globally and available across translation units.
#
# The file formats associated with each factory type are hard-coded as
# The file formats associated with each IO factory type are hard-coded as
# a list in a CMake variable named after `LIST_OF_<factory_type>IO_FORMATS`
# generated by this file.
#
# For the FFT factories, each FFT filter which has a factory needs to
# be included.
#
#
#
# Registration manager header
# ---------------------------
Expand All @@ -40,16 +45,19 @@
# respectively contain a list of function declaration and calls. The associated
# function names are of the form `<factory_name>FactoryRegister__Private`.
#
# These variables are set by iterating over the IO format lists.
#
#
# Disabling static registration
# -----------------------------
#
# Setting variable `ITK_NO_IO_FACTORY_REGISTER_MANAGER` to `OFF` prior calling
# `include(${ITK_USE_FILE})` disables the static registration. As a consequence,
# the two variables `LIST_OF_FACTORY_NAMES` and `LIST_OF_FACTORIES_REGISTRATION`
# are empty and no calls to "Private" function is done.
# Setting variable `ITK_NO_<factory_name_uc>_FACTORY_REGISTER_MANAGER` to `OFF` prior calling
# `include(${ITK_USE_FILE})` disables the static registration.
#
# All factories can be disabled with the following CMake code:
# foreach(_factory_name ${ITK_FACTORY_LIST})
# string(TOLOWER ${_factory_name} _f)
# set ( ITK_NO_${_factory_name}_FACTORY_REGISTER_MANAGER 1 )
# endforeach()
#
#
# IO format lists
# ---------------
Expand Down Expand Up @@ -101,9 +109,12 @@
# ----------------------------------------
#
# The configuration of the registration header for each factory is done
# using the convenience function `_itk_configure_FactoryRegisterManager()`.
# using the convenience function
# `itk_generate_factory_registration()`.
#
# If `UseITK.cmake` is not included into a project this CMake method
# may be called to generate the `Private()` functions.
#
# It expects a list of file format associated with each factory types.
#
# By iterating over the format list, the CMake function `_itk_configure_FactoryRegisterManager()`
# will itself call `_itk_ADD_FACTORY_REGISTRATION()` to generate the Private function
Expand Down Expand Up @@ -182,6 +193,7 @@ endmacro()
# generated, if no arguments are provided then ITK_FACTORY_LIST is
# used for the list of managers generated.
#
# Output cmake variables : LIST_OF_{FACTORY}_FORMATS
#-----------------------------------------------------------------------------
macro(itk_generate_factory_registration)
set(_factory_list ${ITK_FACTORY_LIST})
Expand All @@ -191,6 +203,7 @@ macro(itk_generate_factory_registration)
if (${_argc} GREATER 0)
set(_factory_list ${variadic_args})
endif()

foreach(_factory_name ${ITK_FACTORY_LIST})
string(TOUPPER ${_factory_name} factory_uc)
string(TOLOWER ${_factory_name} factory_lc)
Expand All @@ -211,10 +224,11 @@ macro(itk_generate_factory_registration)
set(${_format}_${factory_lc}_module_name ${Module})
set(${_format}_${factory_lc}_factory_name ${_format}${_factory_name})
endforeach()
if(NOT ITK_NO_${factory_uc}_FACTORY_REGISTER_MANAGER)
# pass generation IO factory registration
elseif(_factory_name MATCHES "FFT" AND ITK_NO_FFT_FACTORY_REGISTER_MANAGER)
# pass generation of FFT factory registration
if(ITK_NO_${factory_uc}_FACTORY_REGISTER_MANAGER)
# pass generation this factory registration
elseif(_factory_name MATCHES "IO" AND ITK_NO_IO_FACTORY_REGISTER_MANAGER)
message(WARNING "ITK_NO_IO_FACTORY_REGISTER_MANAGER CMake variable
is deprecated. Use ITK_NO_${_factory_name}_FACTORY_REGISTER_MANAGER")
else()
_itk_configure_FactoryRegisterManager("${_factory_name}" "${LIST_OF_${factory_uc}_FORMATS}")
endif()
Expand Down
30 changes: 23 additions & 7 deletions CMake/UseITK.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,30 @@ link_directories(${ITK_LIBRARY_DIRS})

itk_generate_factory_registration()


#-----------------------------------------------------------------------------
if(NOT ITK_NO_IO_FACTORY_REGISTER_MANAGER)
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS ITK_IO_FACTORY_REGISTER_MANAGER)
include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR}/ITKFactoryRegistration)
endif()

if(NOT ITK_NO_FFT_FACTORY_REGISTER_MANAGER)
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS ITK_FFT_FACTORY_REGISTER_MANAGER)
set(_need_include 0)
foreach(_factory_name ${ITK_FACTORY_LIST})
string(TOUPPER ${_factory_name} _factory_uc)

if (_factory_name MATCHES "IO" AND
ITK_NO_IO_FACTORY_REGISTER_MANAGER)
if( "${ITK_VERSION_MAJOR}.${ITK_VERSION_MINOR}" VERSION_GREATER_EQUAL "5.4")
message(WARNING "ITK_NO_IO_FACTORY_REGISTER_MANAGER CMake
variable is deprecated. Use ITK_NO_${_factory_uc}_FACTORY_REGISTER_MANAGER")
endif()
continue()
endif()

if(NOT ITK_NO_${_factory_uc}_FACTORY_REGISTER_MANAGER)
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS
ITK_${_factory_uc}_FACTORY_REGISTER_MANAGER)
set(_need_include 1)

endif()
endforeach()

if (_need_include)
include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR}/ITKFactoryRegistration)
endif()
unset(_need_include)
2 changes: 1 addition & 1 deletion Modules/IO/ImageBase/include/itkImageFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ ReadImage(const std::string & filename)
# include "itkImageFileReader.hxx"
#endif

#ifdef ITK_IO_FACTORY_REGISTER_MANAGER
#if defined ITK_IMAGEIO_FACTORY_REGISTER_MANAGER || defined ITK_IO_FACTORY_REGISTER_MANAGER
# include "itkImageIOFactoryRegisterManager.h"
#endif

Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/ImageBase/include/itkImageFileWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ WriteImage(TImagePointer && image, const std::string & filename, bool compress =
# include "itkImageFileWriter.hxx"
#endif

#ifdef ITK_IO_FACTORY_REGISTER_MANAGER
#if defined ITK_IMAGEIO_FACTORY_REGISTER_MANAGER || defined ITK_IO_FACTORY_REGISTER_MANAGER
# include "itkImageIOFactoryRegisterManager.h"
#endif

Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/MeshBase/include/itkMeshFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ ReadMesh(const std::string & filename)
# include "itkMeshFileReader.hxx"
#endif

#ifdef ITK_IO_FACTORY_REGISTER_MANAGER
#if defined ITK_MESHIO_FACTORY_REGISTER_MANAGER || defined ITK_IO_FACTORY_REGISTER_MANAGER
# include "itkMeshIOFactoryRegisterManager.h"
#endif

Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/MeshBase/include/itkMeshFileWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ WriteMesh(TMeshPointer && mesh, const std::string & filename, bool compress = fa
# include "itkMeshFileWriter.hxx"
#endif

#ifdef ITK_IO_FACTORY_REGISTER_MANAGER
#if defined ITK_MESHIO_FACTORY_REGISTER_MANAGER || defined ITK_IO_FACTORY_REGISTER_MANAGER
# include "itkMeshIOFactoryRegisterManager.h"
#endif

Expand Down
4 changes: 2 additions & 2 deletions Modules/IO/TransformBase/include/itkTransformFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ using TransformFileReader = itk::TransformFileReaderTemplate<double>;

} // namespace itk

#ifdef ITK_IO_FACTORY_REGISTER_MANAGER
#if defined ITK_TRANSFORMIO_FACTORY_REGISTER_MANAGER || defined ITK_IO_FACTORY_REGISTER_MANAGER
# include "itkTransformIOFactoryRegisterManager.h"
#endif

// Note: Explicit instantiation is done in itkTransformFileReader.cxx

#endif // itkTransformFileReade_h
#endif // itkTransformFileReader_h

/** Explicit instantiations */
#ifndef ITK_TEMPLATE_EXPLICIT_TransformFileReader
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/TransformBase/include/itkTransformFileWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ ITK_GCC_PRAGMA_DIAG_POP()

} // namespace itk

#ifdef ITK_IO_FACTORY_REGISTER_MANAGER
#if defined ITK_TRANSFORMIO_FACTORY_REGISTER_MANAGER || defined ITK_IO_FACTORY_REGISTER_MANAGER
# include "itkTransformIOFactoryRegisterManager.h"
#endif

Expand Down

0 comments on commit 099b562

Please sign in to comment.