Modern C++ Design. Generic Programming and Design Patterns Applied. Andrei Alexandrescu. .~Addison-Wesley

Similar documents
Modern C++ Design. Generic Programming and Design Patterns Applied. Andrei Alexandrescu. AAddison-Wesley

Modern C++ Design. Generic Programming and Design Patterns Applied. Andrei Alexandrescu

Modern C++ Design: Generic Programming and Design Patterns Applied By UAndrei AlexandrescuU

Application Programming

The Unified Modeling Language User Guide

Programming with POSIX Threads

Programming in Python 3

PYTHON. p ykos vtawynivis. Second eciitiovl. CO Ve, WESLEY J. CHUN

Quality Code. Software Testing Principles, Practices, and Patterns. Stephen Vance. AAddison-Wesley

Framework Design Guidelines

The Power of Events. An Introduction to Complex Event Processing in Distributed Enterprise Systems. David Luckham

Cloud Computing and SOA Convergence in Your Enterprise

LATEX. Leslie Lamport. Digital Equipment Corporation. Illustrations by Duane Bibby. v ADDISON-WESLEY

ECLIPSE RICH CLIENT PLATFORM

SQL Queries. for. Mere Mortals. Third Edition. A Hands-On Guide to Data Manipulation in SQL. John L. Viescas Michael J. Hernandez

Programming. Principles and Practice Using C++ Bjarne Stroustrup. / Addison-Wesley. Second Edition

C++ Coding Standards. 101 Rules, Guidelines, and Best Practices. Herb Sutter Andrei Alexandrescu. Boston. 'Y.'YAddison-Wesley

DATABASE SYSTEM CONCEPTS

Refactoring HTML. Improving the Design of Existing Web Applications. Elliotte Rusty Harold. TT rvaddison-wesley

An Introduction to Object-Oriented Programming

MariaDB Crash Course. A Addison-Wesley. Ben Forta. Upper Saddle River, NJ Boston. Indianapolis. Singapore Mexico City. Cape Town Sydney.

Fundamentals of. Database Systems. Shamkant B. Navathe. College of Computing Georgia Institute of Technology PEARSON.

Secure Coding in C and C++

Programming. In Ada JOHN BARNES TT ADDISON-WESLEY

Xcode 6 Start to Finish

Programming Guide. Aaftab Munshi Dan Ginsburg Dave Shreiner. TT r^addison-wesley

Digital System Design with SystemVerilog

Agile Principles, Patterns, and Practices in C#

Objects First with Java

Rails AntiPatterns. Chad Pytel. Best Practice Ruby on Rails Refactoring. Tammer Saleh. AAddison-Wesley

Database Concepts. David M. Kroenke UNIVERSITATSBIBLIOTHEK HANNOVER

Fit for Developing Software

Systems:;-'./'--'.; r. Ramez Elmasri Department of Computer Science and Engineering The University of Texas at Arlington

Programming Wireless Devices with the Java 2 Platform, Micro Edition

CLASSIC DATA STRUCTURES IN JAVA

A Document Preparation System. User's Guide and Reference Manual. Leslie Lamport

Real-Time Systems and Programming Languages

FUNDAMENTALS OF. Database S wctpmc. Shamkant B. Navathe College of Computing Georgia Institute of Technology. Addison-Wesley

The Java Tutorial. A Short Course on the Basics. Raymond Gallardo. Sowmya Kannan. AAddison-Wesley. Sharon Biocca Zakhour.

\ Smart Client 0" Deploymentwith v^ ClickOnce

VHDL. Douglas L. Perry. Third Edition

C++ Primer, Fifth Edition

Virtualization from the Trenches

DATA ABSTRACTION AND PROBLEM SOLVING WITH JAVA

C++ for System Developers with Design Pattern

Algorithmic Graph Theory and Perfect Graphs

Effective Stl 50 Specific Ways To Improve Your Use Of The... Effective Stl: 50 Specific Ways To Improve The Use Of The...

ECLIPSE MODELING PROJECT

Programming 8-bit PIC Microcontrollers in С

Making New Pseudo-Languages with C++

FUNDAMENTALS OF SEVENTH EDITION

CJT^jL rafting Cm ompiler

ony Gaddis Haywood Community College STARTING OUT WITH PEARSON Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto

0. Overview of this standard Design entities and configurations... 5

C for Electronic Engineering

Win32 Network Programming

Computers as Components Principles of Embedded Computing System Design

Visual C# Tony Gaddis. Haywood Community College STARTING OUT WITH. Piyali Sengupta. Third Edition. Global Edition contributions by.

dewhurst_index.qxd 10/16/02 1:54 PM Page 309 Index

JAVASCRIPT FOR PROGRAMMERS

An Introduction to Parallel Programming

Maya Python. for Games and Film. and the Maya Python API. A Complete Reference for Maya Python. Ryan Trowbridge. Adam Mechtley ELSEVIER

^l^s^^^^^^^^^^s^^^ ^.1^L^ gs *^gs (s^s^^^^s^^ ^S^^^^ls

Data Structures and Abstractions with Java

LabVIEW Graphical Programming

Essentials. Oracle Solaris Cluster. Tim Read. Upper Saddle River, NJ Boston Indianapolis San Francisco. Capetown Sydney Tokyo Singapore Mexico City

Anany Levitin 3RD EDITION. Arup Kumar Bhattacharjee. mmmmm Analysis of Algorithms. Soumen Mukherjee. Introduction to TllG DCSISFI &

Information Modeling and Relational Databases

Access ComprehGnsiwG. Shelley Gaskin, Carolyn McLellan, and. Nancy Graviett. with Microsoft

The Designer's Guide to VHDL Second Edition

Coding for Penetration

Wrapping a complex C++ library for Eiffel. FINAL REPORT July 1 st, 2005

Embedded Systems Architecture

World Wide Web PROGRAMMING THE PEARSON EIGHTH EDITION. University of Colorado at Colorado Springs

Domain-Specific. Languages. Martin Fowler. AAddison-Wesley. Sydney Tokyo. With Rebecca Parsons

IPHONE FOR PROGRAMMERS: AN APP-DRIVEN APPROACH

Engineering Real- Time Applications with Wild Magic

DATA STRUCTURES AND PROBLEM SOLVING USING JAVA

OpenGL SUPERBIBLE. Fifth Edition. Comprehensive Tutorial and Reference. Richard S. Wright, Jr. Nicholas Haemel Graham Sellers Benjamin Lipchak

Integrated Approach. Operating Systems COMPUTER SYSTEMS. LEAHY, Jr. Georgia Institute of Technology. Umakishore RAMACHANDRAN. William D.

THE AVR MICROCONTROLLER AND EMBEDDED SYSTEMS. Using Assembly and С

WS01/02 - Design Pattern and Software Architecture

Simulation Modeling and Analysis

C The new standard

Essentials of Database Management

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Microsoft. Microsoft Visual C# Step by Step. John Sharp

Workbook for C++ THIRD EDITION. Yedidyah Langsam, Ph.D. Brooklyn College of the City University of New York. Custom Publishing

Modern Information Retrieval

Clean Code. A Handbook of Agile Software Craftsmanship. The Object Mentors: Robert С Martin

An Introduction to Search Engines and Web Navigation

Business Driven Data Communications

MPI: A Message-Passing Interface Standard

DB2 SQL Tuning Tips for z/os Developers

Data Structures in C++ Using the Standard Template Library

CCNA Cisco Certified Network Associate Study Guide

Microsoft Visual Studio 2010

Fundarnentals of. Sharnkant B. Navathe College of Computing Georgia Institute of Technology

Elements Of Programming Interviews In Java The Insiders Guide

6 Architecture of C++ programs

Transcription:

Modern C++ Design Generic Programming and Design Patterns Applied Andrei Alexandrescu.~Addison-Wesley Boston " San Francisco " New York " Toronto " Montreal London " Munich " Paris " Madrid Capetown " Sydney + Tokyo " Singapore " Mexico City

Contents Foreword by Scott Meyers Foreword by John Vlissides Preface Acknowledgments xi xv xvii xxi Part I Techniques Chapter 1 Policy-Based Class Design 1.1 The Multiplicity of Software Design 3 1.2 The Failure of the Do-It-All Interface 1.3 Multiple Inheritance to the Rescue? 5 1.4 The Benefit of Templates 1.5 Policies and Policy Classes 7 1.6 Enriched Policies 12 1.7 Destructors of Policy Classes 12 1.8 Optional Functionality Through Incomplete Instantiation 13 1.9 Combining Policy Classes 14 1.10 Customizing Structure with Policy Classes 16 1.11 Compatible and Incompatible Policies 17 1.12 Decomposing a Class into Policies 19 1.13 Summary 20

Vi Contents Chapter 2 Techniques 2.1 Compile-Time Assertions 2,2 Partial Template Specialization 2.3 Local Classes 2.4 Mapping Integral Constants to Types 2.5 Type-to-Type Mapping 2.6 Type Selection 2.7 Detecting Convertibility and Inheritance at Compile Time 2.8 A Wrapper Around type-i nfo 2.9 NuI ltype and EmptyType 2.10 Type Traits 2.11 Summary 23 23 26 28 29 31 33 34 37 39 40 46 Chapter 3 Typelists 49 3.1 The Need for Typelists 49 3.2 Defining Typelists 51 3.3 Linearizing Typelist Creation 52 3.4 Calculating Length 53 3.5 Intermezzo 54 3.6 Indexed Access 55 3.7 Searching Typelists 56 3.8 Appending to Typelists 57 3.9 Erasing a Type from a Typelist 58 3.10 Erasing Duplicates 59 3.11 Replacing an Element in a Typelist 60 3.12 Partially Ordering Typelists 61 3,13 Class Generation with Typelists 3.14 64 Summary 3.15 74 Typel i st Quick Facts 75 Chapter 4 Small-Obj ect Allocation 4.1 The Default Free Store Allocator 4.2 The Workings of a Memory Allocator 4.3 A Small-Object Allocator 4.4 Chunks 4.5 The Fixed-Size Allocator 4.6 The Smal 1 objal 1 ocato r Class 4.7 A Hat Trick 4.8 Simple, Complicated, Yet Simple in the End 4.9 Admirtistrivia 4.10 Summary 4.11 Small-Object Allocator Quick Facts 77 78 78 80 81 84 87 89 92 93 94 94

Contents Vii Part 11 Components 97 Chapter 5 Generalized Functors 99 5.1 The Command Design Pattern 100 5.2 Command in the Real World 102 5.3 C++ Callable Entities 103 5.4 The Fun ctor Class Template Skeleton 104 5.5 Implementing the Forwarding Functor : :operator() 108 5.6 Handling Functors 110 5.7 Build One, Get One Free 112 5.8 Argument and Return Type Conversions 114 5.9 Handling Pointers to Member Functions 115 5.10 Binding 119 5.11 Chaining Requests 122 5.12 Real-World Issues 1. The Cost of Forwarding Functions 122 5.13 Real-World Issues u: Heap Allocation 124 5.14 Implementing Undo and Redo with Functor 125 5.15 Summary 126 5.16 Functor Quick Facts 126 Chapter 6 Implementing Singletons 129 6,1 Static Data + Static Functions!= Singleton 130 6.2 The Basic C++ Idioms Supporting Singleton 131 6.3 Enforcing the Singleton's Uniqueness 132 6.4 Destroying the Singleton 133 6.5 The Dead Reference Problem 135 6.6 Addressing the Dead Reference Problem (I): The Phoenix Singleton 137 6.7 Addressing the Dead Reference Problem (1:C) : Singletons with Longevity 139 6.8 Implementing Singletons with Longevity 142 6.9 Living in a Multithreaded World 145 6.10 Putting It All Together 148 6.11 Working with Si ngl etonhoi der 153 6.12 Summary 155 6.13 51 ngl etonhoi der Class Template Quick Facts 155 Chapter 7 Smart Pointers 157 7.1 Smart Pointers 101 157 7.2 The Deal 158 7.3 Storage of Smart Pointers 160 7.4 Smart Pointer Member Functions 161

viii Contents 7.5 Ownership-Handling Strategies 7.6 The Address-of Operator 7.7 Implicit Conversion to Raw Pointer Types 7.8 Equality and Inequality 7.9 Ordering Comparisons 7.10 Checking and Error Reporting 7.11 Smart Pointers to const and const Smart Pointers 7.12 Arrays 7.13 Smart Pointers and Multithreading 7.14 Putting It All Together 7.15 Summary 7.16 SmartPtr Quick Facts. 163 170 177. 173 178 181 182 183 184 187 194 194 Chapter 8 Object Factories 197 8.1 The Need for Object Factories 198 8.2 Object Factories in C++ : Classes and Objects 200 8.3 Implementing an Object Factory 201 8.4 Type Identifiers 206 8.5 Generalization 207 8.6 Minutiae 210 8.7 Clone Factories 8.8 211 Using Object Factories with Other Generic Components 8 215.9 Summary 216 8.10 Facto ry Class Template Quick Facts 8.11 216 C1 onefactory Class Template Quick Facts 217 Chapter 9 Abstract Factory 219 9.1 The Architectural Role of Abstract Factory 219 9.2 A Generic Abstract Factory Interface 9.3 Implementing 223 AbstractFactory 9.4 A Prototype-Based 226 Abstract Factory Implementation 9.5 Summary 228 9.6 AbstractFacto 233 ry and ConcreteFacto ry Quick Facts 233 Chapter 10 Visitor 10.1 Visitor Basics 235 10.2 Overloading 235 and the Catch-All Function 10.3 An 242 Implementation Refinement : The Acyclic Visitor 10.4 A Generic Implementation 243 of Visitor 10.5 Back to the "Cyclic" 248 Visitor 10.6 Hooking Variations 255 10.7 Summary 258 260 10.8 Vi sitor Generic Component Quick Facts 261

Contents ix Chapter 11 Multimethods 263 11.1 What Are Multimethods? 264 11.2 When Are Multimethods Needed? 264 11.3 Double Switch-on-Type : Brute Force 265 11.4 The Brute-Force Approach Automated 268 11.5 Symmetry with the Brute-Force Dispatcher 273 11.6 The Logarithmic Double Dispatcher 276 11.7 FnDi spatch er and Symmetry 282 11.8 Double Dispatch to Functors 282 11.9 Converting Arguments: stati c_cast or dynami c_cast? 285 11.10 Constant-Time Multimethods : Raw Speed 290 11.11 Bas-icDispatcherandBasicFastDispatcherasPolicies 293 11.12 Looking Forward 294 11.13 Summary 296 11.14 Double Dispatcher Quick Facts 297 Appendix A Minimalist Multithreading Library 307. A.1 A Critique o Multithreading 302 A.2 Loki's Approach 303 A.3 Atomic Operations on Integral Types 303 A.4 Mutexes 305 A.5 Locking Semantics in Object-Oriented Programming 306 A.6 Optional volatile Modifier 308 A.7 Semaphores, Events, and Other Good Things 309 A.8 Summary 309 Bibliography 317.