Debian Codegraph @

yo/advancedtemplates/examples/exprtempl9.cc

raw
1 #include <iostream>
2 #include <vector>
3 #include <functional>
4
5 using namespace std;
6
7 enum
8 {
9 ADD,
10 SUB,
11 };
12
13 template<typename LHS, typename RHS, int operation>
14 struct BinExpr
15 {
16 template<typename Type>
17 struct BasicType;
18
19 template<typename LH, typename RH, int oper>
20 struct BasicType<BinExpr<LH, RH, oper>>;
21
22 using ObjType = typename BasicType<RHS>::ObjType;
23 using value_type = typename ObjType::value_type;
24
25
26 template <typename LH, typename RH, int oper>
27 struct Operation;
28
29 LHS const &d_lhs;
30 RHS const &d_rhs;
31
32 BinExpr(LHS const &lhs, RHS const &rhs)
33 :
34 d_lhs(lhs),
35 d_rhs(rhs)
36 {}
37
38 size_t size() const
39 {
40 return d_lhs.size();
41 }
42
43 value_type operator[](size_t ix) const
44 {
45 return Operation<LHS, RHS, operation>::cpt(d_lhs[ix], d_rhs[ix]);
46 }
47
48 operator ObjType() const
49 {
50 ObjType retVal;
51
52 retVal.reserve(size());
53
54 for (size_t ix = 0, end = size(); ix != end; ++ix)
55 new(&retVal[ix]) value_type{ (*this)[ix] };
56
57 return retVal;
58 }
59 };
60
61 template<typename LHS, typename RHS, int operation>
62 template<typename Type>
63 struct BinExpr<LHS, RHS, operation>::BasicType
64 {
65 using ObjType = Type;
66 };
67
68 template<typename LHS, typename RHS, int operation>
69 template<typename LH, typename RH, int oper>
70 struct BinExpr<LHS, RHS, operation>::BasicType<BinExpr<LH, RH, oper>>
71 {
72 using ObjType = typename BinExpr<LH, RH, oper>::ObjType;
73 };
74
75 template<typename LHS, typename RHS, int operation>
76 template <typename LH, typename RH>
77 struct BinExpr<LHS, RHS, operation>::Operation<LH, RH, ADD>
78 {
79 static value_type cpt(value_type const &lhs, value_type const &rhs)
80 {
81 return lhs + rhs;
82 };
83 };
84
85
86 template<typename LHS, typename RHS, int operation>
87 template <typename LH, typename RH>
88 struct BinExpr<LHS, RHS, operation>::Operation<LH, RH, SUB>
89 {
90 static value_type cpt(value_type const &lhs, value_type const &rhs)
91 {
92 return lhs - rhs;
93 };
94 };
95
96 // define the operators: ADD, SUB, etc. The operation to perform is
97 // specified as enum value
98
99 template<typename LHS, typename RHS>
100 BinExpr<LHS, RHS, ADD> operator+(LHS const &lhs, RHS const &rhs)
101 {
102 return BinExpr<LHS, RHS, ADD>(lhs, rhs);
103 }
104
105 template<typename LHS, typename RHS>
106 BinExpr<LHS, RHS, SUB> operator-(LHS const &lhs, RHS const &rhs)
107 {
108 return BinExpr<LHS, RHS, SUB>(lhs, rhs);
109 }
110
111
112 struct VI: public vector<int> // define the data structure to
113 { // operate on
114 static size_t const s_max = 10000;
115
116 VI()
117 :
118 vector<int>(s_max, 1)
119 {}
120 };
121
122 int main(int argc, char **argv)
123 {
124 if (argc == 1)
125 {
126 cout << "arg1: number of iterations\n";
127 return 0;
128 }
129
130 VI a, b, c, d;
131
132 for (size_t idx = 0, count = stoul(argv[1]); idx != count; ++idx)
133 d = a + b + c + d + a + b + c + d;
134
135 cout << d.front() << ", " << d.back() << '\n';
136
137 for (size_t idx = 0, count = stoul(argv[1]); idx != count; ++idx)
138 d = a - b - c - d - a - b - c - d;
139
140 cout << d.front() << ", " << d.back() << '\n';
141 }