Loading [MathJax]/extensions/tex2jax.js

DUNE PDELab (unstable)

containerfactory.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
5#ifndef DUNE_FUNCTIONS_BACKENDS_CONTAINERFACTORY_HH
6#define DUNE_FUNCTIONS_BACKENDS_CONTAINERFACTORY_HH
7
8#include <type_traits>
9#include <array>
10#include <vector>
11
14#include <dune/common/indices.hh>
16
17#include <dune/functions/functionspacebases/containerdescriptors.hh>
18
19namespace Dune::Functions {
20namespace ContainerDescriptors {
21namespace Impl {
22
23template<class T>
24struct ContainerFactory
25{
26 void operator() (const Unknown& descriptor, const T& defaultValue) const
27 {
28 DUNE_THROW(Dune::NotImplemented, "Cannot create a vector. The container descriptor is unknown.");
29 }
30
31 template<class... ChildDescriptor>
32 auto operator() (const Tuple<ChildDescriptor...>& descriptor, const T& defaultValue) const
33 {
34 return unpackIntegerSequence([&](auto... ii) {
35 return Dune::TupleVector<decltype((*this)(descriptor[ii], defaultValue))...>{(*this)(descriptor[ii], defaultValue)...};
36 }, std::make_index_sequence<sizeof...(ChildDescriptor)>());
37 }
38
39 template<class ChildDescriptor, std::size_t n>
40 auto operator() (const Array<ChildDescriptor,n>& descriptor, const T& defaultValue) const
41 {
42 using ChildContainer = decltype((*this)(descriptor[0], defaultValue));
43 return unpackIntegerSequence([&](auto... ii) {
44 return std::array<ChildContainer, n>{(*this)(descriptor[ii], defaultValue)...};
45 }, std::make_index_sequence<n>());
46 }
47
48 template<class ChildDescriptor>
49 auto operator() (const Vector<ChildDescriptor>& descriptor, const T& defaultValue) const
50 {
51 using ChildContainer = decltype((*this)(descriptor[0], defaultValue));
52 auto result = std::vector<ChildContainer>();
53 result.reserve(descriptor.size());
54 for (std::size_t i = 0; i < descriptor.size(); ++i)
55 result.emplace_back((*this)(descriptor[i], defaultValue));
56 return result;
57 }
58
59 template<class ChildDescriptor, std::size_t n>
60 auto operator() (const UniformArray<ChildDescriptor,n>& descriptor, const T& defaultValue) const
61 {
62 using ChildContainer = decltype((*this)(descriptor[0], defaultValue));
63 auto childContainer = (*this)(descriptor[0], defaultValue);
64 return unpackIntegerSequence([&](auto... ii) {
65 return std::array<ChildContainer, n>{((void)(ii),childContainer)...};
66 }, std::make_index_sequence<n>());
67 }
68
69 template<class ChildDescriptor>
70 auto operator() (const UniformVector<ChildDescriptor>& descriptor, const T& defaultValue) const
71 {
72 using ChildContainer = decltype((*this)(descriptor[0], defaultValue));
73 auto childContainer = (*this)(descriptor[0], defaultValue);
74 return std::vector<ChildContainer>(descriptor.size(), childContainer);
75 }
76
77 // scalar types
78
79 auto operator() (const Value& descriptor, const T& defaultValue) const
80 {
81 return T(defaultValue);
82 }
83
84};
85
86} // end namespace Impl
87} // end namespace ContainerDescriptors
88
89
101template<class T, class ContainerDescriptor>
102auto makeContainer (const ContainerDescriptor& descriptor, const T& defaultValue)
103{
104 auto factory = ContainerDescriptors::Impl::ContainerFactory<T>{};
105 return factory(descriptor, defaultValue);
106}
107
118template<class T, class ContainerDescriptor>
119auto makeContainer (const ContainerDescriptor& descriptor)
120{
121 return makeContainer<T>(descriptor, T());
122}
123
124} // end namespace Dune::Functions
125
126#endif // DUNE_FUNCTIONS_BACKENDS_CONTAINERFACTORY_HH
Default exception for dummy implementations.
Definition: exceptions.hh:357
A class augmenting std::tuple by element access via operator[].
Definition: tuplevector.hh:35
A few common exception classes.
Implements a vector constructed from a given type representing a field and a compile-time given size.
decltype(auto) constexpr unpackIntegerSequence(F &&f, std::integer_sequence< I, i... > sequence)
Unpack an std::integer_sequence<I,i...> to std::integral_constant<I,i>...
Definition: indices.hh:124
#define DUNE_THROW(E,...)
Definition: exceptions.hh:314
Provides the TupleVector class that augments std::tuple by operator[].
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Apr 3, 22:46, 2025)