Loading...
Searching...
No Matches
typeerasure.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
#ifndef DUNE_FUNCTIONS_COMMON_TYPEERASURE_HH
#define DUNE_FUNCTIONS_COMMON_TYPEERASURE_HH
#include <typeinfo>
#include <dune/common/typeutilities.hh>
#include <dune/functions/common/type_traits.hh>
#include <dune/functions/common/interfaces.hh>
#include <dune/functions/common/polymorphicsmallobject.hh>
namespace Dune {
namespace Functions {
namespace Imp {
template<class Interface>
class TypeErasureWrapperInterface :
public Interface,
public PolymorphicType<TypeErasureWrapperInterface<Interface>>
{
public:
virtual const std::type_info& target_type() const = 0;
};
template<class Interface, class T>
class TypeErasureWrapperBase :
public TypeErasureWrapperInterface<Interface>
{
public:
template<class TT, disableCopyMove<TypeErasureWrapperBase, TT> = 0>
TypeErasureWrapperBase(TT&& t) :
wrapped_(std::forward<TT>(t))
{}
T& get()
{
return wrapped_;
}
const T& get() const
{
return wrapped_;
}
protected:
using Wrapped = T;
Wrapped wrapped_;
};
template<class Interface, template<class> class Implementation, class T>
class TypeErasureWrapperImplementation :
public Implementation<TypeErasureWrapperBase<Interface, T> >
{
public:
template<class TT, disableCopyMove<TypeErasureWrapperImplementation, T> = 0>
TypeErasureWrapperImplementation(TT&& t) :
Implementation<TypeErasureWrapperBase<Interface, T> >(std::forward<TT>(t))
{}
virtual TypeErasureWrapperImplementation* clone() const
{
return new TypeErasureWrapperImplementation(*this);
}
virtual TypeErasureWrapperImplementation* clone(void* buffer) const
{
return new (buffer) TypeErasureWrapperImplementation(*this);
}
virtual TypeErasureWrapperImplementation* move(void* buffer)
{
return new (buffer) TypeErasureWrapperImplementation(std::move(*this));
}
virtual const std::type_info& target_type() const
{
return typeid(T);
}
};
} // namespace Dune::Functions::Imp
template<class Interface, template<class> class Implementation, size_t bufferSize = 56>
class TypeErasureBase
{
public:
template<class T, disableCopyMove<TypeErasureBase, T> = 0 >
TypeErasureBase(T&& t) :
wrapped_(Imp::TypeErasureWrapperImplementation<Interface, Implementation, std::decay_t<T>>(std::forward<T>(t)))
{}
TypeErasureBase() = default;
Interface& asInterface()
{
return wrapped_.get();
}
const Interface& asInterface() const
{
return wrapped_.get();
}
explicit operator bool() const noexcept
{
// Forward the query to the wrapped object
return static_cast<bool>(wrapped_);
}
const std::type_info& target_type() const
{
return wrapped_.get().target_type();
}
protected:
PolymorphicSmallObject<Imp::TypeErasureWrapperInterface<Interface>, bufferSize > wrapped_;
};
}} // namespace Dune::Functions
#endif // DUNE_FUNCTIONS_COMMON_TYPEERASURE_HH
interfaces.hh
type_traits.hh
polymorphicsmallobject.hh
Dune
Definition monomialset.hh:19
Dune::Functions
Definition monomialset.hh:19
Dune::Functions::PolymorphicSmallObject
A wrapper providing small object optimization with polymorphic types.
Definition polymorphicsmallobject.hh:51
Dune::Functions::TypeErasureBase::target_type
const std::type_info & target_type() const
Get type of stored object.
Definition typeerasure.hh:201
Dune::Functions::TypeErasureBase::TypeErasureBase
TypeErasureBase(T &&t)
Construct wrapper from object.
Definition typeerasure.hh:174
Dune::Functions::TypeErasureBase< typename Traits::Concept, Traits::template Model >::wrapped_
PolymorphicSmallObject< Imp::TypeErasureWrapperInterface< typename Traits::Concept >, 56 > wrapped_
Definition typeerasure.hh:207
Dune::Functions::TypeErasureBase::TypeErasureBase
TypeErasureBase()=default
Default constructor.
Dune::Functions::TypeErasureBase::asInterface
Interface & asInterface()
Get mutable reference to wrapped object.
Definition typeerasure.hh:182
Dune::Functions::TypeErasureBase::asInterface
const Interface & asInterface() const
Get reference to wrapped object.
Definition typeerasure.hh:188