Add tag-based type list templates

Provies a list of types in a template like boost::mpl::vector and a
method to call a functor on each type. However, rather than explicitly
list each type, uses tags to identify the list. This provides the
following main advantages:

1. Can use these type lists without creating horrendously long class
names based on them, making compiler errors easier to read. For example,
you would have a typename like MyClass<TypeListTagVectors> instead of
MyClass<TypeList<Id3,Vector2,Vector3,Vector4> > (or worse if variadic
templates are not supported). This is the main motivation for this
implementation.

2. Do not require variadic templates and usually few constructions. That
should speed compile times.

There is one main disadvantage to this approach: It is difficult to get
a printed list of items in a list during an error. If necessary, it
probably would not be too hard to make a template to convert a tag to a
boost mpl vector.
This commit is contained in:
Kenneth Moreland 2014-03-31 15:28:09 -06:00
parent c008213561
commit c2d926fa49
10 changed files with 655 additions and 0 deletions

@ -21,6 +21,8 @@
include_directories(${Boost_INCLUDE_DIRS})
set(headers
ListTag.h
TypeListTag.h
Types.h
TypeTraits.h
VectorTraits.h

201
vtkm/ListTag.h Normal file

@ -0,0 +1,201 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt 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.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_ListTag_h
#define vtk_m_ListTag_h
#include <vtkm/internal/ExportMacros.h>
namespace vtkm {
namespace detail {
template<typename T>
struct ListBase { };
template<typename T1, typename T2>
struct ListBase2 { };
template<typename T1, typename T2, typename T3>
struct ListBase3 { };
template<typename T1, typename T2, typename T3, typename T4>
struct ListBase4 { };
template<typename ListTag1, typename ListTag2>
struct ListJoin { };
} // namespace detail
template<typename T> struct ListTagBase;
/// A basic tag for a list of one typename. This struct can be subclassed
/// and still behave like a list tag.
template<typename T>
struct ListTagBase {
typedef detail::ListBase<T> List;
};
/// A basic tag for a list of two typenames. This struct can be subclassed
/// and still behave like a list tag.
template<typename T1, typename T2>
struct ListTagBase2 {
typedef detail::ListBase2<T1, T2> List;
};
/// A basic tag for a list of three typenames. This struct can be subclassed
/// and still behave like a list tag.
template<typename T1, typename T2, typename T3>
struct ListTagBase3 {
typedef detail::ListBase3<T1, T2, T3> List;
};
/// A basic tag for a list of four typenames. This struct can be subclassed
/// and still behave like a list tag.
template<typename T1, typename T2, typename T3, typename T4>
struct ListTagBase4 {
typedef detail::ListBase4<T1, T2, T3, T4> List;
};
/// A tag that is a construction of two other tags joined together. This struct
/// can be subclassed and still behave like a list tag.
template<typename ListTag1, typename ListTag2>
struct ListTagJoin {
typedef detail::ListJoin<ListTag1, ListTag2> List;
};
template<typename Functor, typename ListTag>
VTKM_CONT_EXPORT
void ListForEach(Functor &f, ListTag);
template<typename Functor, typename ListTag>
VTKM_CONT_EXPORT
void ListForEach(const Functor &f, ListTag);
namespace detail {
template<typename Functor, typename T>
VTKM_CONT_EXPORT
void ListForEachImpl(Functor &f, ListBase<T>)
{
f(T());
}
template<typename Functor, typename T1, typename T2>
VTKM_CONT_EXPORT
void ListForEachImpl(Functor &f, ListBase2<T1,T2>)
{
f(T1());
f(T2());
}
template<typename Functor, typename T1, typename T2, typename T3>
VTKM_CONT_EXPORT
void ListForEachImpl(Functor &f, ListBase3<T1,T2,T3>)
{
f(T1());
f(T2());
f(T3());
}
template<typename Functor, typename T1, typename T2, typename T3, typename T4>
VTKM_CONT_EXPORT
void ListForEachImpl(Functor &f, ListBase4<T1,T2,T3,T4>)
{
f(T1());
f(T2());
f(T3());
f(T4());
}
template<typename Functor, typename ListTag1, typename ListTag2>
VTKM_CONT_EXPORT
void ListForEachImpl(Functor &f, ListJoin<ListTag1, ListTag2>)
{
vtkm::ListForEach(f, ListTag1());
vtkm::ListForEach(f, ListTag2());
}
template<typename Functor, typename T>
VTKM_CONT_EXPORT
void ListForEachImpl(const Functor &f, ListBase<T>)
{
f(T());
}
template<typename Functor, typename T1, typename T2>
VTKM_CONT_EXPORT
void ListForEachImpl(const Functor &f, ListBase2<T1,T2>)
{
f(T1());
f(T2());
}
template<typename Functor, typename T1, typename T2, typename T3>
VTKM_CONT_EXPORT
void ListForEachImpl(const Functor &f, ListBase3<T1,T2,T3>)
{
f(T1());
f(T2());
f(T3());
}
template<typename Functor, typename T1, typename T2, typename T3, typename T4>
VTKM_CONT_EXPORT
void ListForEachImpl(const Functor &f, ListBase4<T1,T2,T3,T4>)
{
f(T1());
f(T2());
f(T3());
f(T4());
}
template<typename Functor, typename ListTag1, typename ListTag2>
VTKM_CONT_EXPORT
void ListForEachImpl(const Functor &f, ListJoin<ListTag1, ListTag2>)
{
vtkm::ListForEach(f, ListTag1());
vtkm::ListForEach(f, ListTag2());
}
} // namespace detail
/// For each typename represented by the list tag, call the functor with a
/// default instance of that type.
///
template<typename Functor, typename ListTag>
VTKM_CONT_EXPORT
void ListForEach(Functor &f, ListTag)
{
detail::ListForEachImpl(f, typename ListTag::List());
}
/// For each typename represented by the list tag, call the functor with a
/// default instance of that type.
///
template<typename Functor, typename ListTag>
VTKM_CONT_EXPORT
void ListForEach(const Functor &f, ListTag)
{
detail::ListForEachImpl(f, typename ListTag::List());
}
} // namespace vtkm
#endif //vtk_m_ListTag_h

64
vtkm/TypeListTag.h Normal file

@ -0,0 +1,64 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt 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.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_TypeListTag_h
#define vtk_m_TypeListTag_h
#ifndef VTKM_DEFAULT_TYPE_LIST_TAG
#define VTKM_DEFAULT_TYPE_LIST_TAG ::vtkm::TypeListTagCommon;
#endif
#include <vtkm/ListTag.h>
#include <vtkm/Types.h>
namespace vtkm {
struct TypeListTagId : vtkm::ListTagBase<vtkm::Id> { };
struct TypeListTagId2 : vtkm::ListTagBase<vtkm::Id2> { };
struct TypeListTagId3 : vtkm::ListTagBase<vtkm::Id3> { };
struct TypeListTagScalar : vtkm::ListTagBase<vtkm::Scalar> { };
struct TypeListTagVector2 : vtkm::ListTagBase<vtkm::Vector2> { };
struct TypeListTagVector3 : vtkm::ListTagBase<vtkm::Vector3> { };
struct TypeListTagVector4 : vtkm::ListTagBase<vtkm::Vector4> { };
struct TypeListTagIndex
: vtkm::ListTagBase3<vtkm::Id,vtkm::Id2,vtkm::Id3> { };
struct TypeListTagReal
: vtkm::ListTagBase4<vtkm::Scalar,vtkm::Vector2,vtkm::Vector3,vtkm::Vector4>
{ };
/// A list of all basic types listed in vtkm/Types.h. Does not include all
/// possible VTK-m types like arbitrarily typed and sized tuples or math
/// types like matrices.
///
struct TypeListTagAll
: vtkm::ListTagJoin<vtkm::TypeListTagIndex, vtkm::TypeListTagReal>
{ };
/// A list of the most commonly used types across multiple domains. Includes
/// Id, Scalar, and Vector3.
///
struct TypeListTagCommon
: vtkm::ListTagBase3<vtkm::Id, vtkm::Scalar, vtkm::Vector3>
{ };
} // namespace vtkm
#endif //vtk_m_TypeListTag_h

@ -27,6 +27,7 @@ set(headers
ArrayHandle.h
ArrayPortal.h
Assert.h
ContainerListTag.h
DeviceAdapter.h
DeviceAdapterSerial.h
Error.h

@ -0,0 +1,40 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt 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.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_ContainerListTag_h
#define vtk_m_ContainerListTag_h
#ifndef VTKM_DEFAULT_CONTAINER_LIST_TAG
#define VTKM_DEFAULT_CONTAINER_LIST_TAG ::vtkm::cont::ContainerListTagBasic;
#endif
#include <vtkm/ListTag.h>
#include <vtkm/cont/ArrayContainerControl.h>
namespace vtkm {
namespace cont {
struct ContainerListTagBasic
: vtkm::ListTagBase<vtkm::cont::ArrayContainerControlTagBasic> { };
}
} // namespace vtkm::cont
#endif //vtk_m_ContainerListTag_h

@ -30,6 +30,7 @@ set(unit_tests
UnitTestArrayContainerControlImplicit.cxx
UnitTestArrayHandle.cxx
UnitTestArrayHandleCounting.cxx
UnitTestContainerListTag.cxx
UnitTestContTesting.cxx
UnitTestDeviceAdapterAlgorithmDependency.cxx
UnitTestDeviceAdapterAlgorithmGeneral.cxx

@ -0,0 +1,79 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt 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.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#include <vtkm/cont/ContainerListTag.h>
#include <vtkm/cont/testing/Testing.h>
#include <vector>
namespace {
enum TypeId {
BASIC
};
TypeId GetTypeId(vtkm::cont::ArrayContainerControlTagBasic) { return BASIC; }
struct TestFunctor
{
std::vector<TypeId> FoundTypes;
template<typename T>
VTKM_CONT_EXPORT
void operator()(T) {
this->FoundTypes.push_back(GetTypeId(T()));
}
};
template<int N>
void CheckSame(const vtkm::Tuple<TypeId,N> &expected,
const std::vector<TypeId> &found)
{
VTKM_TEST_ASSERT(static_cast<int>(found.size()) == N,
"Got wrong number of items.");
for (int index = 0; index < N; index++)
{
VTKM_TEST_ASSERT(expected[index] == found[index],
"Got wrong type.");
}
}
template<int N, typename ListTag>
void TryList(const vtkm::Tuple<TypeId,N> &expected, ListTag)
{
TestFunctor functor;
vtkm::ListForEach(functor, ListTag());
CheckSame(expected, functor.FoundTypes);
}
void TestLists()
{
std::cout << "ContainerListTagBasic" << std::endl;
TryList(vtkm::Tuple<TypeId,1>(BASIC), vtkm::cont::ContainerListTagBasic());
}
} // anonymous namespace
int UnitTestContainerListTag(int, char *[])
{
return vtkm::testing::Testing::Run(TestLists);
}

@ -26,7 +26,9 @@ set(headers
VTKM_declare_headers(${headers})
set(unit_tests
UnitTestListTag.cxx
UnitTestTesting.cxx
UnitTestTypeListTag.cxx
UnitTestTypes.cxx
UnitTestTypeTraits.cxx
UnitTestVectorTraits.cxx

@ -0,0 +1,133 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt 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.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#include <vtkm/ListTag.h>
#include <vtkm/Types.h>
#include <vtkm/testing/Testing.h>
#include <vector>
namespace {
template<int N>
struct TestClass
{
static const int NUMBER = N;
};
struct TestListTag1
: vtkm::ListTagBase<TestClass<11> >
{ };
struct TestListTag2
: vtkm::ListTagBase2<TestClass<21>,TestClass<22> >
{ };
struct TestListTag3
: vtkm::ListTagBase3<TestClass<31>,TestClass<32>,TestClass<33> >
{ };
struct TestListTag4
: vtkm::ListTagBase4<TestClass<41>,TestClass<42>,TestClass<43>,TestClass<44> >
{ };
struct TestListTagJoin
: vtkm::ListTagJoin<TestListTag3, TestListTag1>
{ };
struct MutableFunctor
{
std::vector<int> FoundTypes;
template<typename T>
VTKM_CONT_EXPORT
void operator()(T) {
this->FoundTypes.push_back(T::NUMBER);
}
};
std::vector<int> g_FoundType;
struct ConstantFunctor
{
ConstantFunctor() {
g_FoundType.erase(g_FoundType.begin(), g_FoundType.end());
}
template<typename T>
VTKM_CONT_EXPORT
void operator()(T) const {
g_FoundType.push_back(T::NUMBER);
}
};
template<int N>
void CheckSame(const vtkm::Tuple<int,N> &expected,
const std::vector<int> &found)
{
VTKM_TEST_ASSERT(static_cast<int>(found.size()) == N,
"Got wrong number of items.");
for (int index = 0; index < N; index++)
{
VTKM_TEST_ASSERT(expected[index] == found[index],
"Got wrong type.");
}
}
template<int N, typename ListTag>
void TryList(const vtkm::Tuple<int,N> &expected, ListTag)
{
std::cout << " Try mutable for each" << std::endl;
MutableFunctor functor;
vtkm::ListForEach(functor, ListTag());
CheckSame(expected, functor.FoundTypes);
std::cout << " Try constant for each" << std::endl;
vtkm::ListForEach(ConstantFunctor(), ListTag());
CheckSame(expected, g_FoundType);
}
void TestLists()
{
std::cout << "ListTagBase" << std::endl;
TryList(vtkm::Tuple<int,1>(11), TestListTag1());
std::cout << "ListTagBase2" << std::endl;
TryList(vtkm::Tuple<int,2>(21,22), TestListTag2());
std::cout << "ListTagBase3" << std::endl;
TryList(vtkm::Tuple<int,3>(31,32,33), TestListTag3());
std::cout << "ListTagBase4" << std::endl;
TryList(vtkm::Tuple<int,4>(41,42,43,44), TestListTag4());
std::cout << "ListTagJoin" << std::endl;
TryList(vtkm::Tuple<int,4>(31,32,33,11), TestListTagJoin());
}
} // anonymous namespace
int UnitTestListTag(int, char *[])
{
return vtkm::testing::Testing::Run(TestLists);
}

@ -0,0 +1,132 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt 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.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#include <vtkm/TypeListTag.h>
#include <vtkm/Types.h>
#include <vtkm/testing/Testing.h>
#include <vector>
namespace {
enum TypeId {
ID,
ID2,
ID3,
SCALAR,
VECTOR2,
VECTOR3,
VECTOR4
};
TypeId GetTypeId(vtkm::Id) { return ID; }
TypeId GetTypeId(vtkm::Id2) { return ID2; }
TypeId GetTypeId(vtkm::Id3) { return ID3; }
TypeId GetTypeId(vtkm::Scalar) { return SCALAR; }
TypeId GetTypeId(vtkm::Vector2) { return VECTOR2; }
TypeId GetTypeId(vtkm::Vector3) { return VECTOR3; }
TypeId GetTypeId(vtkm::Vector4) { return VECTOR4; }
struct TestFunctor
{
std::vector<TypeId> FoundTypes;
template<typename T>
VTKM_CONT_EXPORT
void operator()(T) {
this->FoundTypes.push_back(GetTypeId(T()));
}
};
template<int N>
void CheckSame(const vtkm::Tuple<TypeId,N> &expected,
const std::vector<TypeId> &found)
{
VTKM_TEST_ASSERT(static_cast<int>(found.size()) == N,
"Got wrong number of items.");
for (int index = 0; index < N; index++)
{
VTKM_TEST_ASSERT(expected[index] == found[index],
"Got wrong type.");
}
}
template<int N, typename ListTag>
void TryList(const vtkm::Tuple<TypeId,N> &expected, ListTag)
{
TestFunctor functor;
vtkm::ListForEach(functor, ListTag());
CheckSame(expected, functor.FoundTypes);
}
void TestLists()
{
std::cout << "TypeListTagId" << std::endl;
TryList(vtkm::Tuple<TypeId,1>(ID), vtkm::TypeListTagId());
std::cout << "TypeListTagId2" << std::endl;
TryList(vtkm::Tuple<TypeId,1>(ID2), vtkm::TypeListTagId2());
std::cout << "TypeListTagId3" << std::endl;
TryList(vtkm::Tuple<TypeId,1>(ID3), vtkm::TypeListTagId3());
std::cout << "TypeListTagScalar" << std::endl;
TryList(vtkm::Tuple<TypeId,1>(SCALAR), vtkm::TypeListTagScalar());
std::cout << "TypeListTagVector2" << std::endl;
TryList(vtkm::Tuple<TypeId,1>(VECTOR2), vtkm::TypeListTagVector2());
std::cout << "TypeListTagVector3" << std::endl;
TryList(vtkm::Tuple<TypeId,1>(VECTOR3), vtkm::TypeListTagVector3());
std::cout << "TypeListTagVector4" << std::endl;
TryList(vtkm::Tuple<TypeId,1>(VECTOR4), vtkm::TypeListTagVector4());
std::cout << "TypeListTagIndex" << std::endl;
TryList(vtkm::Tuple<TypeId,3>(ID,ID2,ID3), vtkm::TypeListTagIndex());
std::cout << "TypeListTagReal" << std::endl;
TryList(vtkm::Tuple<TypeId,4>(SCALAR,VECTOR2,VECTOR3,VECTOR4),
vtkm::TypeListTagReal());
std::cout << "TypeListTagCommon" << std::endl;
TryList(vtkm::Tuple<TypeId,3>(ID,SCALAR,VECTOR3), vtkm::TypeListTagCommon());
std::cout << "TypeListTagAll" << std::endl;
vtkm::Tuple<TypeId,7> allTags;
allTags[0] = ID;
allTags[1] = ID2;
allTags[2] = ID3;
allTags[3] = SCALAR;
allTags[4] = VECTOR2;
allTags[5] = VECTOR3;
allTags[6] = VECTOR4;
TryList(allTags, vtkm::TypeListTagAll());
}
} // anonymous namespace
int UnitTestTypeListTag(int, char *[])
{
return vtkm::testing::Testing::Run(TestLists);
}