Debian Codegraph @

yo/concrete/examples/binopclasses.cc

raw
1 #include <istream>
2 #include <ostream>
3 #include <utility>
4
5 // The complete interface of the class template Binops: if additional
6 // operators are overloaded Binops requires no modifications.
7 //
8 template <typename Derived>
9 class Binops
10 {
11 template <typename Der>
12 friend std::ostream &operator<<(
13 std::ostream &out, Binops<Der> const &rhs);
14
15 template <typename Der>
16 friend std::istream &operator>>(std::istream &in, Binops<Der> &rhs);
17
18 void eWrap(std::istream &in);
19 void iWrap(std::ostream &out) const;
20 };
21
22 // Completing the overloads for operator+: all function templates, that
23 // depend on operator+=(...) defined in Derived
24 //
25 template <class Derived, typename Rhs>
26 Derived &operator+=(Binops<Derived> &lhs, Rhs const &rhs)
27 {
28 Derived tmp{ Derived{static_cast<Derived &>(lhs)} += rhs };
29 tmp.swap(static_cast<Derived &>(lhs));
30 return static_cast<Derived &>(lhs);
31 }
32
33 template <class Derived, typename Rhs>
34 Derived operator+(Binops<Derived> &&lhs, Rhs const &rhs)
35 {
36 return static_cast<Derived &&>(std::move(lhs)) += rhs;
37 }
38
39 template <class Derived, typename Rhs>
40 Derived operator+(Binops<Derived> const &lhs, Rhs const &rhs)
41 {
42 return Derived{static_cast<Derived const &>(lhs)} += rhs;
43 }
44
45 // Other operators may be added here, and are implemented analogously, see
46 // the Annotations.
47
48 // Implementing the insertion operator. The extraction operator is
49 // implemented analogously, see the Annotations:
50 //
51 template <typename Derived>
52 inline void Binops<Derived>::iWrap(std::ostream &out) const
53 {
54 static_cast<Derived const &>(*this).insert(out);
55 }
56
57 template <typename Derived>
58 inline std::ostream &operator<<(std::ostream &out, Binops<Derived> const &rhs)
59 {
60 rhs.iWrap(out);
61 return out;
62 }
63
64
65
66
67
68
69 ////////////////////////////////////////////////////////////////////////
70
71 // User area: the user defines a class Derived, and wants to provide
72 // addition and insertion: the operator+=() && and insert() functions have
73 // to be implemented and Derived derives from Binops<Derived>. For insert
74 // a friend declaration is also required.
75
76 #include <iostream>
77
78 using namespace std;
79
80 class Derived: public Binops<Derived>
81 {
82 friend Binops<Derived>; // only required for stream insertion/extraction
83
84 int d_value = 0;
85
86 public:
87 Derived() = default;
88
89 Derived(int value)
90 :
91 d_value(value)
92 {}
93
94 void swap(Derived &other);
95
96 Derived &&operator+=(Derived const &rhs) &&;
97
98 private:
99 void insert(std::ostream &out) const;
100 };
101
102 inline void Derived::insert(std::ostream &out) const
103 {
104 out << "Inserting a Derived object";
105 }
106
107 Derived &&Derived::operator+=(Derived const &rhs) &&
108 {
109 d_value += rhs.d_value;
110 return std::move(*this);
111 }
112
113 inline void Derived::swap(Derived &rhs)
114 {
115 std::swap(d_value, rhs.d_value);
116 }
117
118 int main()
119 {
120 Derived o1{ 1 },
121 Derived o2{ 2 };
122 o1 += o2;
123 // o1 *= o2; // adding this statement results in
124 // a compilation error
125
126 o1 = o1 + o2;
127 o1 += o1;
128
129 cout << o1 << '\n';
130 }