Andrei Alexandrescu9780201704310, 0201704315
Table of contents :
Modern C++ Design.pdf……Page 0
Table of Content……Page 2
Copyright……Page 6
Foreword……Page 7
Foreword……Page 9
Preface……Page 10
Loki……Page 11
Organization……Page 12
Acknowledgments……Page 13
Part I: Techniques……Page 15
1.1 The Multiplicity of Software Design……Page 16
1.2 The Failure of the Do-It-All Interface……Page 17
1.3 Multiple Inheritance to the Rescue?……Page 18
1.4 The Benefit of Templates……Page 19
1.5 Policies and Policy Classes……Page 20
1.5.1 Implementing Policy Classes with Template Template Parameters……Page 22
1.6 Enriched Policies……Page 23
1.7 Destructors of Policy Classes……Page 24
1.8 Optional Functionality Through Incomplete Instantiation……Page 25
1.9 Combining Policy Classes……Page 26
1.10 Customizing Structure with Policy Classes……Page 27
1.11 Compatible and Incompatible Policies……Page 28
1.12 Decomposing a Class into Policies……Page 30
1.13 Summary……Page 31
2.1 Compile-Time Assertions……Page 33
2.2 Partial Template Specialization……Page 36
2.3 Local Classes……Page 37
2.4 Mapping Integral Constants to Types……Page 38
2.5 Type-to-Type Mapping……Page 40
2.6 Type Selection……Page 42
2.7 Detecting Convertibility and Inheritance at Compile Time……Page 43
2.8 A Wrapper Around type_info……Page 46
2.10 Type Traits……Page 48
2.10.1 Implementing Pointer Traits……Page 49
2.10.2 Detection of Fundamental Types……Page 50
2.10.3 Optimized Parameter Types……Page 51
2.10.5 Using TypeTraits……Page 52
2.11 Summary……Page 54
Table 2.1. TypeTraits Members……Page 55
3.1 The Need for Typelists……Page 56
3.2 Defining Typelists……Page 57
3.4 Calculating Length……Page 59
3.5 Intermezzo……Page 60
3.6 Indexed Access……Page 61
3.7 Searching Typelists……Page 62
3.8 Appending to Typelists……Page 63
3.9 Erasing a Type from a Typelist……Page 64
3.10 Erasing Duplicates……Page 65
3.11 Replacing an Element in a Typelist……Page 66
Figure 3.1. A simple class hierarchy……Page 67
3.13.1 Generating Scattered Hierarchies……Page 70
Figure 3.3. Memory layout for WidgeInfo……Page 72
Figure 3.4. WidgeInfo inherits Holder twice……Page 73
3.13.2 Generating Tuples……Page 75
3.13.3 Generating Linear Hierarchies……Page 76
Figure 3.5. A size-optimized structure for WidgetEventHandler……Page 77
Figure 3.6. The class hirarchy generated by GenLinearHierarchy……Page 78
3.14 Summary……Page 79
3.15 Typelist Quick Facts……Page 80
Table 3.1. Compile-Time Algorithms Operating on Typelists……Page 81
4.1 The Default Free Store Allocator……Page 82
Figure 4.1. The memory map at program startup……Page 83
Figure 4.3. The layered structure of the small-object allocator……Page 84
4.4 Chunks……Page 85
Figure 4.4. A chunk of 255 blocks of 4 bytes each……Page 86
4.5 The Fixed-Size Allocator……Page 88
4.6 The SmallObjAllocator Class……Page 91
4.7 A Hat Trick……Page 93
4.8 Simple, Complicated, Yet Simple in the End……Page 95
4.9 Administrivia……Page 96
4.11 Small-Object Allocator Quick Facts……Page 97
Part II: Components……Page 99
Figure 5.1. The Command design pattern……Page 100
5.3 C++ Callable Entities……Page 103
5.4 The Functor Class Template Skeleton……Page 105
5.5 Implementing the Forwarding Functor::operator()……Page 109
5.6 Handling Functors……Page 110
5.7 Build One, Get One Free……Page 112
5.8 Argument and Return Type Conversions……Page 113
5.9 Handling Pointers to Member Functions……Page 115
5.10 Binding……Page 118
5.11 Chaining Requests……Page 120
5.12 Real-World Issues I: The Cost of Forwarding Functions……Page 121
5.13 Real-World Issues II: Heap Allocation……Page 122
5.15 Summary……Page 124
5.16 Functor Quick Facts……Page 125
6.1 Static Data + Static Functions != Singleton……Page 127
6.2 The Basic C++ Idioms Supporting Singletons……Page 128
6.4 Destroying the Singleton……Page 130
6.5 The Dead Reference Problem……Page 132
6.6 Addressing the Dead Reference Problem (I):The Phoenix Singleton……Page 134
6.6.1 Problems with atexit……Page 135
6.7 Addressing the Dead Reference Problem (II):Singletons with Longevity……Page 136
6.8 Implementing Singletons with Longevity……Page 139
6.9.1 The Double-Checked Locking Pattern……Page 142
6.10.1 Decomposing SingletonHolder into Policies……Page 144
6.10.2 Defining Requirements for SingletonHolder’s Policies……Page 145
6.10.3 Assembling SingletonHolder……Page 146
6.11 Working with SingletonHolder……Page 148
6.13 SingletonHolder Class Template Quick Facts……Page 150
7.1 Smart Pointers 101……Page 152
7.2 The Deal……Page 153
7.3 Storage of Smart Pointers……Page 154
7.4 Smart Pointer Member Functions……Page 156
Figure 7.1. Memory lay out for smart pointers with deep copy……Page 157
7.5.3 Reference Counting……Page 159
Figure 7.3. An alternate structure of reference counted pointers……Page 160
Figure 7.5. Reference linking in action……Page 161
7.5.5 Destructive Copy……Page 163
7.6 The Address-of Operator……Page 164
7.7 Implicit Conversion to Raw Pointer Types……Page 165
7.8 Equality and Inequality……Page 167
7.9 Ordering Comparisons……Page 171
7.10 Checking and Error Reporting……Page 173
7.10.3 Error Reporting……Page 174
7.12 Arrays……Page 175
7.13.1 Multithreading at the Pointee Object Level……Page 176
7.13.2.2 Multithreaded Reference Linking……Page 178
7.14 Putting It All Together……Page 179
7.14.1 The Storage Policy……Page 181
Table 7.2. Ownership Policy Constructs……Page 182
7.14.4 The Checking Policy……Page 184
7.16 SmartPtr Quick Facts……Page 185
Chapter 8. Object Factories……Page 187
8.1 The Need for Object Factories……Page 188
8.2 Object Factories in C++: Classes and Objects……Page 189
8.3 Implementing an Object Factory……Page 190
8.4 Type Identifiers……Page 194
8.5 Generalization……Page 195
8.6 Minutiae……Page 198
8.7 Clone Factories……Page 199
8.8 Using Object Factories with Other Generic Components……Page 202
8.10 Factory Class Template Quick Facts……Page 203
8.11 CloneFactory Class Template Quick Facts……Page 204
Figure 9.1. A hierarchy for a game with two levels of difficulty……Page 205
9.2 A Generic Abstract Factory Interface……Page 207
Figure 9.2. The class hierarchy generated for AbstractEnemyFactory……Page 209
9.3 Implementing AbstractFactory……Page 210
Figure 9.3. The class hierarchy generated for EasyLevelEnemyFactory……Page 212
9.4 A Prototype-Based Abstract Factory Implementation……Page 213
9.5 Summary……Page 216
9.6 AbstractFactory and ConcreteFactory Quick Facts……Page 217
10.1 Visitor Basics……Page 219
10.2 Overloading and the Catch-All Function……Page 224
10.3 An Implementation Refinement: The Acyclic Visitor……Page 225
Figure 10.2. Class structure for an acyclic visitor……Page 228
10.4 A Generic Implementation of Visitor……Page 229
Table 10.1. Component Names……Page 230
10.5 Back to the “Cyclic” Visitor……Page 235
10.6.1 The Catch-All Function……Page 237
10.8 Visitor Generic Components Quick Facts……Page 240
11.1 What Are Multimethods?……Page 242
11.2 When Are Multimethods Needed?……Page 243
11.3 Double Switch-on-Type: Brute Force……Page 244
11.4 The Brute-Force Approach Automated……Page 246
11.5 Symmetry with the Brute-Force Dispatcher……Page 251
11.6 The Logarithmic Double Dispatcher……Page 254
11.6.1 The Logarithmic Dispatcher and Inheritance……Page 256
11.6.2 The Logarithmic Dispatcher and Casts……Page 257
11.7 FnDispatcher and Symmetry……Page 259
11.8 Double Dispatch to Functors……Page 260
Figure 11.2. A diamond-shaped class hierarchy using virtual inheritance……Page 262
Figure 11.3. A diamond-shaped class hierarchy using nonvirtual inheritance……Page 263
11.10 Constant-Time Multimethods: Raw Speed……Page 266
Figure 11.5. Dispatching on Shaped and DrawingDevices……Page 267
11.11 BasicDispatcher and BasicFastDispatcher as Policies……Page 269
Table 11.1. DispatcherBackend Policy Requirements……Page 270
11.12 Looking Forward……Page 271
Table 11.2. Comparison of Various Implementations of Double Dispatch……Page 272
11.14 Double Dispatcher Quick Facts……Page 273
A.1 A Critique of Multithreading……Page 276
A.2 Loki’s Approach……Page 277
A.3 Atomic Operations on Integral Types……Page 278
A.4 Mutexes……Page 279
A.5 Locking Semantics in Object-Oriented Programming……Page 281
Table A.1. Implementations of ThreadingModel……Page 282
A.8 Summary……Page 283
Bibliography……Page 284
Reviews
There are no reviews yet.