DUNE PDELab (git)

signature.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_SIGNATURE_HH
8#define DUNE_FUNCTIONS_COMMON_SIGNATURE_HH
9
10#include <type_traits>
11#include <tuple>
12
13#include <dune/functions/common/defaultderivativetraits.hh>
14
15namespace Dune {
16namespace Functions {
17
23template<typename F>
25
26#ifndef DOXYGEN
27template<typename F>
28struct IsCallable
29{
30 struct yes { std::size_t dummy[2]; };
31 struct no { std::size_t dummy[1]; };
32
33 template<typename C>
34 static yes test(const decltype(&C::operator()) *);
35 template<typename C>
36 static no test(...);
37
38 enum { value = (sizeof(test<F>(0)) == sizeof(yes)) };
39};
40
41template<typename R, typename D>
42struct IsCallable<R(D)>
43{
44 enum { value = true };
45};
46
47template<typename R, typename D>
48struct IsCallable<R(*)(D)>
49{
50 enum { value = true };
51};
52#endif
53
59template<class Signature, bool isCallable = IsCallable<Signature>::value >
61
62#ifndef DOXYGEN
64template<class T>
65struct SignatureTraits<T, true>
66 : public SignatureTraits<decltype(&T::operator()), true>
67{};
68
70template <typename C, typename R, typename D>
71struct SignatureTraits<R(C::*)(D) const, true>
72 : public SignatureTraits<R(D), true>
73{};
74
76template <typename C, typename R, typename D>
77struct SignatureTraits<R(C::*)(D), true>
78 : public SignatureTraits<R(D), true>
79{};
80
82template <typename R, typename D>
83struct SignatureTraits<R(*)(D), true>
84 : public SignatureTraits<R(D), true>
85{};
86
88template<class R, class D>
89struct SignatureTraits<R(D), true>
90{
91 using Range = R;
92 using Domain = D;
93
94 using RawRange = std::decay_t<Range>;
95 using RawDomain = std::decay_t<Domain>;
96
97 using RawSignature = RawRange(RawDomain);
98
99 template<template<class> class DerivativeTraits=DefaultDerivativeTraits>
100 using DerivativeSignature = typename DerivativeTraits<RawSignature>::Range(Domain);
101};
102#endif
103
104
105template<class Signature, template<class> class DerivativeTraits=DefaultDerivativeTraits>
106struct SignatureTag;
107
117template<class Range, class Domain, template<class> class DerivativeTraitsT>
118struct SignatureTag<Range(Domain), DerivativeTraitsT>
119{
120 using Signature = Range(Domain);
121
122 template<class T>
123 using DerivativeTraits = DerivativeTraitsT<T>;
124};
125
126
127
136template<class Range, class Domain, template<class> class DerivativeTraits>
137auto derivativeSignatureTag(SignatureTag<Range(Domain), DerivativeTraits> tag)
138{
139 using DerivativeRange = typename DerivativeTraits<Range(Domain)>::Range;
140 return SignatureTag<DerivativeRange(Domain), DerivativeTraits>();
141}
142
143
144
158template<std::size_t maxOrder, class Signature, template<class> class DerivativeTraits>
159auto derivativeSignatureTags(Dune::Functions::SignatureTag<Signature, DerivativeTraits> tag)
160{
161 if constexpr (maxOrder==0) {
162 // If maxOrder== 0 we just need the given SignatureTag
163 return std::make_tuple(tag);
164 } else {
165 // else we first construct the tail tuple with SignatureTags for derivatives
166 // of order 1 to maxOrder
167 auto tailTagsTuple = derivativeSignatureTags<std::size_t(maxOrder-1)>(derivativeSignatureTag(tag));
168 // and prepend this with the given SignatureTag.
169 // This is done by unpacking the tail tuple with apply().
170 return std::apply([&](auto&&... tailTags){
171 return std::make_tuple(tag, tailTags...);
172 }, tailTagsTuple);
173 }
174}
175
176
177
178} // namespace Functions
179} // namespace Dune
180
181#endif // DUNE_FUNCTIONS_COMMON_SIGNATURE_HH
auto derivativeSignatureTag(SignatureTag< Range(Domain), DerivativeTraits > tag)
Construct SignatureTag for derivative.
Definition: signature.hh:137
auto derivativeSignatureTags(Dune::Functions::SignatureTag< Signature, DerivativeTraits > tag)
Construct SignatureTags for derivatives.
Definition: signature.hh:159
Dune namespace.
Definition: alignedallocator.hh:13
Helper class to check that F is callable.
Definition: signature.hh:24
Helper class to deduce the signature of a callable.
Definition: signature.hh:60
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 13, 23:29, 2024)