Author biography xi
Foreword xiii
Preface xv
Acknowledgments xxi
1. Introduction 1
What are APIs? 1
What’s different about API design? 4
Why should you use APIs? 7
When should you avoid APIs? 11
API examples 13
Libraries, frameworks, and software development kits 17
File formats and network protocols 19
About this book 21
2. Qualities 25
Model the problem domain 25
Hide implementation details 31
Minimally complete 42
Easy to use 50
Loosely coupled 65
Stable, documented, and tested 80
3. Patterns 81
Pimpl idiom 83
Singleton 93
Factory Methods 101
API wrapping patterns 107
Observer pattern 115
4. Design 123
A case for good design 123
Gathering functional requirements 129
Creating use cases 132
Elements of API design 139
Architecture design 142
Class design 154
Function design 167
5. Styles 179
Flat C APIs 180
Object-oriented Cþþ APIs 186
Template-based APIs 189
Functional APIs 193
Data-driven APIs 197
6. Cþþ usage 209
Namespaces 209
Constructors and assignment 211
Const correctness 216
Templates 220
Operator overloading 227
Function parameters 236
Avoid #define for constants 239
Avoid using friends 242
Exporting symbols 244
Coding conventions 247
7. Cþþ revisions 251
Which Cþþ revision to use 251
Cþþ11 API features 252
Cþþ14 API features 283
Cþþ17 API features 289
Cþþ20 API features 307
Cþþ23 API features 322
8. Performance 329
Pass input arguments by const reference 331
Minimize #include dependencies 333
Declaring constants 338
Initialization lists 341
Memory optimization 343
Don’t inline functions until you need to 348
Copy on write 351
Iterating over elements 356
Performance analysis 360
9. Concurrency 367
Multithreading with Cþþ 367
Terminology 369
Accessing shared data 373
Concurrent API design 378
10. Versioning 383
Version numbers 383
Software branching strategies 388
Life cycle of an API 392
Levels of compatibility 393
How to maintain backward compatibility 401
API reviews 408
11. Documentation 415
Reasons to write documentation 415
Types of documentation 423
Documentation usability 430
Using Doxygen 434
12. Testing 443
Reasons to write tests 443
Types of API testing 446
Writing good tests 454
Writing testable code 459
Automated testing tools 476
13. Objective-C and Swift 487
Interface design in Cþþ and Objective-C 487
Data hiding in Objective-C 489
Objective-C behind a Cþþ API 491
Cþþ behind an Objective-C API 494
Cþþ behind a Swift API 497
14. Scripting 501
Adding script bindings 501
Script binding technologies 506
Adding Python bindings with Boost Python 510
Adding Ruby bindings with SWIG 521
15. Extensibility 533
Extending via plugins 533
Extending via inheritance 549
Extending via templates 560
Appendix A: Libraries 565
References 583
Index 589
API Design for C++, Second Edition provides a comprehensive discussion of Application Programming Interface (API) development, from initial design through implementation, testing, documentation, release, versioning, maintenance, and deprecation. It is the only book that teaches the strategies of C++ API development, including interface design, versioning, scripting, and plug-in extensibility. Drawing from the author's experience on large scale, collaborative software projects, the text offers practical techniques of API design that produce robust code for the long-term. It presents patterns and practices that provide real value to individual developers as well as organizations. The Second Edition includes all new material fully updated for the latest versions of C++, including a new chapter on concurrency and multithreading, as well as a new chapter discussing how Objective C++ and C++ code can co-exist and how a C++ API can be accessed from Swift programs. In addition, it explores often overlooked issues, both technical and non-technical, contributing to successful design decisions that produce high quality, robust, and long-lived APIs. It focuses on various API styles and patterns that will allow you to produce elegant and durable libraries. A discussion on testing strategies concentrates on automated API testing techniques rather than attempting to include end-user application testing techniques such as GUI testing, system testing, or manual testing.