Add POSIX assert wrapper

Add in the vtkm namespace an assert macro (technically VTKM_ASSERT) that
basically replicates the functionality of the POSIX assert macro. This
form of assert is set to replace the separate control/exection asserts.

It has been decided that an assert that throws an exception instead of
terminating the program is not all that great of a feature and it causes
some limitations on how it is used. The next commit will remove the
other forms of VTK-m assert.
This commit is contained in:
Kenneth Moreland 2016-04-20 14:19:22 -06:00
parent eede71f918
commit 2ddad8bcc5
5 changed files with 69 additions and 0 deletions

45
vtkm/Assert.h Normal file

@ -0,0 +1,45 @@
//============================================================================
// 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 2016 Sandia Corporation.
// Copyright 2016 UT-Battelle, LLC.
// Copyright 2016 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_Assert_h
#define vtk_m_Assert_h
#include <assert.h>
/// \def VTKM_ASSERT(condition)
///
/// Asserts that \a condition resolves to true. If \a condition is false,
/// then a diagnostic message is outputted and execution is terminated. The
/// behavior is essentially the same as the POSIX assert macro, but is
/// wrapped for added portability.
///
/// Like the POSIX assert macro, the check will be removed when compiling
/// in non-debug mode (specifically when NDEBUG is defined), so be prepared
/// for the possibility that the condition is never evaluated.
///
#if !defined(NDEBUG)
#define VTKM_ASSERT(condition) \
assert(condition)
#else
#define VTKM_ASSERT(condition)
#endif
#endif //vtk_m_Assert_h

@ -21,6 +21,7 @@
include_directories(${Boost_INCLUDE_DIRS})
set(headers
Assert.h
BinaryPredicates.h
BinaryOperators.h
CellShape.h

@ -22,6 +22,7 @@
#ifndef vtk_m_Matrix_h
#define vtk_m_Matrix_h
#include <vtkm/Assert.h>
#include <vtkm/Math.h>
#include <vtkm/Types.h>
#include <vtkm/TypeTraits.h>
@ -59,6 +60,8 @@ public:
VTKM_EXEC_CONT_EXPORT
const vtkm::Vec<ComponentType, NUM_COLUMNS> &
operator[](vtkm::IdComponent rowIndex) const {
VTKM_ASSERT(rowIndex >= 0);
VTKM_ASSERT(rowIndex < this->NUM_ROWS);
return this->Components[rowIndex];
}
@ -68,6 +71,8 @@ public:
VTKM_EXEC_CONT_EXPORT
vtkm::Vec<ComponentType, NUM_COLUMNS> &
operator[](vtkm::IdComponent rowIndex) {
VTKM_ASSERT(rowIndex >= 0);
VTKM_ASSERT(rowIndex < this->NUM_ROWS);
return this->Components[rowIndex];
}
@ -77,6 +82,10 @@ public:
VTKM_EXEC_CONT_EXPORT
const ComponentType &
operator()(vtkm::IdComponent rowIndex, vtkm::IdComponent colIndex) const {
VTKM_ASSERT(rowIndex >= 0);
VTKM_ASSERT(rowIndex < this->NUM_ROWS);
VTKM_ASSERT(colIndex >= 0);
VTKM_ASSERT(colIndex < this->NUM_COLUMNS);
return (*this)[rowIndex][colIndex];
}
@ -86,6 +95,10 @@ public:
VTKM_EXEC_CONT_EXPORT
ComponentType &
operator()(vtkm::IdComponent rowIndex, vtkm::IdComponent colIndex) {
VTKM_ASSERT(rowIndex >= 0);
VTKM_ASSERT(rowIndex < this->NUM_ROWS);
VTKM_ASSERT(colIndex >= 0);
VTKM_ASSERT(colIndex < this->NUM_COLUMNS);
return (*this)[rowIndex][colIndex];
}

@ -24,6 +24,8 @@
#include <vtkm/internal/Configure.h>
#include <vtkm/internal/ExportMacros.h>
#include <vtkm/Assert.h>
VTKM_THIRDPARTY_PRE_INCLUDE
#include <boost/mpl/or.hpp>
#include <boost/type_traits/is_floating_point.hpp>
@ -806,11 +808,15 @@ public:
VTKM_EXEC_CONT_EXPORT
const ComponentType &operator[](vtkm::IdComponent idx) const
{
VTKM_ASSERT(idx >= 0);
VTKM_ASSERT(idx < this->NUM_COMPONENTS);
return this->Components[idx];
}
VTKM_EXEC_CONT_EXPORT
ComponentType &operator[](vtkm::IdComponent idx)
{
VTKM_ASSERT(idx >= 0);
VTKM_ASSERT(idx < this->NUM_COMPONENTS);
return this->Components[idx];
}

@ -20,6 +20,7 @@
#ifndef vtk_m_VecVariable_h
#define vtk_m_VecVariable_h
#include <vtkm/Assert.h>
#include <vtkm/Math.h>
#include <vtkm/Types.h>
#include <vtkm/TypeTraits.h>
@ -48,6 +49,7 @@ public:
VecVariable(const vtkm::VecVariable<ComponentType,SrcSize> &src)
: NumComponents(src.GetNumberOfComponents())
{
VTKM_ASSERT(this->NumComponents <= MaxSize);
for (vtkm::IdComponent index = 0; index < this->NumComponents; index++)
{
this->Data[index] = src[index];
@ -59,6 +61,7 @@ public:
VecVariable(const vtkm::Vec<ComponentType,SrcSize> &src)
: NumComponents(SrcSize)
{
VTKM_ASSERT(this->NumComponents <= MaxSize);
for (vtkm::IdComponent index = 0; index < this->NumComponents; index++)
{
this->Data[index] = src[index];
@ -96,6 +99,7 @@ public:
VTKM_EXEC_CONT_EXPORT
void Append(ComponentType value)
{
VTKM_ASSERT(this->NumComponents < MaxSize);
this->Data[this->NumComponents] = value;
this->NumComponents++;
}