CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
traversalLogic.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2024 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://www.scicomp.uni-kl.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://www.scicomp.uni-kl.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <utility>
38
39#include "../../config.h"
40#include "../../misc/macros.hpp"
41#include "nodeInterface.hpp"
42
44namespace codi {
45
55 template<typename T_Impl>
57 public:
58
59 using Impl = CODI_DD(T_Impl, TraversalLogic);
60
63 return static_cast<Impl&>(*this);
64 }
65
69 template<typename Node, typename... Args>
70 CODI_INLINE void eval(NodeInterface<Node> const& node, Args&&... args) {
71 toNode(node.cast(), std::forward<Args>(args)...);
72 }
73
74 /*******************************************************************************/
77
85 template<typename Node, typename... Args>
86 CODI_INLINE void node(Node const& node, Args&&... args) {
87 // Default logic forwards to all links.
88 toLinks(node, std::forward<Args>(args)...);
89 }
90
96 template<typename Node, typename... Args>
97 CODI_INLINE void leaf(Node const& node, Args&&... args) {
98 CODI_UNUSED(node, args...);
99 // Default logic does nothing.
100 }
101
109 template<size_t ChildNumber, typename Child, typename Root, typename... Args>
110 CODI_INLINE void link(Child const& child, Root const& root, Args&&... args) {
111 CODI_UNUSED(root, args...);
112 // Default logic forwards to node evaluation.
113 toNode(child, std::forward<Args>(args)...);
114 }
115
117
118 protected:
119
120#ifndef DOXYGEN_DISABLE
121 template<typename TraversalImpl, bool endPoint = false>
122 struct CallSwitch {
123 public:
124 template<typename... Args>
125 CODI_INLINE static void call(TraversalImpl& impl, Args&&... args) {
126 impl.node(std::forward<Args>(args)...);
127 }
128 };
129
130 template<typename TraversalImpl>
131 struct CallSwitch<TraversalImpl, true> {
132 public:
133 template<typename... Args>
134 CODI_INLINE static void call(TraversalImpl& impl, Args&&... args) {
135 impl.leaf(std::forward<Args>(args)...);
136 }
137 };
138#endif
139
141 template<typename Node, typename... Args>
142 CODI_INLINE void toNode(Node const& node, Args&&... args) {
143 CallSwitch<Impl, Node::EndPoint>::call(cast(), node, std::forward<Args>(args)...);
144 }
145
147 template<typename Node, typename... Args>
148 CODI_INLINE void toLinks(Node const& node, Args&&... args) {
149 node.forEachLink(cast(), std::forward<Args>(args)...);
150 }
151 };
152}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition: config.h:457
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition: macros.hpp:94
CoDiPack - Code Differentiation Package.
Definition: codi.hpp:90
void CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition: macros.hpp:46
Node side interface for the traversal of expressions.
Definition: nodeInterface.hpp:56
Traversal of CoDiPack expressions.
Definition: traversalLogic.hpp:56
void eval(NodeInterface< Node > const &node, Args &&... args)
Start the evaluation of the logic on the given expression.
Definition: traversalLogic.hpp:70
void toNode(Node const &node, Args &&... args)
Helper method to distinguish between leaf nodes and normal nodes.
Definition: traversalLogic.hpp:142
T_Impl Impl
See TraversalLogic.
Definition: traversalLogic.hpp:59
void leaf(Node const &node, Args &&... args)
Called for all leaf nodes in the expression.
Definition: traversalLogic.hpp:97
void toLinks(Node const &node, Args &&... args)
Helper method which calls forEachLink on the node.
Definition: traversalLogic.hpp:148
void node(Node const &node, Args &&... args)
Called for each node in the expression.
Definition: traversalLogic.hpp:86
void link(Child const &child, Root const &root, Args &&... args)
Called for all links in the expression.
Definition: traversalLogic.hpp:110
Impl & cast()
Cast to the implementation.
Definition: traversalLogic.hpp:62