Skip to content
Snippets Groups Projects
Commit d487fef6 authored by Markus Blatt's avatar Markus Blatt
Browse files

Merge branch 'feature/remove-forbidden-downcast' into 'master'

Remove forbidden downcast

This removes several constructors and assignments from base class in the class hierarchy around `base_array`. These removed methods are all implemented using a downcast which may result in undefined behaviour.
Since they are not used anywhere in the core modules we can maybe remove them. According to @markus.blatt  these base classes are all meant to be internal.

This may cause problems if user code explicitly uses the raw base classes and these methods. However, such user code would only be valid if the passed base class reference is actually an upcasted derived class which is very unlikely.

See merge request !66
parents 3119e52b b797918e
No related branches found
No related tags found
1 merge request!66Remove forbidden downcast
Pipeline #
......@@ -426,27 +426,6 @@ namespace Dune {
for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
}
//! construct from base class object
base_array (const base_array_unmanaged<B,A>& _a)
{
const base_array& a = static_cast<const base_array&>(_a);
// allocate memory with same size as a
this->n = a.n;
if (this->n>0) {
this->p = allocator_.allocate(this->n);
new (this->p)B[this->n];
} else
{
this->n = 0;
this->p = 0;
}
// and copy elements
for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
}
//! free dynamic memory
~base_array ()
{
......@@ -510,12 +489,6 @@ namespace Dune {
return *this;
}
//! assign from base class object
base_array& operator= (const base_array_unmanaged<B,A>& a)
{
return this->operator=(static_cast<const base_array&>(a));
}
protected:
A allocator_;
......
......@@ -25,6 +25,8 @@
namespace Dune {
template<class B, class A=std::allocator<B> >
class BlockVectorWindow;
/**
\brief An unmanaged vector of blocks.
......@@ -524,30 +526,6 @@ namespace Dune {
for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
}
//! construct from base class object
BlockVector (const block_vector_unmanaged<B,A>& _a)
{
// upcast, because protected data inaccessible
const BlockVector& a = static_cast<const BlockVector&>(_a);
// allocate memory with same size as a
this->n = a.n;
capacity_ = a.capacity_;
if (capacity_>0) {
this->p = this->allocator_.allocate(capacity_);
new (this->p)B[capacity_];
} else
{
this->n = 0;
this->p = 0;
capacity_ = 0;
}
// and copy elements
for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
}
//! free dynamic memory
~BlockVector ()
{
......@@ -591,13 +569,6 @@ namespace Dune {
return *this;
}
//! assign from base class object
BlockVector& operator= (const block_vector_unmanaged<B,A>& a)
{
// forward to regular assignement operator
return this->operator=(static_cast<const BlockVector&>(a));
}
//! assign from scalar
BlockVector& operator= (const field_type& k)
{
......@@ -605,6 +576,17 @@ namespace Dune {
(static_cast<block_vector_unmanaged<B,A>&>(*this)) = k;
return *this;
}
//! Assignment from BlockVectorWindow
template<class OtherAlloc>
BlockVector& operator= (const BlockVectorWindow<B,OtherAlloc>& other)
{
resize(other.size());
for(std::size_t i=0; i<other.size(); ++i)
(*this)[i] = other[i];
return *this;
}
protected:
size_type capacity_;
......@@ -655,7 +637,11 @@ namespace Dune {
Setting the compile time switch DUNE_ISTL_WITH_CHECKING
enables error checking.
*/
#ifndef DOXYGEN
template<class B, class A>
#else
template<class B, class A=std::allocator<B> >
#endif
class BlockVectorWindow : public block_vector_unmanaged<B,A>
{
public:
......@@ -706,18 +692,6 @@ namespace Dune {
this->p = a.p;
}
//! construct from base class object with reference semantics!
BlockVectorWindow (const block_vector_unmanaged<B,A>& _a)
{
// cast needed to access protected data
const BlockVectorWindow& a = static_cast<const BlockVectorWindow&>(_a);
// make me point to the other's data
this->n = a.n;
this->p = a.p;
}
//! assignment
BlockVectorWindow& operator= (const BlockVectorWindow& a)
{
......@@ -734,13 +708,6 @@ namespace Dune {
return *this;
}
//! assign from base class object
BlockVectorWindow& operator= (const block_vector_unmanaged<B,A>& a)
{
// forward to regular assignment operator
return this->operator=(static_cast<const BlockVectorWindow&>(a));
}
//! assign from scalar
BlockVectorWindow& operator= (const field_type& k)
{
......@@ -1098,19 +1065,6 @@ namespace Dune {
this->j = a.j;
}
//! construct from base class object with reference semantics!
CompressedBlockVectorWindow (const compressed_block_vector_unmanaged<B,A>& _a)
{
// cast needed to access protected data (downcast)
const CompressedBlockVectorWindow& a = static_cast<const CompressedBlockVectorWindow&>(_a);
// make me point to the other's data
this->n = a.n;
this->p = a.p;
this->j = a.j;
}
//! assignment
CompressedBlockVectorWindow& operator= (const CompressedBlockVectorWindow& a)
{
......@@ -1128,13 +1082,6 @@ namespace Dune {
return *this;
}
//! assign from base class object
CompressedBlockVectorWindow& operator= (const compressed_block_vector_unmanaged<B,A>& a)
{
// forward to regular assignment operator
return this->operator=(static_cast<const CompressedBlockVectorWindow&>(a));
}
//! assign from scalar
CompressedBlockVectorWindow& operator= (const field_type& k)
{
......
......@@ -11,9 +11,6 @@ int main()
base_array<double> v1(10);
base_array<double> v2 = v1;
// Test constructor from base_array_unmanaged
base_array<double> v3 = *(static_cast<base_array_unmanaged<double>*>(&v1));
v1.resize(20);
v1 = v2;
......
......@@ -61,11 +61,6 @@ int testVector()
for(typename Vector::size_type i=0; i < v.N(); ++i)
assert(v[i] == w[i]);
w = static_cast<const Dune::block_vector_unmanaged<VectorBlock,Alloc>&>(v);
for(typename Vector::size_type i=0; i < w.N(); ++i)
assert(v[i] == w[i]);
Vector z(w);
assert(w.N()==z.N());
......@@ -74,15 +69,6 @@ int testVector()
for(typename Vector::size_type i=0; i < w.N(); ++i)
assert(z[i] == w[i]);
Vector z1(static_cast<const Dune::block_vector_unmanaged<VectorBlock,Alloc>&>(v2));
assert(v2.N()==z1.N());
assert(v2.capacity()==z1.capacity());
for(typename Vector::size_type i=1; i < v2.N(); ++i) {
assert(z1[i] == v2[i]);
}
v.reserve(150);
assert(150==v.capacity());
assert(25==v.N());
......
......@@ -96,10 +96,6 @@ void test_basearray ()
Type p[13];
Dune::base_array_window<Type> c(p+4,3); // c contains p[4]...p[6]
// copy window
b = c;
Dune::base_array<Type> d(c);
// move window to p[6]...p[10]
c.move(2,5);
}
......
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