author | alpar |
Tue, 15 Feb 2005 14:56:54 +0000 | |
changeset 1147 | 014054ffd9d2 |
permissions | -rw-r--r-- |
alpar@1001 | 1 |
// -*- mode:C++ -*- |
alpar@1001 | 2 |
|
alpar@1001 | 3 |
////////////////////////////////////////////////////////////////////// |
alpar@1001 | 4 |
// Named "function" parameters |
alpar@1001 | 5 |
////////////////////////////////////////////////////////////////////// |
alpar@1001 | 6 |
|
alpar@1001 | 7 |
class named_fn |
alpar@1001 | 8 |
{ |
alpar@1001 | 9 |
int _id; |
alpar@1001 | 10 |
double _val; |
alpar@1001 | 11 |
int _dim; |
alpar@1001 | 12 |
|
alpar@1001 | 13 |
public: |
alpar@1001 | 14 |
named_fn() : _id(0), _val(1), _dim(2) {} |
alpar@1001 | 15 |
named_fn& id(int p) { _id = p ; return *this; } |
alpar@1001 | 16 |
named_fn& val(double p) { _val = p ; return *this; } |
alpar@1001 | 17 |
named_fn& dim(int p) { _dim = p ; return *this; } |
alpar@1001 | 18 |
|
alpar@1001 | 19 |
~named_fn() { |
alpar@1001 | 20 |
//Itt van maga az algoritmus |
alpar@1001 | 21 |
} |
alpar@1001 | 22 |
}; |
alpar@1001 | 23 |
|
alpar@1001 | 24 |
//Hasznalat: |
alpar@1001 | 25 |
//named_fn().id(3); |
alpar@1001 | 26 |
//named_fn().id(3).val(2); |
alpar@1001 | 27 |
//named_fn().dim(4).id(3); |
alpar@1001 | 28 |
|
alpar@1001 | 29 |
|
alpar@1001 | 30 |
////////////////////////////////////////////////////////////////////// |
alpar@1001 | 31 |
// Named class template parameters (A) |
alpar@1001 | 32 |
////////////////////////////////////////////////////////////////////// |
alpar@1001 | 33 |
|
alpar@1001 | 34 |
template<class A=int,class B=double> |
alpar@1001 | 35 |
class Named_T |
alpar@1001 | 36 |
{ |
alpar@1001 | 37 |
public: |
alpar@1001 | 38 |
|
alpar@1001 | 39 |
typedef A Atype; |
alpar@1001 | 40 |
typedef B Btype; |
alpar@1001 | 41 |
|
alpar@1001 | 42 |
template <class T> class SetAType : public Named_T<T,Btype> { }; |
alpar@1001 | 43 |
template <class T> class SetBType : public Named_T<Atype,T> { }; |
alpar@1001 | 44 |
}; |
alpar@1001 | 45 |
|
alpar@1001 | 46 |
// Named_T<>::SetAType<double>::SetBType<double> |
alpar@1001 | 47 |
|
alpar@1001 | 48 |
////////////////////////////////////////////////////////////////////// |
alpar@1001 | 49 |
// Named class template parameters (A) |
alpar@1001 | 50 |
////////////////////////////////////////////////////////////////////// |
alpar@1001 | 51 |
|
alpar@1001 | 52 |
struct _NTR |
alpar@1001 | 53 |
{ |
alpar@1001 | 54 |
typedef int Atype; |
alpar@1001 | 55 |
typedef double Btype; |
alpar@1001 | 56 |
}; |
alpar@1001 | 57 |
|
alpar@1001 | 58 |
template<class TR=_NTR> |
alpar@1001 | 59 |
class Named_TR |
alpar@1001 | 60 |
{ |
alpar@1001 | 61 |
public: |
alpar@1001 | 62 |
|
alpar@1001 | 63 |
typedef typename TR::Atype Atype; |
alpar@1001 | 64 |
typedef typename TR::Btype Btype; |
alpar@1001 | 65 |
|
alpar@1001 | 66 |
Atype a; |
alpar@1001 | 67 |
Btype b; |
alpar@1001 | 68 |
|
alpar@1001 | 69 |
template <class T> |
alpar@1001 | 70 |
struct ATR : public TR { |
alpar@1001 | 71 |
typedef T Atype; |
alpar@1001 | 72 |
}; |
alpar@1001 | 73 |
|
alpar@1001 | 74 |
template <class T> |
alpar@1001 | 75 |
class SetAType : public Named_TR<ATR<T> > { }; |
alpar@1001 | 76 |
|
alpar@1001 | 77 |
template <class T> |
alpar@1001 | 78 |
struct BTR : public TR { |
alpar@1001 | 79 |
typedef T Btype; |
alpar@1001 | 80 |
}; |
alpar@1001 | 81 |
template <class T> |
alpar@1001 | 82 |
class SetBType : public Named_TR<BTR<T> > { }; |
alpar@1001 | 83 |
|
alpar@1001 | 84 |
Named_TR() {}; |
alpar@1001 | 85 |
Named_TR<TR> &setA(Atype _a) { a=_a; return *this;} |
alpar@1001 | 86 |
Named_TR<TR> &setB(Btype _b) { b=_b; return *this;} |
alpar@1001 | 87 |
|
alpar@1001 | 88 |
void run() { |
alpar@1001 | 89 |
// itt az algoritmus |
alpar@1001 | 90 |
} |
alpar@1001 | 91 |
|
alpar@1001 | 92 |
////////////////////////////////////////////////////////////////////// |
alpar@1001 | 93 |
template<class T> |
alpar@1001 | 94 |
SetAType<T> SETA(T t) { SetAType<T> r; r.a=t; r.b=b; return r;} |
alpar@1001 | 95 |
template<class T> |
alpar@1001 | 96 |
SetBType<T> SETB(T t) { SetBType<T> r; r.a=a; r.b=t; return r;} |
alpar@1001 | 97 |
}; |
alpar@1001 | 98 |
|
alpar@1001 | 99 |
// Hasznalat: |
alpar@1001 | 100 |
// 1. |
alpar@1001 | 101 |
// Named_TR<>::SetAType<double> nt; |
alpar@1001 | 102 |
// Named_TR<>::SetBType<double>::SetAType<double> nt2; |
alpar@1001 | 103 |
// nt2.setA(5).setB(6).run(); |
alpar@1001 | 104 |
// 2. |
alpar@1001 | 105 |
// double x; |
alpar@1001 | 106 |
// Named_TR<>().SETA(5.2).SETB(x).run(); |
alpar@1001 | 107 |
// 3. |
alpar@1001 | 108 |
// struct MyTr : public _NTR { typedef float Btype; }; |
alpar@1001 | 109 |
// int main() |
alpar@1001 | 110 |
// { |
alpar@1001 | 111 |
// Named_TR<MyTr> d2; d2=d2; |
alpar@1001 | 112 |
// } |
alpar@1001 | 113 |
|
alpar@1001 | 114 |
|
alpar@1001 | 115 |
// Sajnos ezt csak a fuggvenyen kivul lehet deklaralni: |
alpar@1001 | 116 |
struct MyTr : public _NTR { typedef float Btype; }; |
alpar@1001 | 117 |
|
alpar@1001 | 118 |
typedef Named_T<> Named_TN; |
alpar@1001 | 119 |
|
alpar@1001 | 120 |
int main() |
alpar@1001 | 121 |
{ |
alpar@1001 | 122 |
|
alpar@1001 | 123 |
Named_T<> a;a=a; |
alpar@1001 | 124 |
Named_T<>::SetAType<double> b;b=b; |
alpar@1001 | 125 |
Named_T<>::SetAType<double>::SetBType<int> c;c=c; |
alpar@1001 | 126 |
|
alpar@1001 | 127 |
Named_TR<> a2;a2=a2; |
alpar@1001 | 128 |
Named_TR<>::SetAType<double> b2;b2=b2; |
alpar@1001 | 129 |
Named_TR<>::SetAType<double>::SetBType<int> c2;c2=c2; |
alpar@1001 | 130 |
|
alpar@1001 | 131 |
//De igy is lehet: |
alpar@1001 | 132 |
Named_TR<MyTr> d2; d2=d2; |
alpar@1001 | 133 |
|
alpar@1001 | 134 |
named_fn().id(3); |
alpar@1001 | 135 |
named_fn().id(3).val(2); |
alpar@1001 | 136 |
named_fn().dim(4).id(3); |
alpar@1001 | 137 |
|
alpar@1001 | 138 |
Named_TR<>::SetAType<double> nt; |
alpar@1001 | 139 |
Named_TR<>::SetBType<double>::SetAType<double> nt2; |
alpar@1001 | 140 |
nt2.setA(5).setB(6).run(); |
alpar@1001 | 141 |
|
alpar@1001 | 142 |
double x; |
alpar@1001 | 143 |
Named_TR<>().SETA(5.2).SETB(x).run(); |
alpar@1001 | 144 |
} |
alpar@1001 | 145 |