Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
PolymorphicGeometry.hpp
Go to the documentation of this file.
1 // Copyright (c) 2018 Maikel Nadolski
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #ifndef FUB_GEOMETRY_POLYMORPHIC_GEOMETRY_HPP
22 #define FUB_GEOMETRY_POLYMORPHIC_GEOMETRY_HPP
23 
24 #include "fub/core/type_traits.hpp"
26 
27 #include <memory>
28 
29 namespace fub {
30 
31 template <std::size_t Rank, typename G>
32 struct GeometryWrapper : Geometry<Rank> {
33  GeometryWrapper(const G& base) : base_{base} {} // NOLINT
34  GeometryWrapper(G&& base) : base_{std::move(base)} {} // NOLINT
35 
36  std::unique_ptr<Geometry<Rank>> Clone() const override {
37  return std::make_unique<GeometryWrapper>(base_);
38  }
39 
40  double ComputeDistanceTo(const std::array<double, Rank>& x) const override {
41  return base_.ComputeDistanceTo(x);
42  }
43 
44  G base_;
45 };
46 
47 template <std::size_t Rank> class PolymorphicGeometry : public Geometry<Rank> {
48 public:
50  : base_{other.Clone()} {}
51 
52  template <
53  typename G,
54  std::enable_if_t<!std::is_base_of<Geometry<Rank>, G>::value>* = nullptr,
55  std::enable_if_t<!std::is_same<G, PolymorphicGeometry>::value>* = nullptr>
56  PolymorphicGeometry(const G& geometry)
57  : base_{std::make_unique<GeometryWrapper<Rank, G>>(geometry)} {}
58 
59  template <
60  typename G,
61  std::enable_if_t<std::is_base_of<Geometry<Rank>, G>::value>* = nullptr,
62  std::enable_if_t<!std::is_same<G, PolymorphicGeometry>::value>* = nullptr>
63  PolymorphicGeometry(const G& geometry)
64  : base_{std::make_unique<G>(geometry)} {}
65 
66  double ComputeDistanceTo(const std::array<double, Rank>& x) const override {
67  return base_->ComputeDistanceTo(x);
68  }
69 
70  std::unique_ptr<Geometry<Rank>> Clone() const override {
71  return base_->Clone();
72  }
73 
74  Geometry<Rank>& Base() noexcept { return *base_; }
75  const Geometry<Rank>& Base() const noexcept { return *base_; }
76 
77 private:
78  std::unique_ptr<Geometry<Rank>> base_;
79 };
80 
81 } // namespace fub
82 
83 #endif
Definition: PolymorphicGeometry.hpp:47
PolymorphicGeometry(const PolymorphicGeometry &other)
Definition: PolymorphicGeometry.hpp:49
Geometry< Rank > & Base() noexcept
Definition: PolymorphicGeometry.hpp:74
PolymorphicGeometry(const G &geometry)
Definition: PolymorphicGeometry.hpp:56
std::unique_ptr< Geometry< Rank > > base_
Definition: PolymorphicGeometry.hpp:78
double ComputeDistanceTo(const std::array< double, Rank > &x) const override
Computes the minimum distance between geometry and point x.
Definition: PolymorphicGeometry.hpp:66
const Geometry< Rank > & Base() const noexcept
Definition: PolymorphicGeometry.hpp:75
std::unique_ptr< Geometry< Rank > > Clone() const override
Returns a copy of the concrete geometry as a pointer to the base class.
Definition: PolymorphicGeometry.hpp:70
The fub namespace.
Definition: AnyBoundaryCondition.hpp:31
Definition: PolymorphicGeometry.hpp:32
GeometryWrapper(const G &base)
Definition: PolymorphicGeometry.hpp:33
G base_
Definition: PolymorphicGeometry.hpp:44
double ComputeDistanceTo(const std::array< double, Rank > &x) const override
Computes the minimum distance between geometry and point x.
Definition: PolymorphicGeometry.hpp:40
std::unique_ptr< Geometry< Rank > > Clone() const override
Returns a copy of the concrete geometry as a pointer to the base class.
Definition: PolymorphicGeometry.hpp:36
GeometryWrapper(G &&base)
Definition: PolymorphicGeometry.hpp:34
Definition: geometry/Geometry.hpp:29
This file adds basic type traits utilities which are not yet implemented in all standard libraries.