Team-Fly® Table of Content
Total Page:16
File Type:pdf, Size:1020Kb
Modern C++ Design: Generic Programming and Design Patterns Applied By Andrei Alexandrescu Publisher : Addison Wesley Pub Date : February 01, 2001 ISBN : 0-201-70431-5 Table of Contents Pages : 352 Modern C++ Design is an important book. Fundamentally, it demonstrates 'generic patterns' or 'pattern templates' as a powerful new way of creating extensible designs in C++--a new way to combine templates and patterns that you may never have dreamt was possible, but is. If your work involves C++ design and coding, you should read this book. Highly recommended.-Herb Sutter What's left to say about C++ that hasn't already been said? Plenty, it turns out.-From the Foreword by John Vlissides In Modern C++ Design, Andrei Alexandrescu opens new vistas for C++ programmers. Displaying extraordinary creativity and programming virtuosity, Alexandrescu offers a cutting-edge approach to design that unites design patterns, generic programming, and C++, enabling programmers to achieve expressive, flexible, and highly reusable code. This book introduces the concept of generic components-reusableY design templates that produce boilerplate code for compiler consumption-allL within C++. Generic components enable an easier and more seamless transition from design to application code, generate code that better expresses the original designF intention, and support the reuse of design structures with minimal recoding. M The author describes the specificA C++ techniques and features that are used in building generic components and goes on to implement industrial strength generic components for real-world applications. RecuE rring issues that C++ developers face in their day-to-day activity are discussedT in depth and implemented in a generic way. These include: • Policy-based design for flexibility • Partial template specialization • Typelists-powerful type manipulation structures • Patterns such as Visitor, Singleton, Command, and Factories • Multi-method engines For each generic component, the book presents the fundamental problems and design options, and finally implements a generic solution. In addition, an accompanying Web site, http://www.awl.com/cseng/titles/0-201-70431-5, makes the code implementations available for the generic components in the book and provides a free, downloadable C++ library, called Loki, created by the author. Loki provides out-of-the-box functionality for virtually any C++ project. Team-Fly® Table of Content Table of Content .................................................................................................................. i Copyright............................................................................................................................. vi Foreword............................................................................................................................ vii Foreword............................................................................................................................. ix Preface................................................................................................................................. x Audience......................................................................................................................... xi Loki.................................................................................................................................. xi Organization.................................................................................................................. xii Acknowledgments ...........................................................................................................xiii Part I: Techniques................................................................................................................... 1 Chapter 1. Policy-Based Class Design........................................................................... 2 1.1 The Multiplicity of Software Design ...................................................................... 2 1.2 The Failure of the Do-It-All Interface .................................................................... 3 1.3 Multiple Inheritance to the Rescue? ..................................................................... 4 1.4 The Benefit of Templates ....................................................................................... 5 1.5 Policies and Policy Classes ................................................................................... 6 1.6 Enriched Policies ..................................................................................................... 9 1.7 Destructors of Policy Classes.............................................................................. 10 1.8 Optional Functionality Through Incomplete Instantiation ................................ 11 1.9 Combining Policy Classes.................................................................................... 12 1.10 Customizing Structure with Policy Classes..................................................... 13 1.11 Compatible and Incompatible Policies............................................................. 14 1.12 Decomposing a Class into Policies .................................................................. 16 1.13 Summary .............................................................................................................. 17 Chapter 2. Techniques .................................................................................................... 19 2.1 Compile-Time Assertions ..................................................................................... 19 2.2 Partial Template Specialization........................................................................... 22 2.3 Local Classes......................................................................................................... 23 2.4 Mapping Integral Constants to Types ................................................................ 24 2.5 Type-to-Type Mapping.......................................................................................... 26 2.6 Type Selection ....................................................................................................... 28 2.7 Detecting Convertibility and Inheritance at Compile Time .............................. 29 2.8 A Wrapper Around type_info.......................................................................... 32 2.9 NullType and EmptyType ................................................................................ 34 2.10 Type Traits ........................................................................................................... 34 2.11 Summary .............................................................................................................. 40 Chapter 3. Typelists ......................................................................................................... 42 3.1 The Need for Typelists.......................................................................................... 42 3.2 Defining Typelists .................................................................................................. 43 3.3 Linearizing Typelist Creation ............................................................................... 45 3.4 Calculating Length................................................................................................. 45 3.5 Intermezzo.............................................................................................................. 46 3.6 Indexed Access ..................................................................................................... 47 3.7 Searching Typelists............................................................................................... 48 3.8 Appending to Typelists ......................................................................................... 49 3.9 Erasing a Type from a Typelist............................................................................ 50 3.10 Erasing Duplicates .............................................................................................. 51 3.11 Replacing an Element in a Typelist .................................................................. 52 3.12 Partially Ordering Typelists................................................................................ 53 3.13 Class Generation with Typelists........................................................................ 56 3.14 Summary .............................................................................................................. 65 3.15 Typelist Quick Facts ...................................................................................... 66 ii Chapter 4. Small-Object Allocation................................................................................ 68 4.1 The Default Free Store Allocator ........................................................................ 68 4.2 The Workings of a Memory Allocator ................................................................. 69 4.3 A Small-Object Allocator ...................................................................................... 70 4.4 Chunks ................................................................................................................... 71 4.5 The Fixed-Size Allocator ...................................................................................... 74 4.6 The SmallObjAllocator Class...................................................................... 77 4.7 A Hat Trick.............................................................................................................