Skip to content
Snippets Groups Projects
Commit 4546b4bd authored by Oliver Sander's avatar Oliver Sander
Browse files

Guard against misuse of AxisAlignedCubeGeometry constructor

The AxisAlignedCubeGeometry has always had a constructor that was always
intended to be used with dim==coorddim only.  However, this precondition
was never checked anywhere, which could lead to strange bugs.
This patch introduces a static_assert that fails at compile time
if the dim==coorddim precondition is not met.

BUG: core/dune-geometry#25
parent d1954974
No related branches found
No related tags found
1 merge request!150Guard against misuse of AxisAlignedCubeGeometry constructor
Pipeline #30040 passed
......@@ -6,6 +6,12 @@
Furthermore, flags for either shared library or position independent code
needs to be used.
- The class `AxisAlignedCubeGeometry` has always had a constructor taking
two arguments `FieldVector<ctype,coorddim> lower` and `FieldVector<ctype,coorddim> upper`.
This constructor was always to be used in the case `dim==coorddim` only,
but this was never enforced. Starting with version 2.8, compilation
fails with an error message if this constructor is used with `dim!=coorddim`.
## Deprecations and removals
- Remove code needed to use reference elements by reference.
......
......@@ -99,6 +99,7 @@ namespace Dune {
upper_(upper),
axes_()
{
static_assert(dim==coorddim, "Use this constructor only if dim==coorddim!");
// all 'true', but is never actually used
axes_ = (1<<coorddim)-1;
}
......
......@@ -397,19 +397,22 @@ namespace Dune
assert(codimension == 0 or codimension == dimension);
if (codimension == 0) {
if constexpr (codimension == 0) {
for (size_t j = 0; j < dimension; j++)
{
lower[j] = double(intCoords[j]) / double(_nIntervals);
upper[j] = double(intCoords[j] + 1) / double(_nIntervals);
}
return typename RefinementImp<dimension,
CoordType>::template Codim<codimension>::Geometry(lower,upper);
} else {
for (size_t j = 0; j < dimension; j++)
lower[j] = upper[j] = double(intCoords[j]) / double(_nIntervals);
}
return typename RefinementImp<dimension,
CoordType>::template Codim<codimension>::Geometry(lower,upper);
return typename RefinementImp<dimension,
CoordType>::template Codim<codimension>::Geometry(lower,upper,std::bitset<dimension>(0));
}
}
#endif // DOXYGEN
......
......@@ -58,8 +58,11 @@ void testCodimZero(int& result)
FieldVector<double,coorddim> lower(0);
FieldVector<double,coorddim> upper(1);
std::bitset<coorddim> axes(0);
for (int i=0; i<dim; i++)
axes[i] = true;
ElementGeometry geometry( lower, upper );
ElementGeometry geometry( lower, upper, axes );
if (checkGeometry(geometry))
pass(result);
......@@ -69,7 +72,7 @@ void testCodimZero(int& result)
testBasicGeometryAffine(geometry, result);
// test assignability
ElementGeometry geometry2( lower, upper );
ElementGeometry geometry2( lower, upper, axes );
geometry2 = geometry;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment