Commit e12a8157 authored by Brad King's avatar Brad King
Browse files

Add a vtkTemplate2Macro to dispatch to two template arguments at once



The vtkTemplateMacro is useful for dispatching to filter implementations
that use one template argument.  Add a new vtkTemplate2Macro to dispatch
filter implementations that use two template arguments.  Use a single
switch over two VTK type ids packed into a single integer.
Suggested-by: Will Schroeder's avatarWill Schroeder <will.schroeder@kitware.com>
parent e94b24ff
......@@ -47,6 +47,7 @@ vtk_add_test_cxx(${vtk-module}CxxTests tests
TestSortDataArray.cxx
TestSparseArrayValidation.cxx
TestSystemInformation.cxx
TestTemplateMacro.cxx
TestTimePointUtility.cxx
TestUnicodeStringAPI.cxx
TestUnicodeStringArrayAPI.cxx
......
/*=========================================================================
Program: Visualization Toolkit
Module: ObjectFactory.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSetGet.h"
template <typename T1, typename T2>
static void myFunc1(T1* p1, T2* p2)
{
*p2 = T2(*p1);
}
static bool RunTemplate2Macro1(int tIn, void* pIn,
int tOut, void* pOut)
{
switch (vtkTemplate2PackMacro(tIn, tOut))
{
// Test implicit deduction of multiple template arguments.
vtkTemplate2Macro(myFunc1(static_cast<VTK_T1*>(pIn),
static_cast<VTK_T2*>(pOut)));
default:
// Unknown input or output VTK type id.
return false;
break;
}
return true;
}
template <typename T1, typename T2>
static void myFunc2(void* p1, void* p2)
{
*static_cast<T2*>(p2) = T2(*static_cast<T1*>(p1));
}
static bool RunTemplate2Macro2(int tIn, void* pIn,
int tOut, void* pOut)
{
switch (vtkTemplate2PackMacro(tIn, tOut))
{
// Test explicit specification of multiple template arguments.
vtkTemplate2Macro((myFunc2<VTK_T1,VTK_T2>(pIn, pOut)));
default:
// Unknown input or output VTK type id.
return false;
break;
}
return true;
}
template <int NIn, typename TIn, int NOut, typename TOut>
static bool TestTemplate2Macro()
{
TIn in = 1;
TOut out = 0;
if (!RunTemplate2Macro1(NIn, &in, NOut, &out) || out != 1)
{ return false; }
in = 2;
if (!RunTemplate2Macro2(NIn, &in, NOut, &out) || out != 2)
{ return false; }
return true;
}
int TestTemplateMacro(int, char *[])
{
bool res = true;
// Verify that a few combinations are dispatched.
res = TestTemplate2Macro<VTK_FLOAT, float, VTK_INT, int>() && res;
res = TestTemplate2Macro<VTK_DOUBLE, double,
VTK_ID_TYPE, vtkIdType>() && res;
res = TestTemplate2Macro<VTK_INT, int, VTK_LONG, long>() && res;
res = TestTemplate2Macro<VTK_CHAR, char, VTK_LONG, long>() && res;
// Verify that bad VTK type ids are rejected.
res = !TestTemplate2Macro<127, char, VTK_LONG, long>() && res;
res = !TestTemplate2Macro<VTK_CHAR, char, 127, long>() && res;
return res? EXIT_SUCCESS : EXIT_FAILURE;
}
......@@ -692,6 +692,61 @@ virtual double *Get##name() \
vtkTemplateMacro(call); \
vtkTemplateMacroCase(VTK_STRING, vtkStdString, call)
// The vtkTemplate2Macro is used to dispatch like vtkTemplateMacro but
// over two template arguments instead of one.
//
// Example usage:
// switch(vtkTemplate2PackMacro(array1->GetDataType(),
// array2->GetDataType()))
// {
// vtkTemplateMacro(myFunc(static_cast<VTK_T1*>(data1),
// static_cast<VTK_T2*>(data2),
// otherArg));
// }
#define vtkTemplate2Macro(call) \
vtkTemplate2MacroCase1(VTK_DOUBLE, double, call); \
vtkTemplate2MacroCase1(VTK_FLOAT, float, call); \
vtkTemplate2MacroCase1_ll(VTK_LONG_LONG, long long, call) \
vtkTemplate2MacroCase1_ll(VTK_UNSIGNED_LONG_LONG, unsigned long long, call) \
vtkTemplate2MacroCase1_si64(VTK___INT64, __int64, call) \
vtkTemplate2MacroCase1_ui64(VTK_UNSIGNED___INT64, unsigned __int64, call) \
vtkTemplate2MacroCase1(VTK_ID_TYPE, vtkIdType, call); \
vtkTemplate2MacroCase1(VTK_LONG, long, call); \
vtkTemplate2MacroCase1(VTK_UNSIGNED_LONG, unsigned long, call); \
vtkTemplate2MacroCase1(VTK_INT, int, call); \
vtkTemplate2MacroCase1(VTK_UNSIGNED_INT, unsigned int, call); \
vtkTemplate2MacroCase1(VTK_SHORT, short, call); \
vtkTemplate2MacroCase1(VTK_UNSIGNED_SHORT, unsigned short, call); \
vtkTemplate2MacroCase1(VTK_CHAR, char, call); \
vtkTemplate2MacroCase1(VTK_SIGNED_CHAR, signed char, call); \
vtkTemplate2MacroCase1(VTK_UNSIGNED_CHAR, unsigned char, call)
#define vtkTemplate2MacroCase1(type1N, type1, call) \
vtkTemplate2MacroCase2(type1N, type1, VTK_DOUBLE, double, call); \
vtkTemplate2MacroCase2(type1N, type1, VTK_FLOAT, float, call); \
vtkTemplate2MacroCase2_ll(type1N, type1, VTK_LONG_LONG, long long, call) \
vtkTemplate2MacroCase2_ll(type1N, type1, VTK_UNSIGNED_LONG_LONG, unsigned long long, call) \
vtkTemplate2MacroCase2_si64(type1N, type1, VTK___INT64, __int64, call) \
vtkTemplate2MacroCase2_ui64(type1N, type1, VTK_UNSIGNED___INT64, unsigned __int64, call) \
vtkTemplate2MacroCase2(type1N, type1, VTK_ID_TYPE, vtkIdType, call); \
vtkTemplate2MacroCase2(type1N, type1, VTK_LONG, long, call); \
vtkTemplate2MacroCase2(type1N, type1, VTK_UNSIGNED_LONG, unsigned long, call); \
vtkTemplate2MacroCase2(type1N, type1, VTK_INT, int, call); \
vtkTemplate2MacroCase2(type1N, type1, VTK_UNSIGNED_INT, unsigned int, call); \
vtkTemplate2MacroCase2(type1N, type1, VTK_SHORT, short, call); \
vtkTemplate2MacroCase2(type1N, type1, VTK_UNSIGNED_SHORT, unsigned short, call); \
vtkTemplate2MacroCase2(type1N, type1, VTK_CHAR, char, call); \
vtkTemplate2MacroCase2(type1N, type1, VTK_SIGNED_CHAR, signed char, call); \
vtkTemplate2MacroCase2(type1N, type1, VTK_UNSIGNED_CHAR, unsigned char, call)
#define vtkTemplate2MacroCase2(type1N, type1, type2N, type2, call) \
case vtkTemplate2PackMacro(type1N, type2N): { \
typedef type1 VTK_T1; \
typedef type2 VTK_T2; \
call; \
}; break
#define vtkTemplate2PackMacro(type1N, type2N) \
((((type1N) & 0xFF) << 8) | \
((type2N) & 0xFF))
// The vtkArrayIteratorTemplateMacro is used to centralize the set of types
// supported by Execute methods. It also avoids duplication of long
// switch statement case lists.
......@@ -738,10 +793,16 @@ virtual double *Get##name() \
#if defined(VTK_TYPE_USE_LONG_LONG)
# define vtkTemplateMacroCase_ll(typeN, type, call) \
vtkTemplateMacroCase(typeN, type, call);
# define vtkTemplate2MacroCase1_ll(type1N, type1, call) \
vtkTemplate2MacroCase1(type1N, type1, call);
# define vtkTemplate2MacroCase2_ll(type1N, type1, type2N, type2, call) \
vtkTemplate2MacroCase2(type1N, type1, type2N, type2, call);
# define vtkArrayIteratorTemplateMacroCase_ll(typeN, type, call) \
vtkArrayIteratorTemplateMacroCase(typeN, type, call)
#else
# define vtkTemplateMacroCase_ll(typeN, type, call)
# define vtkTemplate2MacroCase1_ll(type1N, type1, call)
# define vtkTemplate2MacroCase2_ll(type1N, type1, type2N, type2, call)
# define vtkArrayIteratorTemplateMacroCase_ll(typeN, type, call)
#endif
......@@ -749,10 +810,16 @@ virtual double *Get##name() \
#if defined(VTK_TYPE_USE___INT64)
# define vtkTemplateMacroCase_si64(typeN, type, call) \
vtkTemplateMacroCase(typeN, type, call);
# define vtkTemplate2MacroCase1_si64(type1N, type1, call) \
vtkTemplate2MacroCase1(type1N, type1, call);
# define vtkTemplate2MacroCase2_si64(type1N, type1, type2N, type2, call) \
vtkTemplate2MacroCase2(type1N, type1, type2N, type2, call);
# define vtkArrayIteratorTemplateMacroCase_si64(typeN, type, call) \
vtkArrayIteratorTemplateMacroCase(typeN, type, call)
#else
# define vtkTemplateMacroCase_si64(typeN, type, call)
# define vtkTemplate2MacroCase1_si64(type1N, type1, call)
# define vtkTemplate2MacroCase2_si64(type1N, type1, type2N, type2, call)
# define vtkArrayIteratorTemplateMacroCase_si64(typeN, type, call)
#endif
......@@ -761,10 +828,16 @@ virtual double *Get##name() \
#if defined(VTK_TYPE_USE___INT64) && defined(VTK_TYPE_CONVERT_UI64_TO_DOUBLE)
# define vtkTemplateMacroCase_ui64(typeN, type, call) \
vtkTemplateMacroCase(typeN, type, call);
# define vtkTemplate2MacroCase1_ui64(type1N, type1, call) \
vtkTemplate2MacroCase1(type1N, type1, call);
# define vtkTemplate2MacroCase2_ui64(type1N, type1, type2N, type2, call) \
vtkTemplate2MacroCase2(type1N, type1, type2N, type2, call);
# define vtkArrayIteratorTemplateMacroCase_ui64(typeN, type, call) \
vtkArrayIteratorTemplateMacroCase(typeN, type, call);
#else
# define vtkTemplateMacroCase_ui64(typeN, type, call)
# define vtkTemplate2MacroCase1_ui64(type1N, type1, call)
# define vtkTemplate2MacroCase2_ui64(type1N, type1, type2N, type2, call)
# define vtkArrayIteratorTemplateMacroCase_ui64(typeN, type, call)
#endif
......
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