AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
SwitchCases.hpp
1#pragma once
2
3#include <cassert>
4
5#include <dune/common/hybridutilities.hh>
6#include <dune/common/rangeutilities.hh>
7
8namespace AMDiS
9{
10 // static switching. Iterates over all possible values
11 template <class T, T to, T from, class Value, class Then, class Else>
12 constexpr decltype(auto) switchCases(const Dune::StaticIntegralRange<T,to,from>& cases, const Value& value,
13 Then&& thenBranch, Else&& elseBranch)
14 {
15 using integer_sequence = typename Dune::StaticIntegralRange<T,to,from>::integer_sequence;
16 return Dune::Hybrid::switchCases(integer_sequence{}, value, FWD(thenBranch), FWD(elseBranch));
17 }
18
19 // dynamic switching. Calls the `thenBranch` or `elseBranch` directly
20 template <class T, class Value, class Then, class Else>
21 constexpr decltype(auto) switchCases(const Dune::IntegralRange<T>& cases, const Value& value,
22 Then&& thenBranch, Else&& elseBranch)
23 {
24 if (value >= cases[0] && value <= cases[cases.size()-1])
25 return thenBranch(value);
26 else
27 return elseBranch(value);
28 }
29
30 // static switching. Iterates over all possible values
31 template<class T, T to, T from, class Value, class Then>
32 constexpr void switchCases(const Dune::StaticIntegralRange<T,to,from>& cases, const Value& value, Then&& thenBranch)
33 {
34 using integer_sequence = typename Dune::StaticIntegralRange<T,to,from>::integer_sequence;
35 Dune::Hybrid::switchCases(integer_sequence{}, value, FWD(thenBranch));
36 }
37
38 // dynamic switching. Calls the `thenBranch` directly
39 template<class T, class Value, class Then>
40 constexpr void switchCases(const Dune::IntegralRange<T>& cases, const Value& value, Then&& thenBranch)
41 {
42 assert(value >= cases[0] && value <= cases[cases.size()-1]);
43 thenBranch(value);
44 }
45
46} // end namespace AMDiS