Commit bb317378 authored by Timothy M. Shead's avatar Timothy M. Shead
Browse files

BUG: vtkTryDowncast() wasn't returning the correct succeeded/failed result.

parent 208fce91
......@@ -30,22 +30,74 @@
#include <boost/algorithm/string.hpp>
//
//
// Ignore this stuff, it's just for testing ...
//
//
#define VTK_CREATE(type, name) \
vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
#define test_expression(expression) \
{ \
if(!(expression)) \
{ \
vtkstd::ostringstream buffer; \
buffer << "Expression failed at line " << __LINE__ << ": " << #expression; \
throw vtkstd::runtime_error(buffer.str()); \
} \
}
//
//
// Here are some sample functors that end-users might write.
//
//
struct DowncastTest
{
DowncastTest(int& count) :
Count(count)
{
}
template<typename T>
void operator()(T* array) const
{
++Count;
}
int& Count;
};
template<template <typename> class TargetT, typename TypesT>
void SuccessTest(vtkObject* source, int line)
{
int count = 0;
if(!vtkTryDowncast<TargetT, TypesT>(source, DowncastTest(count)))
{
vtkstd::ostringstream buffer;
buffer << "Expression failed at line " << line;
throw vtkstd::runtime_error(buffer.str());
}
if(count != 1)
{
vtkstd::ostringstream buffer;
buffer << "Functor was called " << count << " times at line " << line;
throw vtkstd::runtime_error(buffer.str());
}
}
template<template <typename> class TargetT, typename TypesT>
void FailTest(vtkObject* source, int line)
{
int count = 0;
if(vtkTryDowncast<TargetT, TypesT>(source, DowncastTest(count)))
{
vtkstd::ostringstream buffer;
buffer << "Expression failed at line " << line;
throw vtkstd::runtime_error(buffer.str());
}
if(count != 0)
{
vtkstd::ostringstream buffer;
buffer << "Functor was called " << count << " times at line " << line;
throw vtkstd::runtime_error(buffer.str());
}
}
/*
// This functor increments array values in-place using a parameter passed via the algorithm (instead of a parameter
// stored in the functor). It can work with any numeric array type.
struct IncrementValues
......@@ -108,6 +160,7 @@ struct Transpose
vtkSmartPointer<vtkArray>& ResultMatrix;
};
*/
//
//
......@@ -122,23 +175,49 @@ int main(int vtkNotUsed(argc), char *vtkNotUsed(argv)[])
VTK_CREATE(vtkDenseArray<int>, dense_int);
VTK_CREATE(vtkDenseArray<double>, dense_double);
VTK_CREATE(vtkDenseArray<vtkStdString>, dense_string);
// Calling a functor with extra arguments passed via the algorithm ...
vtkTryDowncast<vtkTypedArray, vtkNumericTypes>(dense_int, IncrementValues(), 1);
vtkTryDowncast<vtkTypedArray, vtkNumericTypes>(dense_double, IncrementValues(), 2);
vtkTryDowncast<vtkTypedArray, vtkNumericTypes>(dense_string, IncrementValues(), 3);
// Alternative syntax: passing arguments via the functor ...
// vtkTryDowncast<vtkTypedArray, vtkNumericTypes>(dense_int, IncrementValues(1));
// vtkTryDowncast<vtkTypedArray, vtkNumericTypes>(dense_double, IncrementValues(2));
// vtkTryDowncast<vtkTypedArray, vtkNumericTypes>(dense_string, IncrementValues(3));
vtkTryDowncast<vtkTypedArray, vtkStringTypes>(dense_int, FoldCase());
vtkTryDowncast<vtkTypedArray, vtkStringTypes>(dense_double, FoldCase());
vtkTryDowncast<vtkTypedArray, vtkStringTypes>(dense_string, FoldCase());
vtkSmartPointer<vtkArray> transposed_matrix;
vtkTryDowncast<vtkDenseArray, vtkAllTypes>(dense_double, Transpose(transposed_matrix));
VTK_CREATE(vtkSparseArray<int>, sparse_int);
VTK_CREATE(vtkSparseArray<double>, sparse_double);
VTK_CREATE(vtkSparseArray<vtkStdString>, sparse_string);
SuccessTest<vtkTypedArray, vtkIntegerTypes>(dense_int, __LINE__);
FailTest<vtkTypedArray, vtkIntegerTypes>(dense_double, __LINE__);
FailTest<vtkTypedArray, vtkIntegerTypes>(dense_string, __LINE__);
SuccessTest<vtkTypedArray, vtkIntegerTypes>(sparse_int, __LINE__);
FailTest<vtkTypedArray, vtkIntegerTypes>(sparse_double, __LINE__);
FailTest<vtkTypedArray, vtkIntegerTypes>(sparse_string, __LINE__);
FailTest<vtkTypedArray, vtkFloatingPointTypes>(dense_int, __LINE__);
SuccessTest<vtkTypedArray, vtkFloatingPointTypes>(dense_double, __LINE__);
FailTest<vtkTypedArray, vtkFloatingPointTypes>(dense_string, __LINE__);
FailTest<vtkTypedArray, vtkFloatingPointTypes>(sparse_int, __LINE__);
SuccessTest<vtkTypedArray, vtkFloatingPointTypes>(sparse_double, __LINE__);
FailTest<vtkTypedArray, vtkFloatingPointTypes>(sparse_string, __LINE__);
SuccessTest<vtkTypedArray, vtkNumericTypes>(dense_int, __LINE__);
SuccessTest<vtkTypedArray, vtkNumericTypes>(dense_double, __LINE__);
FailTest<vtkTypedArray, vtkNumericTypes>(dense_string, __LINE__);
SuccessTest<vtkTypedArray, vtkNumericTypes>(sparse_int, __LINE__);
SuccessTest<vtkTypedArray, vtkNumericTypes>(sparse_double, __LINE__);
FailTest<vtkTypedArray, vtkNumericTypes>(sparse_string, __LINE__);
FailTest<vtkTypedArray, vtkStringTypes>(dense_int, __LINE__);
FailTest<vtkTypedArray, vtkStringTypes>(dense_double, __LINE__);
SuccessTest<vtkTypedArray, vtkStringTypes>(dense_string, __LINE__);
FailTest<vtkTypedArray, vtkStringTypes>(sparse_int, __LINE__);
FailTest<vtkTypedArray, vtkStringTypes>(sparse_double, __LINE__);
SuccessTest<vtkTypedArray, vtkStringTypes>(sparse_string, __LINE__);
SuccessTest<vtkTypedArray, vtkAllTypes>(dense_int, __LINE__);
SuccessTest<vtkTypedArray, vtkAllTypes>(dense_double, __LINE__);
SuccessTest<vtkTypedArray, vtkAllTypes>(dense_string, __LINE__);
SuccessTest<vtkTypedArray, vtkAllTypes>(sparse_int, __LINE__);
SuccessTest<vtkTypedArray, vtkAllTypes>(sparse_double, __LINE__);
SuccessTest<vtkTypedArray, vtkAllTypes>(sparse_string, __LINE__);
SuccessTest<vtkDenseArray, vtkAllTypes>(dense_int, __LINE__);
FailTest<vtkDenseArray, vtkAllTypes>(sparse_int, __LINE__);
FailTest<vtkSparseArray, vtkAllTypes>(dense_int, __LINE__);
SuccessTest<vtkSparseArray, vtkAllTypes>(sparse_int, __LINE__);
return 0;
}
......
......@@ -97,11 +97,11 @@ IF(VTK_USE_N_WAY_ARRAYS)
ADD_TEST(Array-${TName} ${CXX_TEST_PATH}/ArrayCxxTests ${TName})
ENDFOREACH(test)
# IF(VTK_USE_BOOST)
# ADD_EXECUTABLE(ArrayCasting ArrayCasting.cxx)
# TARGET_LINK_LIBRARIES(ArrayCasting vtkCommon)
# ADD_TEST(Array-ArrayCasting ${CXX_TEST_PATH}/ArrayCasting)
# ENDIF()
IF(VTK_USE_BOOST)
ADD_EXECUTABLE(ArrayCasting ArrayCasting.cxx)
TARGET_LINK_LIBRARIES(ArrayCasting vtkCommon)
ADD_TEST(Array-ArrayCasting ${CXX_TEST_PATH}/ArrayCasting)
ENDIF()
ENDIF(VTK_USE_N_WAY_ARRAYS)
......@@ -50,10 +50,10 @@ typedef boost::mpl::joint_view<vtkNumericTypes, vtkStringTypes> vtkAllTypes;
template<template <typename> class TargetT, typename FunctorT>
struct vtkTryDowncastHelper
{
vtkTryDowncastHelper(vtkObject* source, FunctorT functor) :
vtkTryDowncastHelper(vtkObject* source, FunctorT functor, bool& succeeded) :
Source(source),
Functor(functor),
Succeeded(false)
Succeeded(succeeded)
{
}
......@@ -72,17 +72,17 @@ struct vtkTryDowncastHelper
vtkObject* Source;
FunctorT Functor;
mutable bool Succeeded;
bool& Succeeded;
};
template<template <typename> class TargetT, typename FunctorT, typename Arg1T>
struct vtkTryDowncastHelper1
{
vtkTryDowncastHelper1(vtkObject* source, FunctorT functor, Arg1T arg1) :
vtkTryDowncastHelper1(vtkObject* source, FunctorT functor, Arg1T arg1, bool& succeeded) :
Source(source),
Functor(functor),
Arg1(arg1),
Succeeded(false)
Succeeded(succeeded)
{
}
......@@ -102,22 +102,22 @@ struct vtkTryDowncastHelper1
vtkObject* Source;
FunctorT Functor;
Arg1T Arg1;
mutable bool Succeeded;
bool& Succeeded;
};
template<template <typename> class TargetT, typename TypesT, typename FunctorT>
bool vtkTryDowncast(vtkObject* source, FunctorT functor)
{
vtkTryDowncastHelper<TargetT, FunctorT> helper(source, functor);
boost::mpl::for_each<TypesT>(helper);
return helper.Succeeded;
bool succeeded = false;
boost::mpl::for_each<TypesT>(vtkTryDowncastHelper<TargetT, FunctorT>(source, functor, succeeded));
return succeeded;
}
template<template <typename> class TargetT, typename TypesT, typename FunctorT, typename Arg1T>
bool vtkTryDowncast(vtkObject* source, FunctorT functor, Arg1T arg1)
{
vtkTryDowncastHelper1<TargetT, FunctorT, Arg1T> helper(source, functor, arg1);
boost::mpl::for_each<TypesT>(helper);
return helper.Succeeded;
bool succeeded = false;
boost::mpl::for_each<TypesT>(vtkTryDowncastHelper1<TargetT, FunctorT, Arg1T>(source, functor, arg1, succeeded));
return succeeded;
}
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