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