-
Notifications
You must be signed in to change notification settings - Fork 80
/
G27_StrongType.cpp
165 lines (119 loc) · 4.32 KB
/
G27_StrongType.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**************************************************************************************************
*
* \file G27_StrongType.cpp
* \brief Guideline 27: Use CRTP for Static Mixin Classes
*
* Copyright (C) 2022 Klaus Iglberger - All Rights Reserved
*
* This file is part of the supplemental material for the O'Reilly book "C++ Software Design"
* (https://www.oreilly.com/library/view/c-software-design/9781098113155/).
*
**************************************************************************************************/
//---- <Addable.h> --------------------------------------------------------------------------------
template< typename Derived >
struct Addable
{
friend Derived& operator+=( Derived& lhs, Derived const& rhs ) {
lhs.get() += rhs.get();
return lhs;
}
friend Derived operator+( Derived const& lhs, Derived const& rhs ) {
return Derived{ lhs.get() + rhs.get() };
}
};
//---- <Subtractable.h> ---------------------------------------------------------------------------
template< typename Derived >
struct Subtractable
{
friend Derived& operator-=( Derived& lhs, Derived const& rhs ) {
lhs.get() -= rhs.get();
return lhs;
}
friend Derived operator-( Derived const& lhs, Derived const& rhs ) {
return Derived{ lhs.get() - rhs.get() };
}
};
//---- <IntegralArithmetic.h> ---------------------------------------------------------------------
//#include <Addable.h>
//#include <Subtractable.h>
template< typename Derived >
struct IntegralArithmetic
: public Addable<Derived>
, public Subtractable<Derived>
{};
//---- <Printable.h> ------------------------------------------------------------------------------
#include <iosfwd>
template< typename Derived >
struct Printable
{
friend std::ostream& operator<<( std::ostream& os, Derived const& d )
{
os << d.get();
return os;
}
};
//---- <Swappable.h> ------------------------------------------------------------------------------
#include <utility>
template< typename Derived >
struct Swappable
{
friend void swap( Derived& lhs, Derived& rhs )
{
using std::swap; // Enable ADL
swap( lhs.get(), rhs.get() );
}
};
//---- <StrongType.h> -----------------------------------------------------------------------------
#include <utility>
template< typename T, typename Tag, template<typename> class... Skills >
struct StrongType
: public Skills< StrongType<T,Tag,Skills...> >...
{
public:
using value_type = T;
explicit constexpr StrongType( T const& value ) : value_( value ) {}
constexpr T& get() noexcept { return value_; }
constexpr T const& get() const noexcept { return value_; }
void swap( StrongType& other ) {
using std::swap;
swap( value_, other.value_ );
}
private:
T value_;
};
template< typename T, typename Tag, template<typename> class... Skills >
std::ostream& operator<<( std::ostream& os, StrongType<T,Tag,Skills...> const& nt )
{
os << nt.get();
return os;
}
template< typename T, typename Tag, template<typename> class... Skills >
void swap( StrongType<T,Tag,Skills...>& a, StrongType<T,Tag,Skills...>& b )
{
a.swap( b );
}
//---- <Distances.h> ------------------------------------------------------------------------------
//#include <IntegralArithmetic.h>
//#include <Printable.h>
//#include <StrongType.h>
//#include <Swappable.h>
template< typename T >
using Meter = StrongType<T,struct MeterTag,IntegralArithmetic,Printable,Swappable>;
template< typename T >
using Kilometer = StrongType<T,struct KilometerTag,IntegralArithmetic,Printable,Swappable>;
//---- <Person.h> ---------------------------------------------------------------------------------
//#include <StrongType.h>
#include <string>
using Surname = StrongType<std::string,struct SurnameTag,Printable,Swappable>;
//---- <Main.cpp> ---------------------------------------------------------------------------------
//#include <Distances.h>
#include <cstdlib>
#include <iostream>
int main()
{
auto const m1 = Meter<long>{ 100 };
auto const m2 = Meter<long>{ 50 };
auto const m3 = m1 + m2; // Compiles and results in 150 meters
std::cout << "\n m3 = " << m3 << "m\n\n";
return EXIT_SUCCESS;
}