Debian Codegraph @

yo/advancedtemplates/examples/exprtempl2.cc

raw
1 #include <iostream>
2 #include <string>
3 #include <vector>
4 #include <functional>
5
6 using namespace std;
7
8 template<typename LHS, typename RHS, template<typename> class Operation>
9 struct BinExpr;
10
11 template<typename RHS>
12 struct BasicType
13 {
14 using ObjType = RHS;
15 };
16
17 template<typename LHS, typename RHS, template<typename> class Operation>
18 struct BasicType<BinExpr<LHS, RHS, Operation>>
19 {
20 using ObjType = typename BinExpr<LHS, RHS, Operation>::ObjType;
21 };
22
23 size_t g_binExpr;
24
25 template<typename LHS, typename RHS, template <typename> class Operation>
26 struct BinExpr
27 {
28 string d_name;
29
30 using ObjType = typename BasicType<RHS>::ObjType;
31 using value_type = typename ObjType::value_type;
32
33 LHS const &d_lhs;
34 RHS const &d_rhs;
35
36 BinExpr(LHS const &lhs, RHS const &rhs)
37 :
38 d_name("BinExpr" + to_string(g_binExpr++)),
39 d_lhs(lhs),
40 d_rhs(rhs)
41 {
42 cout << "creating " << d_name <<
43 " holding " << lhs.d_name << " and " << rhs.d_name << '\n';
44 }
45
46 BinExpr(BinExpr const &other)
47 :
48 d_name(other.d_name + "_cc"),
49 d_lhs(other.d_lhs),
50 d_rhs(other.d_rhs)
51 {
52 cout << "BinExpr copy cons " << d_name << "\n";
53 }
54
55 BinExpr(BinExpr &&tmp)
56 :
57 d_name(tmp.d_name + "_mc"),
58 d_lhs(std::move(tmp.d_lhs)),
59 d_rhs(std::move(tmp.d_rhs))
60 {
61 cout << "BinExpr move cons " << d_name << "\n";
62 }
63
64 size_t size() const
65 {
66 return d_lhs.size();
67 }
68
69 value_type operator[](size_t ix) const
70 {
71 static Operation<value_type> operation;
72 cout << "computing " << d_lhs.d_name << '[' << ix << "] + " <<
73 d_rhs.d_name << '[' << ix << "]\n";
74
75 auto ret = operation(d_lhs[ix], d_rhs[ix]);
76
77 cout << "computed " << d_lhs.d_name << '[' << ix << "] + " <<
78 d_rhs.d_name << '[' << ix << "]\n";
79
80 return ret;
81
82 }
83
84 operator ObjType() const
85 {
86 ObjType retVal;
87 for (size_t ix = 0; ix != d_lhs.size(); ++ix)
88 retVal.push_back((*this)[ix]);
89 return retVal;
90 }
91 };
92
93 template<typename LHS, typename RHS>
94 BinExpr<LHS, RHS, plus> operator+(LHS const &lhs, RHS const &rhs)
95 {
96 return BinExpr<LHS, RHS, plus>(lhs, rhs);
97 }
98
99 struct V: public vector<int>
100 {
101 string d_name;
102 static size_t s_nr;
103
104 V(string name, int value)
105 :
106 vector<int> {value},
107 d_name(name)
108 {
109 cout << "creating V " << d_name << '\n';
110 }
111
112 V()
113 :
114 d_name("tmp" + to_string(s_nr++))
115 {
116 cout << "creating V " << d_name << '\n';
117 }
118
119 V(V const &other)
120 :
121 vector<int>(other),
122 d_name(other.d_name + "_cc")
123 {
124 cout << "V copy cons " << d_name << "\n";
125 }
126 V(V &&tmp)
127 :
128 vector<int>(std::move(tmp)),
129 d_name(tmp.d_name + "_mc")
130 {
131 cout << "V move cons " << d_name << "\n";
132 }
133
134 V &operator=(V const &other)
135 {
136 cout << "assign " << other.d_name << " to " << d_name << '\n';
137 d_name = other.d_name;
138 static_cast<vector<int>&>(*this) = other;
139 return *this;
140 }
141
142 V &operator=(V &&tmp)
143 {
144 cout << "moving " << tmp.d_name << " to " << d_name << '\n';
145 d_name = std::move(tmp.d_name);
146 static_cast<vector<int>&>(*this) = std::move(tmp);
147 return *this;
148 }
149 };
150
151 size_t V::s_nr;
152
153 int main()
154 {
155 V one{"one", 1};
156 V two{"two", 2};
157 V three{"three", 3};
158 V four{"four", 0};
159
160 four = one + two + three + four;
161
162 cout << four.d_name << ' ' << four.size() << " first value = " <<
163 four[0] << '\n';
164 }