Line data Source code
1 : ///////////////////////////////////////////////////////////////////////////
2 : // Inastemp - Berenger Bramas MPCDF - 2016
3 : // Under MIT Licence, please you must read the LICENCE file.
4 : ///////////////////////////////////////////////////////////////////////////
5 : #ifndef INAIFELSE_HPP
6 : #define INAIFELSE_HPP
7 :
8 : #include <functional>
9 : #include <type_traits>
10 :
11 : template < class VecType >
12 : class InaIfElse {
13 : public:
14 : using MaskType = typename VecType::MaskType;
15 :
16 : class ThenClass;
17 :
18 : class ElseIfClass {
19 : protected:
20 : MaskType cumulatedTest;
21 : VecType currentValue;
22 :
23 : public:
24 : inline ElseIfClass(const MaskType& inCumulatedTest,
25 : const VecType& inCurrentValue)
26 : : cumulatedTest(inCumulatedTest), currentValue(inCurrentValue) {
27 : }
28 :
29 : inline ThenClass ElseIf(const MaskType& inTestRes) {
30 126 : return ThenClass(inTestRes, cumulatedTest, currentValue);
31 : }
32 :
33 : template<class FunctionType>
34 : inline typename std::enable_if<!std::is_convertible<FunctionType,VecType>::value, VecType>::type
35 : Else(FunctionType snippet) {
36 224 : const VecType snippetRes = static_cast<VecType>(snippet());
37 112 : return Else(snippetRes);
38 : }
39 :
40 : inline VecType Else(const VecType& rawValue) {
41 552 : currentValue = VecType::BitsOr(VecType::IfFalse(cumulatedTest, rawValue), currentValue);
42 : return currentValue;
43 : }
44 :
45 : inline operator VecType() const {
46 : return currentValue;
47 : }
48 : };
49 :
50 : class ThenClass {
51 : protected:
52 : MaskType onGoingTest;
53 : MaskType cumulatedTest;
54 : VecType currentValue;
55 :
56 : public:
57 : inline ThenClass(const MaskType& inOnGoingTest,
58 : const MaskType& inCumulatedTest, const VecType& inCurrentValue)
59 : : onGoingTest(inOnGoingTest),
60 : cumulatedTest(inCumulatedTest), currentValue(inCurrentValue) {
61 : }
62 :
63 : template<class FunctionType>
64 : inline typename std::enable_if<!std::is_convertible<FunctionType,VecType>::value, ElseIfClass>::type
65 : Then(FunctionType snippet) {
66 448 : const VecType snippetRes = static_cast<VecType>(snippet());
67 224 : return Then(snippetRes);
68 : }
69 :
70 : inline ElseIfClass Then(const VecType& rawValue) {
71 1440 : currentValue = VecType::BitsOr(VecType::IfFalse(cumulatedTest,
72 672 : VecType::IfTrue(onGoingTest, rawValue)),
73 : currentValue);
74 648 : cumulatedTest = MaskType::Or(cumulatedTest, onGoingTest);
75 336 : return ElseIfClass(cumulatedTest, currentValue);
76 : }
77 :
78 : inline operator VecType() const {
79 : return currentValue;
80 : }
81 : };
82 :
83 : class IfClass {
84 : public:
85 : inline IfClass() {
86 : }
87 :
88 : inline ThenClass If(const MaskType& inTestRes) {
89 270 : return ThenClass(inTestRes, MaskType(false), VecType::GetZero());
90 : }
91 : };
92 : };
93 :
94 : #endif
|