From 1ef4d5cfdd3e3e53d44b9c69b7832d726b13db03 Mon Sep 17 00:00:00 2001 From: Timo Koch Date: Tue, 27 Oct 2020 02:01:51 +0100 Subject: [PATCH] [bugfix][cc][scv] Replace optional by unique_ptr Using optional lead to issues probably because the assignment operator were not correcty implemented. However there is not need for optional here since the member is in fact not optional but always there when the object is validly constructed. We can simply use a unique_ptr to work around the fact that not all geometry imeplementation are default-constructible and/or copy-assignable. --- .../cellcentered/subcontrolvolume.hh | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/dumux/discretization/cellcentered/subcontrolvolume.hh b/dumux/discretization/cellcentered/subcontrolvolume.hh index 814985cc89..5f93087ba4 100644 --- a/dumux/discretization/cellcentered/subcontrolvolume.hh +++ b/dumux/discretization/cellcentered/subcontrolvolume.hh @@ -24,7 +24,7 @@ #ifndef DUMUX_DISCRETIZATION_CC_SUBCONTROLVOLUME_HH #define DUMUX_DISCRETIZATION_CC_SUBCONTROLVOLUME_HH -#include +#include #include @@ -92,13 +92,14 @@ public: CCSubControlVolume(GeometryParamType geometry, GridIndexType elementIndex) : ParentType() - , geometry_(std::move(geometry)) - , center_(geometry_.value().center()) + , geometry_(std::make_unique(std::move(geometry))) + , center_(geometry_->center()) , elementIndex_(elementIndex) {} //! The copy constrcutor - CCSubControlVolume(const CCSubControlVolume& other) = default; + CCSubControlVolume(const CCSubControlVolume& other) + { deepCopy_(other); } //! The move constrcutor CCSubControlVolume(CCSubControlVolume&& other) = default; @@ -106,26 +107,12 @@ public: //! The copy assignment operator CCSubControlVolume& operator=(const CCSubControlVolume& other) { - // We want to use the default copy/move assignment. - // But since geometry is not copy assignable :( we - // have to construct it again - geometry_.emplace(other.geometry_.value()); - center_ = other.center_; - elementIndex_ = other.elementIndex_; + deepCopy_(other); return *this; } //! The move assignment operator - CCSubControlVolume& operator=(CCSubControlVolume&& other) noexcept - { - // We want to use the default copy/move assignment. - // But since geometry is not copy assignable :( we - // have to construct it again - geometry_.emplace(std::move(other.geometry_.value())); - center_ = std::move(other.center_); - elementIndex_ = std::move(other.elementIndex_); - return *this; - } + CCSubControlVolume& operator=(CCSubControlVolume&& other) = default; //! The center of the sub control volume const GlobalPosition& center() const @@ -143,8 +130,7 @@ public: // e.g. for integration const Geometry& geometry() const { - assert((geometry_)); - return geometry_.value(); + return *geometry_; } //! The index of the dof this scv is embedded in (the global index of this scv) @@ -186,8 +172,18 @@ public: } private: - // Work around the fact that geometry is not default constructible - std::optional geometry_; + void deepCopy_(const CCSubControlVolume& other) + { + if (other.geometry_) + geometry_ = std::make_unique(other.geometry()); + else + geometry_.reset(); + center_ = other.center_; + elementIndex_ = other.elementIndex_; + } + + // Work around the fact that geometry is not default-constructible and not copy-assignable + std::unique_ptr geometry_; GlobalPosition center_; GridIndexType elementIndex_; }; -- GitLab