Dune Core Modules (2.8.0)

typeerasure.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_FUNCTIONS_COMMON_TYPEERASURE_HH
4#define DUNE_FUNCTIONS_COMMON_TYPEERASURE_HH
5
6#include <typeinfo>
7
9
10#include <dune/functions/common/type_traits.hh>
11#include <dune/functions/common/interfaces.hh>
12#include <dune/functions/common/polymorphicsmallobject.hh>
13
14namespace Dune {
15namespace Functions {
16namespace Imp {
17
18
19
33template<class Interface>
34class TypeErasureWrapperInterface :
35 public Interface,
36 public PolymorphicType<TypeErasureWrapperInterface<Interface>>
37{
38public:
39 virtual const std::type_info& target_type() const = 0;
40};
41
42
43
60template<class Interface, class T>
61class TypeErasureWrapperBase :
62 public TypeErasureWrapperInterface<Interface>
63{
64public:
65 template<class TT, disableCopyMove<TypeErasureWrapperBase, TT> = 0>
66 TypeErasureWrapperBase(TT&& t) :
67 wrapped_(std::forward<TT>(t))
68 {}
69
71 T& get()
72 {
73 return wrapped_;
74 }
75
77 const T& get() const
78 {
79 return wrapped_;
80 }
81
82protected:
83 using Wrapped = T;
84 Wrapped wrapped_;
85};
86
87
88
112template<class Interface, template<class> class Implementation, class T>
113class TypeErasureWrapperImplementation :
114 public Implementation<TypeErasureWrapperBase<Interface, T> >
115{
116public:
117
119 template<class TT, disableCopyMove<TypeErasureWrapperImplementation, T> = 0>
120 TypeErasureWrapperImplementation(TT&& t) :
121 Implementation<TypeErasureWrapperBase<Interface, T> >(std::forward<TT>(t))
122 {}
123
125 virtual TypeErasureWrapperImplementation* clone() const
126 {
127 return new TypeErasureWrapperImplementation(*this);
128 }
129
131 virtual TypeErasureWrapperImplementation* clone(void* buffer) const
132 {
133 return new (buffer) TypeErasureWrapperImplementation(*this);
134 }
135
137 virtual TypeErasureWrapperImplementation* move(void* buffer)
138 {
139 return new (buffer) TypeErasureWrapperImplementation(std::move(*this));
140 }
141
143 virtual const std::type_info& target_type() const
144 {
145 return typeid(T);
146 }
147};
148
149} // namespace Dune::Functions::Imp
150
151
152
163template<class Interface, template<class> class Implementation, size_t bufferSize = 56>
165{
166public:
167
169 template<class T, disableCopyMove<TypeErasureBase, T> = 0 >
171 wrapped_(Imp::TypeErasureWrapperImplementation<Interface, Implementation, typename std::decay<T>::type>(std::forward<T>(t)))
172 {}
173
175 TypeErasureBase() = default;
176
179 {
180 return wrapped_.get();
181 }
182
184 const Interface& asInterface() const
185 {
186 return wrapped_.get();
187 }
188
190 const std::type_info& target_type() const
191 {
192 return wrapped_.get().target_type();
193 }
194
195protected:
197};
198
199
200}} // namespace Dune::Functions
201
202
203
204#endif // DUNE_FUNCTIONS_COMMON_TYPEERASURE_HH
A wrapper providing small object optimization with polymorphic types.
Definition: polymorphicsmallobject.hh:45
Base class for type-erased interface wrapper.
Definition: typeerasure.hh:165
const std::type_info & target_type() const
Get type of stored object.
Definition: typeerasure.hh:190
TypeErasureBase(T &&t)
Construct wrapper from object.
Definition: typeerasure.hh:170
TypeErasureBase()=default
Default constructor.
Interface & asInterface()
Get mutable reference to wrapped object.
Definition: typeerasure.hh:178
const Interface & asInterface() const
Get reference to wrapped object.
Definition: typeerasure.hh:184
Communication interface between remote and local indices.
Definition: interface.hh:207
Dune namespace.
Definition: alignedallocator.hh:11
STL namespace.
Utilities for type computations, constraining overloads, ...
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)