Commit dcb7de9b authored by Ben Boeckel's avatar Ben Boeckel

vtkObjectFactory: update for the new module system

parent 68cdd62c
# This file provides several helper macros/functions to aid in overrideing
# the VTK object factories. These methods should be used to ensure that certain
# interface classes have valid overrides provided in implementation modules.
# If the interface class doesn't use one of the object factory based new macros
# in C++ then the overrides will not work as expected.
# Add a new override, base is the base class name and override is the name of
# the class that should override. For example,
# vtk_add_override(vtkRenderer vtkOpenGLRenderer)
macro(vtk_add_override base override)
list(APPEND vtk_module_overrides ${base})
set(vtk_module_${base}_override ${override})
endmacro()
# Create the relevant object factory files from the override list. This uses
# ${vtk-module} to construct the file names (${vtk-module}ObjectFactory.[h,cxx]
# in the current binary directory.
function(vtk_object_factory_configure override_list)
# Now we iterate and create that class file...
foreach(_class ${override_list})
set(_override ${vtk_module_${_class}_override})
set(_vtk_override_includes
"${_vtk_override_includes}\n#include \"${_override}.h\"")
set(_vtk_override_creates "${_vtk_override_creates}
VTK_CREATE_CREATE_FUNCTION(${_override})")
set(_vtk_override_do "${_vtk_override_do}
this->RegisterOverride(\"${_class}\",
\"${_override}\",
\"Override for ${vtk-module} module\", 1,
vtkObjectFactoryCreate${_override});")
endforeach()
string(TOUPPER ${vtk-module} VTK-MODULE)
configure_file(${VTK_CMAKE_DIR}/vtkObjectFactory.h.in
${CMAKE_CURRENT_BINARY_DIR}/${vtk-module}ObjectFactory.h)
configure_file(${VTK_CMAKE_DIR}/vtkObjectFactory.cxx.in
${CMAKE_CURRENT_BINARY_DIR}/${vtk-module}ObjectFactory.cxx)
endfunction()
#[==[.md
# Object Factories
In VTK, certain classes can have what are called "implementations". When the
base class is requested, it instead looks into a list of available
implementations. One of the implementations is then constructed and returned
instead.
For instance, there is a `vtkRenderWindow` class in VTK. When this is
constructed, it instead actually returns a window for the X window system,
Cocoa, or Win32 depending on what is available.
VTK's implementation utilizes the "autoinit" logic of the module system. A
module which contains an object factory must declare itself as `IMPLEMENTABLE`
and modules which contain an implementation of an object factory must claim
that they `IMPLEMENTS` modules containing those base object factories (a module
may contain the object factory and an implementation; it then says that it
`IMPLEMENTS` itself).
#]==]
set(_vtkObjectFactory_source_dir "${CMAKE_CURRENT_LIST_DIR}")
#[==[.md
## Declaring a factory override
In order to declare an override, the `vtk_object_factory_declare` function must
be used.
```
vtk_object_factory_declare(
BASE <base>
OVERRIDE <implementation>)
```
#]==]
function (vtk_object_factory_declare)
cmake_parse_arguments(_vtk_object_factory_declare
""
"BASE;OVERRIDE"
""
${ARGN})
if (_vtk_object_factory_declare_UNPARSED_ARGUMENTS)
message(FATAL_ERROR
"Unparsed arguments for vtk_object_factory_declare: "
"${_vtk_object_factory_declare_UNPARSED_ARGUMENTS}")
endif ()
if (NOT DEFINED _vtk_object_factory_declare_BASE)
message(FATAL_ERROR
"The `BASE` argument is required.")
endif ()
if (NOT DEFINED _vtk_object_factory_declare_OVERRIDE)
message(FATAL_ERROR
"The `OVERRIDE` argument is required.")
endif ()
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND
PROPERTY
_vtk_object_factory_overrides "${_vtk_object_factory_declare_OVERRIDE}")
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND
PROPERTY
"_vtk_object_factory_override_${_vtk_object_factory_declare_OVERRIDE}" "${_vtk_object_factory_declare_BASE}")
endfunction ()
#[==[.md
## Generating the source for a module
A module may only have a single declaration of all its object factory
implementations. This is done using the `vtk_object_factory_configure`
function.
```
vtk_object_factory_configure(
[SOURCE_FILE <variable>]
[HEADER_FILE <variable>]
[EXPORT_MACRO <macro>]
[INITIAL_CODE <code>]
[EXTRA_INCLUDES <include>...])
```
- `SOURCE_FILE`: (Recommended) A variable to set to the path to generated
source file.
- `HEADER_FILE`: (Recommended) A variable to set to the path to generated
header file. This should not be treated as a public header.
- `EXPORT_MACRO`: (Recommended) The export macro to add to the generated
class.
- `INITIAL_CODE`: C++ code to run when the object factory is initialized.
- `EXTRA_INCLUDES`: A list of headers to include. The header names need to
include the `<>` or `""` quoting.
#]==]
function (vtk_object_factory_configure)
if (NOT DEFINED _vtk_build_module)
message(FATAL_ERROR
"The `vtk_object_factory_configure` function needs to be run within a module context.")
endif ()
cmake_parse_arguments(_vtk_object_factory_configure
""
"SOURCE_FILE;HEADER_FILE;INITIAL_CODE;EXPORT_MACRO"
"EXTRA_INCLUDES"
${ARGN})
if (_vtk_object_factory_configure_UNPARSED_ARGUMENTS)
message(FATAL_ERROR
"Unparsed arguments for vtk_object_factory_configure: "
"${_vtk_object_factory_configure_UNPARSED_ARGUMENTS}")
endif ()
get_property(_vtk_object_factory_done
GLOBAL
PROPERTY "_vtk_object_factory_${_vtk_build_module}"
SET)
if (_vtk_object_factory_done)
message(FATAL_ERROR
"An object factory has already been created for ${_vtk_build_module}.")
else ()
set_property(GLOBAL
PROPERTY
"_vtk_object_factory_${_vtk_build_module}" 1)
endif ()
get_property(_vtk_object_factory_overrides
DIRECTORY
PROPERTY _vtk_object_factory_overrides)
if (NOT _vtk_object_factory_overrides)
message(WARNING
"The ${_vtk_build_module} is generating an object factory, but does not have any declared overrides.")
endif ()
set(_vtk_object_factory_doc
"Override for ${_vtk_build_module} module")
set(_vtk_object_factory_includes "")
set(_vtk_object_factory_functions "")
set(_vtk_object_factory_calls "")
foreach (_vtk_object_factory_extra_include IN LISTS _vtk_object_factory_configure_EXTRA_INCLUDES)
set(_vtk_object_factory_includes
"${_vtk_object_factory_includes}#include ${_vtk_object_factory_extra_include}\n")
endforeach ()
foreach (_vtk_object_factory_override IN LISTS _vtk_object_factory_overrides)
get_property(_vtk_object_factory_base
DIRECTORY
PROPERTY "_vtk_object_factory_override_${_vtk_object_factory_override}")
set(_vtk_object_factory_includes
"${_vtk_object_factory_includes}#include \"${_vtk_object_factory_override}.h\"\n")
set(_vtk_object_factory_functions
"${_vtk_object_factory_functions}VTK_CREATE_CREATE_FUNCTION(${_vtk_object_factory_override})\n")
set(_vtk_object_factory_calls
"${_vtk_object_factory_calls}this->RegisterOverride(\"${_vtk_object_factory_base}\", \"${_vtk_object_factory_override}\", \"${_vtk_object_factory_doc}\", 1, vtkObjectFactoryCreate${_vtk_object_factory_override});\n")
endforeach ()
get_property(_vtk_object_factory_library_name GLOBAL
PROPERTY "_vtk_module_${_vtk_build_module}_library_name")
set(_vtk_object_factory_overrides_header
"${CMAKE_CURRENT_BINARY_DIR}/${_vtk_object_factory_library_name}ObjectFactory.h")
set(_vtk_object_factory_overrides_source
"${CMAKE_CURRENT_BINARY_DIR}/${_vtk_object_factory_library_name}ObjectFactory.cxx")
configure_file(
"${_vtkObjectFactory_source_dir}/vtkObjectFactory.h.in"
"${_vtk_object_factory_overrides_header}"
@ONLY)
configure_file(
"${_vtkObjectFactory_source_dir}/vtkObjectFactory.cxx.in"
"${_vtk_object_factory_overrides_source}"
@ONLY)
if (_vtk_object_factory_configure_HEADER_FILE)
set("${_vtk_object_factory_configure_HEADER_FILE}"
"${_vtk_object_factory_overrides_header}"
PARENT_SCOPE)
endif ()
if (_vtk_object_factory_configure_SOURCE_FILE)
set("${_vtk_object_factory_configure_SOURCE_FILE}"
"${_vtk_object_factory_overrides_source}"
PARENT_SCOPE)
endif ()
endfunction ()
/*=========================================================================
Program: Visualization Toolkit
Module: @vtk-module@ObjectFactory.cxx
Module: @_vtk_object_factory_library_name@ObjectFactory.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
......@@ -13,55 +13,56 @@
=========================================================================*/
#include "@vtk-module@ObjectFactory.h"
#include "@_vtk_object_factory_library_name@ObjectFactory.h"
#include "vtkVersion.h"
// Include all of the classes we want to create overrides for.
@_vtk_override_includes@
@_vtk_object_factory_includes@
vtkStandardNewMacro(@vtk-module@ObjectFactory)
vtkStandardNewMacro(@_vtk_object_factory_library_name@ObjectFactory)
// Now create the functions to create overrides with.
@_vtk_override_creates@
@_vtk_object_factory_functions@
@vtk-module@ObjectFactory::@vtk-module@ObjectFactory()
@_vtk_object_factory_library_name@ObjectFactory::@_vtk_object_factory_library_name@ObjectFactory()
{
@_vtk_override_do@
@_vtk_object_factory_calls@
}
const char * @vtk-module@ObjectFactory::GetVTKSourceVersion()
const char * @_vtk_object_factory_library_name@ObjectFactory::GetVTKSourceVersion()
{
return VTK_SOURCE_VERSION;
}
void @vtk-module@ObjectFactory::PrintSelf(ostream &os, vtkIndent indent)
void @_vtk_object_factory_library_name@ObjectFactory::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
// Registration of object factories.
static unsigned int @vtk-module@Count;
static unsigned int @_vtk_object_factory_library_name@Count;
@VTK-MODULE@_EXPORT void @vtk-module@_AutoInit_Construct()
@_vtk_object_factory_configure_EXPORT_MACRO@ void @_vtk_object_factory_library_name@_AutoInit_Construct()
{
if(++@vtk-module@Count == 1)
{
@vtk-object-factory-extra-init@
@vtk-module@ObjectFactory* factory = @vtk-module@ObjectFactory::New();
if(++@_vtk_object_factory_library_name@Count == 1)
{
@_vtk_object_factory_configure_INITIAL_CODE@
@_vtk_object_factory_library_name@ObjectFactory* factory = @_vtk_object_factory_library_name@ObjectFactory::New();
if (factory)
{
{
// vtkObjectFactory keeps a reference to the "factory",
vtkObjectFactory::RegisterFactory(factory);
factory->Delete();
}
}
}
}
@VTK-MODULE@_EXPORT void @vtk-module@_AutoInit_Destruct()
@_vtk_object_factory_configure_EXPORT_MACRO@ void @_vtk_object_factory_library_name@_AutoInit_Destruct()
{
if(--@vtk-module@Count == 0)
{
if(--@_vtk_object_factory_library_name@Count == 0)
{
// Do not call vtkObjectFactory::UnRegisterFactory because
// vtkObjectFactory.cxx statically unregisters all factories.
}
}
}
/*=========================================================================
Program: Visualization Toolkit
Module: @vtk-module@ObjectFactory.h
Module: @_vtk_object_factory_library_name@ObjectFactory.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
......@@ -13,30 +13,30 @@
=========================================================================*/
#ifndef @vtk-module@ObjectFactory_h
#define @vtk-module@ObjectFactory_h
#ifndef @_vtk_object_factory_library_name@ObjectFactory_h
#define @_vtk_object_factory_library_name@ObjectFactory_h
#include "@vtk-module@Module.h" // For export macro
#include "@_vtk_object_factory_library_name@Module.h" // For export macro
#include "vtkObjectFactory.h"
class @VTK-MODULE@_EXPORT @vtk-module@ObjectFactory : public vtkObjectFactory
class @_vtk_object_factory_configure_EXPORT_MACRO@ @_vtk_object_factory_library_name@ObjectFactory : public vtkObjectFactory
{
public:
static @vtk-module@ObjectFactory * New();
vtkTypeMacro(@vtk-module@ObjectFactory, vtkObjectFactory)
static @_vtk_object_factory_library_name@ObjectFactory * New();
vtkTypeMacro(@_vtk_object_factory_library_name@ObjectFactory, vtkObjectFactory)
const char * GetDescription() override { return "@vtk-module@ factory overrides."; }
const char * GetDescription() override { return "@_vtk_object_factory_library_name@ factory overrides."; }
const char * GetVTKSourceVersion() override;
void PrintSelf(ostream &os, vtkIndent indent) override;
protected:
@vtk-module@ObjectFactory();
@_vtk_object_factory_library_name@ObjectFactory();
private:
@vtk-module@ObjectFactory(const @vtk-module@ObjectFactory&) = delete;
void operator=(const @vtk-module@ObjectFactory&) = delete;
@_vtk_object_factory_library_name@ObjectFactory(const @_vtk_object_factory_library_name@ObjectFactory&) = delete;
void operator=(const @_vtk_object_factory_library_name@ObjectFactory&) = delete;
};
#endif // @vtk-module@ObjectFactory_h
#endif // @_vtk_object_factory_library_name@ObjectFactory_h
......@@ -52,6 +52,7 @@ include(vtkCrossCompiling)
include(vtkThirdParty)
include(vtkThreads)
include(vtkWrapSettings)
include(vtkObjectFactory)
# Setup compiler flags for dynamic analysis
# Should be included after vtkTesting
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment