Debian Codegraph @

dune/localfunctions/common/localfiniteelement.hh

raw
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 #ifndef DUNE_LOCALFUNCTIONS_COMMON_TYPEERASEDLOCALFINITEELEMENT
6 #define DUNE_LOCALFUNCTIONS_COMMON_TYPEERASEDLOCALFINITEELEMENT
7
8 #include <memory>
9 #include <type_traits>
10 #include <utility>
11
12 #include <dune/common/typeutilities.hh>
13
14 #include <dune/geometry/type.hh>
15
16
17 #include <dune/localfunctions/common/virtualinterface.hh>
18 #include <dune/localfunctions/common/virtualwrappers.hh>
19
20 namespace Dune
21 {
22
23 /**
24 * \brief Type erasure class storing a local finite element
25 *
26 * \tparam LBT A LocalBasisTraits class encoding the local basis signature.
27 *
28 * In the flavor of `std::function` this class implements a type
29 * erased wrapper around local finite element implementations.
30 * One can either construct the wrapper directly with the
31 * local finite element that should be stored or construct
32 * and empty wrapper and assign it later.
33 * The type erasure is internally implemented using LocalFiniteElementVirtualInterface
34 * and LocalFiniteElementVirtualImp. In contrast to these,
35 * the LocalFiniteElement provides value-semantics and encapsulates
36 * dynamic memory management.
37 */
38 template<class LBT>
39 class LocalFiniteElement
40 {
41 using LocalBasisTraits = LBT;
42 using VirtualLFE = typename Dune::LocalFiniteElementVirtualInterface<LBT>;
43
44 public:
45
46 using Traits = typename VirtualLFE::Traits;
47
48 /**
49 * \brief Default constructor
50 *
51 * The constructed wrapper is empty and can later
52 * be filled with a local finite element using the
53 * assignment operators.
54 */
55 LocalFiniteElement() = default;
56
57 /**
58 * \brief Construct from implementation
59 *
60 * \tparam LFEImpl LocalFiniteElement implementation type
61 *
62 * \param lfe LocalFiniteElement implementation
63 *
64 * Construct a LocalFiniteElement wrapper storing a copy
65 * of the given local finite element object.
66 */
67 template<class LFEImpl,
68 Dune::disableCopyMove<LocalFiniteElement, LFEImpl> = 0 >
69 LocalFiniteElement(LFEImpl&& lfe)
70 : lfe_(new LocalFiniteElementVirtualImp<std::decay_t<LFEImpl>>(std::forward<LFEImpl>(lfe)))
71 , size_(lfe.size())
72 , type_(lfe.type())
73 {}
74
75 /**
76 * \brief Copy constructor
77 *
78 * \param other The LocalFiniteElement to copy from
79 *
80 * Depending of the state of the argument, the newly constructed
81 * LocalFiniteElement will either be empty or contain a copy of the
82 * local finite element stored in the LocalFiniteElement passed as
83 * constructor argument.
84 */
85 LocalFiniteElement(const LocalFiniteElement& other)
86 : lfe_(other.lfe_ ? other.lfe_->clone() : nullptr)
87 , size_(other.size())
88 , type_(other.type())
89 {}
90
91 /**
92 * \brief Move constructor
93 *
94 * \param other The LocalFiniteElement to move from
95 *
96 * Depending of the state of the argument, the newly constructed
97 * LocalFiniteElement will either be empty or contain the
98 * local finite element that was originally stored in the
99 * LocalFiniteElement passed as constructor argument.
100 */
101 LocalFiniteElement(LocalFiniteElement&& other) = default;
102
103 /**
104 * \brief Copy assignment
105 *
106 * \param rhs The LocalFiniteElement to assign from
107 *
108 * This will first remove the stored local finite element. Then,
109 * depending of the state of the argument, `*this` will either
110 * be set to empty state or store a copy of the local finite element
111 * stored in the LocalFiniteElement passed as right hand side.
112 */
113 LocalFiniteElement& operator=(const LocalFiniteElement& rhs)
114 {
115 if (&rhs!=this)
116 {
117 if (rhs.lfe_)
118 lfe_.reset(rhs.lfe_->clone());
119 else
120 lfe_.release();
121 size_ = rhs.size();
122 type_ = rhs.type();
123 }
124 return *this;
125 }
126
127 /**
128 * \brief Move assignment
129 *
130 * \param other The LocalFiniteElement to assign from
131 *
132 * This will first remove the stored local finite element. Then,
133 * depending of the state of the argument, `*this` will either
134 * be set to empty state or take over the local finite element
135 * stored in the LocalFiniteElement passed as right hand side.
136 * In any case the latter will be empty afterwards.
137 */
138 LocalFiniteElement& operator=(LocalFiniteElement&& other) = default;
139
140 /**
141 * \brief Check if the LocalFiniteElement is not empty
142 *
143 * This method returns `false` if `*this` is empty and `true` otherwise
144 */
145 operator bool () const
146 {
147 return lfe_;
148 }
149
150 /**
151 * \brief Access the LocalBasis of the stored local finite element
152 *
153 * Calling this method if the LocalFiniteElement is empty
154 * will invoke undefined behavior.
155 */
156 const typename Traits::LocalBasisType& localBasis () const
157 {
158 return lfe_->localBasis();
159 }
160
161 /**
162 * \brief Access the LocalCoefficients of the stored local finite element
163 *
164 * Calling this method if the LocalFiniteElement is empty
165 * will invoke undefined behavior.
166 */
167 const typename Traits::LocalCoefficientsType& localCoefficients () const
168 {
169 return lfe_->localCoefficients();
170 }
171
172 /**
173 * \brief Access the LocalInterpolation of the stored local finite element
174 *
175 * Calling this method if the LocalFiniteElement is empty
176 * will invoke undefined behavior.
177 */
178 const typename Traits::LocalInterpolationType& localInterpolation () const
179 {
180 return lfe_->localInterpolation();
181 }
182
183 /**
184 * \brief Get the number of basis functions of the stored local finite element
185 *
186 * The result of this method is unspecified if the LocalFiniteElement is empty.
187 */
188 unsigned int size () const
189 {
190 return size_;
191 }
192
193 /**
194 * \brief Get the GeometryType of the stored local finite element
195 *
196 * The result of this method is unspecified if the LocalFiniteElement is empty.
197 */
198 const GeometryType& type () const
199 {
200 return type_;
201 }
202
203 private:
204 std::unique_ptr<VirtualLFE> lfe_;
205 unsigned int size_ = 0;
206 Dune::GeometryType type_;
207 };
208
209 }
210 #endif