DUNE PDELab (git)

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
4// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUNCTIONS_COMMON_TYPEERASURE_HH
8#define DUNE_FUNCTIONS_COMMON_TYPEERASURE_HH
9
10#include <typeinfo>
11
13
14#include <dune/functions/common/type_traits.hh>
15#include <dune/functions/common/interfaces.hh>
16#include <dune/functions/common/polymorphicsmallobject.hh>
17
18namespace Dune {
19namespace Functions {
20namespace Imp {
21
22
23
37template<class Interface>
38class TypeErasureWrapperInterface :
39 public Interface,
40 public PolymorphicType<TypeErasureWrapperInterface<Interface>>
41{
42public:
43 virtual const std::type_info& target_type() const = 0;
44};
45
46
47
64template<class Interface, class T>
65class TypeErasureWrapperBase :
66 public TypeErasureWrapperInterface<Interface>
67{
68public:
69 template<class TT, disableCopyMove<TypeErasureWrapperBase, TT> = 0>
70 TypeErasureWrapperBase(TT&& t) :
71 wrapped_(std::forward<TT>(t))
72 {}
73
75 T& get()
76 {
77 return wrapped_;
78 }
79
81 const T& get() const
82 {
83 return wrapped_;
84 }
85
86protected:
87 using Wrapped = T;
88 Wrapped wrapped_;
89};
90
91
92
116template<class Interface, template<class> class Implementation, class T>
117class TypeErasureWrapperImplementation :
118 public Implementation<TypeErasureWrapperBase<Interface, T> >
119{
120public:
121
123 template<class TT, disableCopyMove<TypeErasureWrapperImplementation, T> = 0>
124 TypeErasureWrapperImplementation(TT&& t) :
125 Implementation<TypeErasureWrapperBase<Interface, T> >(std::forward<TT>(t))
126 {}
127
129 virtual TypeErasureWrapperImplementation* clone() const
130 {
131 return new TypeErasureWrapperImplementation(*this);
132 }
133
135 virtual TypeErasureWrapperImplementation* clone(void* buffer) const
136 {
137 return new (buffer) TypeErasureWrapperImplementation(*this);
138 }
139
141 virtual TypeErasureWrapperImplementation* move(void* buffer)
142 {
143 return new (buffer) TypeErasureWrapperImplementation(std::move(*this));
144 }
145
147 virtual const std::type_info& target_type() const
148 {
149 return typeid(T);
150 }
151};
152
153} // namespace Dune::Functions::Imp
154
155
156
167template<class Interface, template<class> class Implementation, size_t bufferSize = 56>
169{
170public:
171
173 template<class T, disableCopyMove<TypeErasureBase, T> = 0 >
175 wrapped_(Imp::TypeErasureWrapperImplementation<Interface, Implementation, std::decay_t<T>>(std::forward<T>(t)))
176 {}
177
179 TypeErasureBase() = default;
180
183 {
184 return wrapped_.get();
185 }
186
188 const Interface& asInterface() const
189 {
190 return wrapped_.get();
191 }
192
194 const std::type_info& target_type() const
195 {
196 return wrapped_.get().target_type();
197 }
198
199protected:
201};
202
203
204}} // namespace Dune::Functions
205
206
207
208#endif // DUNE_FUNCTIONS_COMMON_TYPEERASURE_HH
A wrapper providing small object optimization with polymorphic types.
Definition: polymorphicsmallobject.hh:51
Base class for type-erased interface wrapper.
Definition: typeerasure.hh:169
const std::type_info & target_type() const
Get type of stored object.
Definition: typeerasure.hh:194
TypeErasureBase(T &&t)
Construct wrapper from object.
Definition: typeerasure.hh:174
TypeErasureBase()=default
Default constructor.
Interface & asInterface()
Get mutable reference to wrapped object.
Definition: typeerasure.hh:182
const Interface & asInterface() const
Get reference to wrapped object.
Definition: typeerasure.hh:188
Communication interface between remote and local indices.
Definition: interface.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
constexpr auto get(std::integer_sequence< T, II... >, std::integral_constant< std::size_t, pos >={})
Return the entry at position pos of the given sequence.
Definition: integersequence.hh:22
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 13, 23:29, 2024)