Commit 87128181 authored by Dennis Gläser's avatar Dennis Gläser
Browse files

reintroduce Dumux optional

We still want to be compatible with Dune 2.5 where the optional
class is not yet available.
parent a44eae5b
......@@ -9,6 +9,7 @@ exceptions.hh
fixedlengthspline_.hh
intersectionmapper.hh
math.hh
optional.hh
parameters.hh
propertysystem.hh
quad.hh
......
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*****************************************************************************
* See the file COPYING for full copying permissions. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
/*!
* \file
* \brief A wrapper that can either contain an object of T or be empty.
* This might be used as a workaround for non-default constructible classes.
* \note This was adopted from dune-functions.
*/
#ifndef DUMUX_COMMON_OPTIONAL_HH
#define DUMUX_COMMON_OPTIONAL_HH
#include <utility>
#include <dune/common/typeutilities.hh>
namespace Dumux {
/**
* \brief A wrapper that can either contain an object of T or be empty
*
* \ingroup Utility
*
* \tparam T Type of wrapped objects
*/
template<class T>
class Optional
{
public:
Optional() :
p_(nullptr)
{}
template<class TT, Dune::disableCopyMove<Optional, TT> = 0>
Optional(TT&& t) :
p_(nullptr)
{
emplace(std::forward<TT>(t));
}
Optional(Optional&& other)
{
if (other)
p_ = new (buffer_) T(std::move(other.value()));
else
p_ = nullptr;
}
Optional(const Optional& other)
{
if (other)
p_ = new (buffer_) T(other.value());
else
p_ = nullptr;
}
~Optional()
{
if (operator bool())
p_->~T();
}
template<class TT, Dune::disableCopyMove<Optional, TT> = 0 >
Optional& operator=(TT&& t)
{
if (operator bool())
*p_ = std::forward<T>(t);
else
p_ = new (buffer_) T(std::forward<T>(t));
return *this;
}
Optional& operator=(const Optional& other)
{
if (other)
*this = other.value();
else if (operator bool())
{
p_->~T();
p_ = nullptr;
}
return *this;
}
Optional& operator=(Optional&& other)
{
if (other)
*this = std::move(other.value());
else if (operator bool())
{
p_->~T();
p_ = nullptr;
}
return *this;
}
explicit operator bool() const
{
return p_;
}
const T& value() const
{
return *p_;
}
T& value()
{
return *p_;
}
template< class... Args >
void emplace(Args&&... args)
{
if (operator bool())
p_->~T();
p_ = new (buffer_) T(std::forward<Args>(args)...);
}
void release()
{
if (operator bool())
{
p_->~T();
p_ = nullptr;
}
}
private:
alignas(T) char buffer_[sizeof(T)];
T* p_;
};
} // namespace Dumux
#endif // DUMUX_COMMON_OPTIONAL_HH
......@@ -24,7 +24,7 @@
#define DUMUX_DISCRETIZATION_CC_SUBCONTROLVOLUME_HH
#include <dumux/discretization/subcontrolvolumebase.hh>
#include <dune/common/std/optional.hh>
#include <dumux/common/optional.hh>
namespace Dumux
{
......@@ -66,7 +66,7 @@ public:
// We want to use the default copy/move assignment.
// But since geometry is not copy assignable :( we
// have to construct it again
geometry_.reset();
geometry_.release();
geometry_.emplace(other.geometry_.value());
center_ = other.center_;
elementIndex_ = other.elementIndex_;
......@@ -79,7 +79,7 @@ public:
// We want to use the default copy/move assignment.
// But since geometry is not copy assignable :( we
// have to construct it again
geometry_.reset();
geometry_.release();
geometry_.emplace(std::move(other.geometry_.value()));
center_ = std::move(other.center_);
elementIndex_ = std::move(other.elementIndex_);
......@@ -139,7 +139,7 @@ public:
private:
// Work around the fact that geometry is not default constructible
Dune::Std::optional<Geometry> geometry_;
Optional<Geometry> geometry_;
GlobalPosition center_;
GridIndexType elementIndex_;
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment