Commit ff7ce07d authored by David Gobbi's avatar David Gobbi

Simplify extern template declaration

For C++11 (and for some pre-C++11 compilers via extensions) a template
instantiation can be declared "extern" so that the compiler will avoid
generating more than one copy of the code for that template.

This commit is an attempt to simplify the use of extern template in VTK.
parent be8d2a8e
Pipeline #25731 passed with stage
...@@ -163,7 +163,7 @@ ...@@ -163,7 +163,7 @@
/** extern template declarations for C++11 /** extern template declarations for C++11
*/ */
#if __cplusplus >= 201103L && !defined(_MSC_VER) && !defined(__MINGW32__) #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1700)
#define VTK_USE_EXTERN_TEMPLATE #define VTK_USE_EXTERN_TEMPLATE
#endif #endif
......
...@@ -320,5 +320,30 @@ inline vtkTypeBool vtkDataTypesCompare(int a, int b) ...@@ -320,5 +320,30 @@ inline vtkTypeBool vtkDataTypesCompare(int a, int b)
} }
#endif #endif
/*--------------------------------------------------------------------------*/
/** A macro to instantiate a template over all numerical types */
#define vtkInstantiateTemplateMacro(decl) \
decl<float>; \
decl<double>; \
decl<char>; \
decl<signed char>; \
decl<unsigned char>; \
decl<short>; \
decl<unsigned short>; \
decl<int>; \
decl<unsigned int>; \
decl<long>; \
decl<unsigned long>; \
decl<long long>; \
decl<unsigned long long>;
/** A macro to declare extern templates for all numerical types */
#ifdef VTK_USE_EXTERN_TEMPLATE
#define vtkExternTemplateMacro(decl) \
vtkInstantiateTemplateMacro(decl)
#else
#define vtkExternTemplateMacro(decl)
#endif
#endif #endif
// VTK-HeaderTest-Exclude: vtkType.h // VTK-HeaderTest-Exclude: vtkType.h
...@@ -13,18 +13,10 @@ ...@@ -13,18 +13,10 @@
=========================================================================*/ =========================================================================*/
#define vtkImageIterator_cxx
#include "vtkImageIterator.txx" #include "vtkImageIterator.txx"
template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator<signed char>; vtkInstantiateTemplateMacro(
template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator<char>; template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator
template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator<int>; )
template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator<long>;
template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator<long long>;
template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator<short>;
template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator<float>;
template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator<double>;
template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator<unsigned long>;
template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator<unsigned long long>;
template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator<unsigned short>;
template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator<unsigned char>;
template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator<unsigned int>;
...@@ -80,5 +80,11 @@ protected: ...@@ -80,5 +80,11 @@ protected:
vtkIdType ContinuousIncrements[3]; vtkIdType ContinuousIncrements[3];
}; };
#ifndef vtkImageIterator_cxx
vtkExternTemplateMacro(
extern template class VTKCOMMONDATAMODEL_EXPORT vtkImageIterator
)
#endif
#endif #endif
// VTK-HeaderTest-Exclude: vtkImageIterator.h // VTK-HeaderTest-Exclude: vtkImageIterator.h
...@@ -13,33 +13,10 @@ ...@@ -13,33 +13,10 @@
=========================================================================*/ =========================================================================*/
#include "vtkConfigure.h" #define vtkImageProgressIterator_cxx
#if defined(_MSC_VER) && !defined(VTK_DISPLAY_WIN32_WARNINGS)
#pragma warning ( disable : 4275 )
#endif
// Do not include vtkImageIterator.txx here - will cause implicit template
// instantiation, breaking symbol visibility for the iterator with GCC.
// Silencing the MSVC warning after discussions about the fact that the explicit
// instantiation of the this class tries to implicitly instantiate the member
// class. This is not an error, and the symbols should be found at link time.
#if defined(_MSC_VER)
# pragma warning (disable:4661)
#endif
#include "vtkImageProgressIterator.txx" #include "vtkImageProgressIterator.txx"
template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator<signed char>; vtkInstantiateTemplateMacro(
template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator<char>; template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator
template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator<int>; )
template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator<long>;
template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator<long long>;
template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator<short>;
template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator<float>;
template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator<double>;
template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator<unsigned long>;
template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator<unsigned long long>;
template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator<unsigned short>;
template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator<unsigned char>;
template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator<unsigned int>;
...@@ -58,5 +58,11 @@ protected: ...@@ -58,5 +58,11 @@ protected:
int ID; int ID;
}; };
#ifndef vtkImageProgressIterator_cxx
vtkExternTemplateMacro(
extern template class VTKCOMMONEXECUTIONMODEL_EXPORT vtkImageProgressIterator
)
#endif
#endif #endif
// VTK-HeaderTest-Exclude: vtkImageProgressIterator.h // VTK-HeaderTest-Exclude: vtkImageProgressIterator.h
...@@ -13,22 +13,10 @@ ...@@ -13,22 +13,10 @@
=========================================================================*/ =========================================================================*/
#include "vtkImageStencilIterator.h" #define vtkImageStencilIterator_cxx
#ifndef VTK_NO_EXPLICIT_TEMPLATE_INSTANTIATION
template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator<signed char>; #include "vtkImageStencilIterator.h"
template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator<char>;
template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator<int>;
template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator<long>;
template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator<short>;
template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator<float>;
template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator<double>;
template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator<unsigned long>;
template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator<unsigned short>;
template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator<unsigned char>;
template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator<unsigned int>;
template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator<long long>;
template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator<unsigned long long>;
#endif vtkInstantiateTemplateMacro(
template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator
)
...@@ -125,5 +125,20 @@ protected: ...@@ -125,5 +125,20 @@ protected:
DType *SpanEndPointer; // end of current span DType *SpanEndPointer; // end of current span
}; };
#ifndef vtkImageStencilIterator_cxx
#ifdef _MSC_VER
#pragma warning (push)
// The following is needed when the vtkImageStencilIterator template
// class is declared dllexport and is used within vtkImagingCore
#pragma warning (disable: 4910) // extern and dllexport incompatible
#endif
vtkExternTemplateMacro(
extern template class VTKIMAGINGCORE_EXPORT vtkImageStencilIterator
)
#ifdef _MSC_VER
#pragma warning (pop)
#endif
#endif
#endif #endif
// VTK-HeaderTest-Exclude: vtkImageStencilIterator.h // VTK-HeaderTest-Exclude: vtkImageStencilIterator.h
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