DUNE-ACFEM (unstable)

literals.hh
1#ifndef __DUNE_ACFEM_COMMON_LITERALS_HH__
2#define __DUNE_ACFEM_COMMON_LITERALS_HH__
3
4#include "types.hh"
5#include "../mpl/accumulate.hh"
6#include "../mpl/sequenceslice.hh"
7#include "../mpl/compare.hh"
8#include "fractionconstant.hh"
9
10namespace Dune {
11
12 namespace ACFem {
13
14 namespace Literals {
15
21 namespace {
22
23 template<class Seq, class SFINAE = void>
24 struct IntParserHelper;
25
30 template<char c0, char... c>
31 struct IntParserHelper<Sequence<char, c0, c...>,
32 std::enable_if_t<c0 != '0' || sizeof...(c) == 0> >
33 {
34 using Digits = IndexSequence<(c0 - '0'), (c-'0')...>;
35 static constexpr std::size_t base = 10;
36 static_assert(Digits{} <= MakeConstantSequence<sizeof...(c)+1, 9>{},
37 "Expecting a sequence of non-negative decimal digits");
38 };
39
41 template<char c2, char... c>
42 struct IntParserHelper<Sequence<char, '0', c2, c...>, std::enable_if_t<c2 == 'x' || c2 == 'X'> >
43 {
44 using Digits = IndexSequence<((c >= '0' && c <= '9')
45 ? (c - '0')
46 : ((c >= 'a' && c <= 'f')
47 ? (10 + c - 'a')
48 : ((c >= 'A' && c <= 'F')
49 ? (10 + c - 'A')
50 : ~0UL)))...>;
51 static constexpr std::size_t base = 16;
52 static_assert(Digits{} <= MakeConstantSequence<sizeof...(c), 15>{},
53 "Expecting a sequence of non-negative binary digits");
54 };
55
57 template<char c2, char... c>
58 struct IntParserHelper<Sequence<char, '0', c2, c...>, std::enable_if_t<c2 == 'b' || c2 == 'B'> >
59 {
60 using Digits = IndexSequence<(c-'0')...>;
61 static constexpr std::size_t base = 2;
62 static_assert(Digits{} <= MakeConstantSequence<sizeof...(c), 1>{},
63 "Expecting a sequence of non-negative binary digits");
64 };
65
67 template<char c2, char... c>
68 struct IntParserHelper<
69 Sequence<char, '0', c2, c...>,
70 std::enable_if_t<c2 != 'x' && c2 != 'X' && c2 != 'b' && c2 != 'B'>
71 >
72 {
73 using Digits = IndexSequence<(c2-'0'), (c-'0')...>;
74 static constexpr std::size_t base = 8;
75 static_assert(Digits{} <= MakeConstantSequence<sizeof...(c)+1, 7>{},
76 "Expecting a sequence of non-negative octal digits");
77 };
78 }
79
80 namespace {
81 template<char... c>
82 constexpr auto integerLiteral()
83 {
84 using Traits = IntParserHelper<Sequence<char, c...> >;
85 using Digits = typename Traits::Digits;
86 constexpr std::size_t base = Traits::base;
87 return AccumulateSequence<MultiplyAddFunctor<std::size_t, base>, ReverseSequence<Digits> >{};
88 }
89 }
90
92 template<char... c>
93 constexpr auto operator"" _c ()
94 {
95 return integerLiteral<c...>();
96 }
97
101 template<char... c>
102 constexpr auto operator"" _f ()
103 {
104 return TypedValue::intFraction(integerLiteral<c...>());
105 }
106
108
109 } // NS Literals
110
111 } // NS ACFem
112
113} // NS Dune
114
115#endif // __DUNE_ACFEM_COMMON_LITERALS_HH__
MakeIndexSequence< N, Value, 0 > MakeConstantSequence
Generate a constant index sequence of the given size.
Definition: generators.hh:45
Sequence< std::size_t, V... > IndexSequence
Sequence of std::size_t values.
Definition: types.hh:64
std::integer_sequence< T, V... > Sequence
Sequence of any type of integer values.
Definition: types.hh:56
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Jun 19, 23:17, 2026)