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

ENH: Add a level of indirection to vtkVariant creation, so vtkArray can be...

ENH: Add a level of indirection to vtkVariant creation, so vtkArray can be used with user-defined types.
parent e73af9b8
/*=========================================================================
Program: Visualization Toolkit
Module: ArrayUserTypes.cxx
-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------
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 <vtkDenseArray.h>
#include <vtkSmartPointer.h>
#include <vtkSparseArray.h>
#include <vtksys/ios/iostream>
#include <vtksys/ios/sstream>
#include <vtksys/stl/stdexcept>
#define test_expression(expression) \
{ \
if(!(expression)) \
{ \
vtksys_ios::ostringstream buffer; \
buffer << "Expression failed at line " << __LINE__ << ": " << #expression; \
throw vtkstd::runtime_error(buffer.str()); \
} \
}
class UserType
{
public:
UserType() :
Value("")
{
}
UserType(const vtkStdString& value) :
Value(value)
{
}
bool operator==(const UserType& other) const
{
return this->Value == other.Value;
}
vtkStdString Value;
};
template<>
inline UserType vtkVariantCast<UserType>(const vtkVariant& value, bool* valid)
{
if(valid)
*valid = true;
return UserType(value.ToString());
}
template<>
inline vtkVariant vtkVariantCreate<UserType>(const UserType& value)
{
return vtkVariant(value.Value);
}
int ArrayUserTypes(int vtkNotUsed(argc), char *vtkNotUsed(argv)[])
{
try
{
vtkSmartPointer<vtkDenseArray<UserType> > dense = vtkSmartPointer<vtkDenseArray<UserType> >::New();
dense->Resize(3, 4);
dense->Fill(UserType("red"));
for(vtkIdType n = 0; n != dense->GetNonNullSize(); ++n)
test_expression(dense->GetValueN(n) == UserType("red"));
dense->SetValue(1, 2, UserType("green"));
test_expression(dense->GetValue(1, 2) == UserType("green"));
dense->SetVariantValue(1, 2, vtkVariant("puce"));
test_expression(dense->GetValue(1, 2) == UserType("puce"));
test_expression(dense->GetVariantValue(1, 2) == vtkVariant("puce"));
vtkSmartPointer<vtkSparseArray<UserType> > sparse = vtkSmartPointer<vtkSparseArray<UserType> >::New();
sparse->Resize(3, 4);
sparse->SetNullValue(UserType("blue"));
test_expression(sparse->GetNullValue() == UserType("blue"));
test_expression(sparse->GetValue(1, 2) == UserType("blue"));
sparse->SetValue(0, 1, UserType("white"));
test_expression(sparse->GetValue(0, 1) == UserType("white"));
sparse->AddValue(2, 3, UserType("yellow"));
test_expression(sparse->GetValue(2, 3) == UserType("yellow"));
sparse->SetVariantValue(2, 3, vtkVariant("slate"));
test_expression(sparse->GetValue(2, 3) == UserType("slate"));
test_expression(sparse->GetVariantValue(2, 3) == vtkVariant("slate"));
return 0;
}
catch(vtkstd::exception& e)
{
cerr << e.what() << endl;
return 1;
}
}
......@@ -81,6 +81,7 @@ IF(VTK_USE_N_WAY_ARRAYS)
ArrayInterpolationDense.cxx
ArrayNullValues.cxx
ArraySlice.cxx
ArrayUserTypes.cxx
ArrayVariants.cxx
SparseArrayValidation.cxx
EXTRA_INCLUDE vtkTestDriver.h
......
......@@ -20,6 +20,7 @@
=========================================================================*/
#include "vtkVariantCast.h"
#include "vtkVariantCreate.h"
template<typename T>
void vtkTypedArray<T>::PrintSelf(ostream &os, vtkIndent indent)
......@@ -30,13 +31,13 @@ void vtkTypedArray<T>::PrintSelf(ostream &os, vtkIndent indent)
template<typename T>
vtkVariant vtkTypedArray<T>::GetVariantValue(const vtkArrayCoordinates& coordinates)
{
return this->GetValue(coordinates);
return vtkVariantCreate<T>(this->GetValue(coordinates));
}
template<typename T>
vtkVariant vtkTypedArray<T>::GetVariantValueN(const vtkIdType n)
{
return this->GetValueN(n);
return vtkVariantCreate<T>(this->GetValueN(n));
}
template<typename T>
......
......@@ -37,7 +37,11 @@
template<typename T>
T vtkVariantCast(const vtkVariant& value, bool* valid = 0)
{
vtkGenericWarningMacro(<< "cannot cast vtkVariant containing " << value.GetTypeAsString() << " to unsupported type.");
vtkGenericWarningMacro(
<< "Cannot convert vtkVariant containing [" << value.GetTypeAsString() << "] "
<< "to unsupported type [" << typeid(T).name() << "]. "
<< "Create a vtkVariantCast<> specialization to eliminate this warning."
);
if(valid)
*valid = false;
......
/*=========================================================================
Program: Visualization Toolkit
Module: vtkVariantCreate.h
-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------
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.
=========================================================================*/
#ifndef __vtkVariantCreate_h
#define __vtkVariantCreate_h
// .SECTION Thanks
// Developed by Timothy M. Shead (tshead@sandia.gov) at Sandia National Laboratories.
// Description:
// Performs an explicit conversion from an arbitrary type to a vtkVariant. Provides
// callers with a "hook" for defining conversions from user-defined types to vtkVariant.
template<typename T>
vtkVariant vtkVariantCreate(const T& value)
{
vtkGenericWarningMacro(
<< "Cannot convert unsupported type [" << typeid(T).name() << "] to vtkVariant. "
<< "Create a vtkVariantCreate<> specialization to eliminate this warning."
);
return vtkVariant();
}
template<>
inline vtkVariant vtkVariantCreate<char>(const char& value)
{
return value;
}
template<>
inline vtkVariant vtkVariantCreate<unsigned char>(const unsigned char& value)
{
return value;
}
template<>
inline vtkVariant vtkVariantCreate<short>(const short& value)
{
return value;
}
template<>
inline vtkVariant vtkVariantCreate<unsigned short>(const unsigned short& value)
{
return value;
}
template<>
inline vtkVariant vtkVariantCreate<int>(const int& value)
{
return value;
}
template<>
inline vtkVariant vtkVariantCreate<unsigned int>(const unsigned int& value)
{
return value;
}
template<>
inline vtkVariant vtkVariantCreate<long>(const long& value)
{
return value;
}
template<>
inline vtkVariant vtkVariantCreate<unsigned long>(const unsigned long& value)
{
return value;
}
#ifdef VTK_TYPE_USE___INT64
template<>
inline vtkVariant vtkVariantCreate<__int64>(const __int64& value)
{
return value;
}
template<>
inline vtkVariant vtkVariantCreate<unsigned __int64>(const unsigned __int64& value)
{
return value;
}
#endif
#ifdef VTK_TYPE_USE_LONG_LONG
template<>
inline vtkVariant vtkVariantCreate<long long>(const long long& value)
{
return value;
}
template<>
inline vtkVariant vtkVariantCreate<unsigned long long>(const unsigned long long& value)
{
return value;
}
#endif
template<>
inline vtkVariant vtkVariantCreate<float>(const float& value)
{
return value;
}
template<>
inline vtkVariant vtkVariantCreate<double>(const double& value)
{
return value;
}
template<>
inline vtkVariant vtkVariantCreate<vtkStdString>(const vtkStdString& value)
{
return value;
}
template<>
inline vtkVariant vtkVariantCreate<vtkUnicodeString>(const vtkUnicodeString& value)
{
return value;
}
template<>
inline vtkVariant vtkVariantCreate<vtkVariant>(const vtkVariant& value)
{
return value;
}
#endif
......@@ -35,7 +35,11 @@
template<typename T>
T vtkVariantExtract(const vtkVariant& value, bool& valid = 0)
{
vtkGenericWarningMacro(<< "cannot cast vtkVariant containing " << value.GetTypeAsString() << " to unsupported type.");
vtkGenericWarningMacro(
<< "Cannot convert vtkVariant containing [" << value.GetTypeAsString() << "] "
<< "to unsupported type [" << typeid(T).name() << "]. "
<< "Create a vtkVariantExtract<> specialization to eliminate this warning."
);
valid = false;
......
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