1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| #include <iostream>
template <typename T, typename ... Args>
class Mecanism
{
public:
Mecanism() {};
virtual ~Mecanism() = 0;
virtual T getValue(Args ... args) = 0;
};
template <typename T, typename ... Args>
Mecanism<T,Args...>::~Mecanism()
{}
template <typename T, typename U>
class CarreXMecanism : public Mecanism<T,U>
{
public:
CarreXMecanism() {};
~CarreXMecanism() {};
T getValue(U arg1)
{
return (T)(arg1*arg1);
}
};
template <typename T, typename U>
class Plus100Mecanism : public Mecanism<T,U>
{
public:
Plus100Mecanism() {};
~Plus100Mecanism() {};
T getValue(U arg1)
{
return (T)(arg1+100);
}
};
template <typename T, typename U, typename V>
class CarreXYMecanism : public Mecanism<T,U,V>
{
public:
CarreXYMecanism() {};
~CarreXYMecanism() {};
T getValue(U arg1, V arg2)
{
return (T)(arg1*arg1+arg2*arg2);
}
};
template <typename T, typename ... Args>
class function
{
public:
function(Mecanism<T,Args...> &mec) : m_mec(mec) {}
~function() {};
T getValue(Args ... args) {return m_mec.getValue(args...);}
private:
Mecanism<T,Args...> &m_mec;
};
template <unsigned int S, typename ... Args>
class Composee;
template <>
class Composee<0>{};
template <unsigned int S, typename T, typename U, typename ... Args>
class Composee<S, T, U, Args ...> : public Composee<0>
{
public:
Composee(Composee<S-1,U,Args...> &XFunction, Mecanism<T,U> &mec) : m_mec(mec), m_func(XFunction) {}
~Composee() {};
template <typename ... Args2>
T getValue(Args2 ... args2) {return m_mec.getValue(m_func.getValue(args2...));}
private:
Mecanism<T,U> &m_mec;
Composee<S-1,U,Args...> &m_func;
};
template <typename T, typename U, typename ... Args>
class Composee<0, T, U, Args ...> : public Composee<0>
{
public:
Composee(function<U,Args...> &XFunction, Mecanism<T,U> &mec) : m_mec(mec), m_func(XFunction) {}
~Composee() {};
T getValue(Args ... args) {return m_mec.getValue(m_func.getValue(args...));}
private:
Mecanism<T,U> &m_mec;
function<U,Args...> &m_func;
};
using namespace std;
int main()
{
CarreXYMecanism<int, double, char> m1;
function<int, double, char> f1(m1);
std::cout<<f1.getValue(10,'.')<<" premiere valeur. "<<std::endl; //affiche 10*10+la valeur ascii du point au carré
Plus100Mecanism<double, int> m2;
Composee<0, double, int, double, char> c1(f1,m2);
std::cout<<c1.getValue(8.596,'A')<<" seconde valeur. "<<std::endl; //affiche (char)(8.596*8.596+65*65)+100
CarreXMecanism<long, double> m3;
Composee<1, long, double, int, double, char> c2(c1,m3);
std::cout<<c2.getValue(8.596,'A')<<" troisieme valeur. "<<std::endl; //affiche ((char)(8.596*8.596+65*65)+100)²
return 0;
} |
Partager