VTK  9.1.0
vtkObjectFactory.cmake
Go to the documentation of this file.
1#[==[
2@file vtkObjectFactory.cmake
3
4In VTK, certain classes can have what are called "implementations". When the
5base class is requested, it instead looks into a list of available
6implementations. One of the implementations is then constructed and returned
7instead.
8
9For instance, there is a `vtkRenderWindow` class in VTK. When this is
10constructed, it instead actually returns a window for the X window system,
11Cocoa, or Win32 depending on what is available.
12
13VTK's implementation utilizes the [autoinit](@ref module-autoinit) logic of the
14module system. A module which contains an object factory must declare itself as
15`IMPLEMENTABLE` and modules which contain an implementation of an object
16factory must claim that they `IMPLEMENTS` modules containing those base object
17factories (a module may contain the object factory and an implementation; it
18then says that it `IMPLEMENTS` itself).
19#]==]
20
21set(_vtkObjectFactory_source_dir "${CMAKE_CURRENT_LIST_DIR}")
22
23#[==[
24@brief Declare a factory override
25
26Declare that a class in this module (the implementation) is an `OVERRIDE` for a
27base class.
28
29~~~
31 BASE <base>
32 OVERRIDE <implementation>)
33~~~
34#]==]
36 cmake_parse_arguments(PARSE_ARGV 0 _vtk_object_factory_declare
37 ""
38 "BASE;OVERRIDE"
39 "")
40
41 if (_vtk_object_factory_declare_UNPARSED_ARGUMENTS)
42 message(FATAL_ERROR
43 "Unparsed arguments for vtk_object_factory_declare: "
44 "${_vtk_object_factory_declare_UNPARSED_ARGUMENTS}")
45 endif ()
46
47 if (NOT DEFINED _vtk_object_factory_declare_BASE)
48 message(FATAL_ERROR
49 "The `BASE` argument is required.")
50 endif ()
51
52 if (NOT DEFINED _vtk_object_factory_declare_OVERRIDE)
53 message(FATAL_ERROR
54 "The `OVERRIDE` argument is required.")
55 endif ()
56
57 set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND
58 PROPERTY
59 _vtk_object_factory_overrides "${_vtk_object_factory_declare_OVERRIDE}")
60 set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND
61 PROPERTY
62 "_vtk_object_factory_override_${_vtk_object_factory_declare_OVERRIDE}" "${_vtk_object_factory_declare_BASE}")
63endfunction ()
64
65#[==[
66@brief Generate source for overrides in a module
67
68A module may only have a single declaration of all its object factory
69implementations. This function generates the source for all of the overrides
70declared using @ref vtk_object_factory_declare.
71
72~~~
73vtk_object_factory_configure(
74 SOURCE_FILE <variable>
75 [HEADER_FILE <variable>]
76 [EXPORT_MACRO <macro>]
77 [INITIAL_CODE <code>]
78 [EXTRA_INCLUDES <include>...])
79~~~
80
81 - `SOURCE_FILE`: (Required) A variable to set to the path to generated source
82 file.
83 - `HEADER_FILE`: (Recommended) A variable to set to the path to generated
84 header file. This should not be treated as a public header.
85 - `EXPORT_MACRO`: (Recommended) The export macro to add to the generated
86 class.
87 - `INITIAL_CODE`: C++ code to run when the object factory is initialized.
88 - `EXTRA_INCLUDES`: A list of headers to include. The header names need to
89 include the `<>` or `""` quoting.
90#]==]
91function (vtk_object_factory_configure)
92 if (NOT DEFINED _vtk_build_module)
93 message(FATAL_ERROR
94 "The `vtk_object_factory_configure` function needs to be run within a module context.")
95 endif ()
96
97 cmake_parse_arguments(PARSE_ARGV 0 _vtk_object_factory_configure
98 ""
99 "SOURCE_FILE;HEADER_FILE;INITIAL_CODE;EXPORT_MACRO"
100 "EXTRA_INCLUDES")
101
102 if (_vtk_object_factory_configure_UNPARSED_ARGUMENTS)
103 message(FATAL_ERROR
104 "Unparsed arguments for vtk_object_factory_configure: "
105 "${_vtk_object_factory_configure_UNPARSED_ARGUMENTS}")
106 endif ()
107
108 get_property(_vtk_object_factory_done
109 GLOBAL
110 PROPERTY "_vtk_object_factory_${_vtk_build_module}"
111 SET)
112 if (_vtk_object_factory_done)
113 message(FATAL_ERROR
114 "An object factory has already been created for ${_vtk_build_module}.")
115 else ()
116 set_property(GLOBAL
117 PROPERTY
118 "_vtk_object_factory_${_vtk_build_module}" 1)
119 endif ()
120
121 get_property(_vtk_object_factory_overrides
122 DIRECTORY
123 PROPERTY _vtk_object_factory_overrides)
124
125 if (NOT _vtk_object_factory_overrides)
126 message(WARNING
127 "The ${_vtk_build_module} is generating an object factory, but does not have any declared overrides.")
128 endif ()
129
130 set(_vtk_object_factory_doc
131 "Override for ${_vtk_build_module} module")
132
133 set(_vtk_object_factory_includes "")
134 set(_vtk_object_factory_functions "")
135 set(_vtk_object_factory_calls "")
136
137 foreach (_vtk_object_factory_extra_include IN LISTS _vtk_object_factory_configure_EXTRA_INCLUDES)
138 string(APPEND _vtk_object_factory_includes
139 "#include ${_vtk_object_factory_extra_include}\n")
140 endforeach ()
141
142 foreach (_vtk_object_factory_override IN LISTS _vtk_object_factory_overrides)
143 get_property(_vtk_object_factory_base
144 DIRECTORY
145 PROPERTY "_vtk_object_factory_override_${_vtk_object_factory_override}")
146 string(APPEND _vtk_object_factory_includes
147 "#include \"${_vtk_object_factory_override}.h\"\n")
148 string(APPEND _vtk_object_factory_functions
149 "VTK_CREATE_CREATE_FUNCTION(${_vtk_object_factory_override})\n")
150 string(APPEND _vtk_object_factory_calls
151 "this->RegisterOverride(\"${_vtk_object_factory_base}\", \"${_vtk_object_factory_override}\", \"${_vtk_object_factory_doc}\", 1, vtkObjectFactoryCreate${_vtk_object_factory_override});\n")
152 endforeach ()
153
154 get_property(_vtk_object_factory_library_name GLOBAL
155 PROPERTY "_vtk_module_${_vtk_build_module}_library_name")
156
157 set(_vtk_object_factory_overrides_header
158 "${CMAKE_CURRENT_BINARY_DIR}/${_vtk_object_factory_library_name}ObjectFactory.h")
159 set(_vtk_object_factory_overrides_source
160 "${CMAKE_CURRENT_BINARY_DIR}/${_vtk_object_factory_library_name}ObjectFactory.cxx")
161
162 configure_file(
163 "${_vtkObjectFactory_source_dir}/vtkObjectFactory.h.in"
164 "${_vtk_object_factory_overrides_header}"
165 @ONLY)
166 configure_file(
167 "${_vtkObjectFactory_source_dir}/vtkObjectFactory.cxx.in"
168 "${_vtk_object_factory_overrides_source}"
169 @ONLY)
170
171 if (_vtk_object_factory_configure_HEADER_FILE)
172 set("${_vtk_object_factory_configure_HEADER_FILE}"
173 "${_vtk_object_factory_overrides_header}"
174 PARENT_SCOPE)
175 endif ()
176
177 set("${_vtk_object_factory_configure_SOURCE_FILE}"
178 "${_vtk_object_factory_overrides_source}"
179 PARENT_SCOPE)
180endfunction ()
abstract base class for vtkObjectFactories
create a window for renderers to draw into
@ on
Definition: vtkX3D.h:445
@ string
Definition: vtkX3D.h:496
function vtk_object_factory_configure()
Generate source for overrides in a module.
function vtk_object_factory_declare()
Declare a factory override.