logo资料库

C++ Reactive Programming.pdf

第1页 / 共319页
第2页 / 共319页
第3页 / 共319页
第4页 / 共319页
第5页 / 共319页
第6页 / 共319页
第7页 / 共319页
第8页 / 共319页
资料共319页,剩余部分请下载后查看
Cover
Title Page
Copyright and Credits
Packt Upsell
Contributors
Table of Contents
Preface
Chapter 1: Reactive Programming Model – Overview and History
Event-driven programming model
Event-driven programming on X Windows
Event-driven programming on Microsoft Windows
Event-driven programming under Qt
Event-driven programming under MFC
Other event-driven programming models
Limitations of classical event processing models
Reactive programming model
Functional reactive programming
The key interfaces of a reactive program
Pull-versus push-based reactive programming
The IEnumerable/IObservable duality
Converting events to IObservable
The philosophy of our book
Summary
Chapter 2: A Tour of Modern C++ and its Key Idioms
The key concerns of the C++ programming language
Zero cost abstraction
Expressivity
Substitutability
Enhancements to C++ for writing better code
Type deduction and inference
Uniform initialization of variables
Variadic templates
Rvalue references 
Move semantics
Smart pointers
Lambda functions
Functors and Lambdas
Composition, currying, and partial function application
Function wrappers
Composing functions together with the pipe operator
Miscellaneous features
Fold expressions
Variant type
Other important topics
Range-based for loops and observables
Summary
Chapter 3: Language-Level Concurrency and Parallelism in C++
What is concurrency?
Hello World of concurrency (using std::thread)
Managing threads
Thread launch
Thread join
Passing arguments into a thread
Using Lambdas
Ownership management
Sharing data between threads
Mutexes
Avoiding deadlock
Locking with std::unique_lock
Condition variables
A thread-safe stack data structure
Summary
Chapter 4: Asynchronous and Lock-Free Programming in C++
Task-based parallelism in C++
Future and promise
std::packaged_task
std::async
C++ memory model
Memory access and concurrency
The modification contract
Atomic operations and types in C++
Atomic types
std::atomic_flag
std::atomic
Standard atomic integral types
std::atomic – pointer arithmetic
std::atomic<> primary class template
Memory ordering
Sequential consistency
Acquire-release ordering
Relaxed ordering
A lock-free data structure  queue
Summary
Chapter 5: Introduction to Observables
The GoF Observer pattern
The limitations of the GoF Observer pattern
A holistic look at GoF patterns
The OOP programming model and hierarchies
A Composite/Visitor pattern for expression processing
Flattening the composite for iterative processing
Map and filter operations on the list
Reversing the gaze for Observables!
Summary
Chapter 6: Introduction to Event Stream Programming Using C++
What is Stream programming model?
Advantages of the Stream programming model
Applied Stream programming using the Streams library
Lazy evaluation
A simple Stream program
Aggregating values using the Stream paradigm
The STL and the Stream paradigm
A word about the Streams library
Event Stream programming
Advantages of Event Stream programming
The Streamulus library and its programming model
The Streamulus library – a peek into its internals
The Streamulus Library – a look into expression processing
The spreadsheet Library — a change-propagation engine
RaftLib – another Stream-processing library
What do these things have to do with Rx programming?
Summary
Chapter 7: Introduction to Data Flow Computation and the RxCpp Library
The data flow computation paradigm
An introduction to the RxCpp library
The RxCpp library and its programming model
A simple Observable/Observer interaction
Filters and transformations with Observables
Streaming values from C++ containers
Creating Observables from scratch
Concatenating Observable Streams
Unsubscribing from Observable Streams
An introduction to marble diagrams for visual representation
RxCpp (Stream) operators
The average operator
The scan operator
Composing operators through the pipe operator
Working with Schedulers
A tale of two operators – flat versus concat map
More operators that are of importance
A peek into the things we haven't covered yet
Summary
Chapter 8: RxCpp – the Key Elements
Observables
What's a producer?
Hot versus cold Observables
Hot Observables
Hot Observables and the replay mechanism
Observers and their variants (subscribers)
Subjects
Schedulers
ObserveOn versus SubscribeOn
The RunLoop Scheduler
Operators
Creational operators
Transformation operators
Filtering operators
Combining operators
Error-handling operators
Observable utility operators
Conditional and Boolean operators
Mathematical and aggregate operators
Connectable Observable operators
Summary
Chapter 9: Reactive GUI Programming Using Qt/C++
A quick introduction to Qt GUI programming
Qt object model
Signals and slots
Event system
Event handlers
Sending events
Meta-object system
Hello World – Qt program
Qt event model with signals/slots/MOC – an example
Creating a custom widget
Creating the application dialog
Executing the application
Integrating the RxCpp library with the Qt event model
Qt event filter – a reactive approach
Creating the window – setting layouts and alignments
Event type specific observables
An introduction to RxQt
Summary
Chapter 10: Design Patterns and Idioms for C++ Rx Programming
The OOP and design patterns movement
Key pattern catalogs
GOF patterns
POSA catalog
The design pattern redux
From design patterns to reactive programming
Flattening the hierarchy to navigate through them
From iterators to observables
The cell pattern
The active object pattern
Resource loan pattern
The event bus pattern
Summary
Chapter 11: Reactive Microservices Using C++
The C++ language and web programming
The REST programming model
The C++ REST SDK
HTTP client programming using the C++ REST SDK
HTTP server programming using the C++ REST SDK
Testing the HTTP server using CURL and POSTMAN
The libcurl and the HTTP client programming
Kirk Shoop's CURL Wrapper library
The JSON and HTTP protocols
The C++ REST SDK-based REST server
Invoking REST services using the RxCurl library
A word about the Reactive microservices architecture
Fine-grained services
Polyglot persistence
Independent deployment
Service orchestration and choreography
Reactive web service call
Summary
Chapter 12: Advanced Streams and Handling Errors
A short recap of the characteristics of a reactive system
RxCpp error and exception handling operators
Executing an action on an error
Resuming when an error occurs
Retry when an error occurs
Cleanup with the finally() operator
Schedulers and error handling
Event-based Stream handling – some examples
Aggregation based on Stream data
Application event handling example
Summary
Other Books You May Enjoy
Index
C++ Reactive Programming Design concurrent and asynchronous applications using the RxCpp library and Modern C++17 Praseed Pai Peter Abraham BIRMINGHAM - MUMBAI
C++ Reactive Programming Copyright © 2018 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Richa Tripathi Acquisition Editor: Sandeep Mishra Content Development Editor: Rohit Singh Technical Editor: Ketan Kamble Copy Editor: Safis Editing Project Coordinator: Vaidehi Sawant Proofreader: Safis Editing Indexer: Aishwarya Gangawane Graphics: Jason Monteiro Production Coordinator: Shraddha Falebhai First published: June 2018 Production reference: 1280618 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78862-977-5 www.packtpub.com
mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content PacktPub.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors About the authors Praseed Pai has been working in the software industry for the last 25 years, starting his career as a MS-DOS systems programmer using ANSI C. He has been actively involved in developing large-scale, cross-platform, native code-based systems using C++ on Windows, GNU Linux, and macOS X. He has experience in COM+ and CORBA programming using C++. In the last decade, he has worked with Java- and .NET-based systems. He is the primary implementer of the SLANG4.net compilation system, which has been ported to C++ with an LLVM backend. He coauthored .NET Design Patterns, by Packt Publishing. Peter Abraham has been a performance fanatic and a C/C++ programming language enthusiast right from his college days, where he excelled in Microsoft Windows programming and GNU Linux programming. He garnered experience in working with CUDA, image processing, and computer graphics programs by virtue of working with companies such as Quest Global, Siemens, and Tektronics. Peter has been eagerly following the C++ standard and RxCpp libraries as part of his profession. He has worked with cross-platform GUI toolkits such as Qt, WxWidgets, and FOX toolkit.
About the reviewer Sumant Tambe is a software engineer, researcher, open source contributor, blogger, speaker, author, and gamer. He is experienced in using Modern C++, Kafka, data distribution service, reactive programming, and stream processing to solve new problems in big data and industrial IoT. He has authored C++ Truths blog and the More C++ Idioms wikibook. He shares his learnings on his blog, and at local code camps, meetups, and conferences. He has been a recipient of the Microsoft MVP Award in development technologies for 5 years. He has a PhD in computer science from Vanderbilt University. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Table of Contents Preface Chapter 1: Reactive Programming Model – Overview and History Event-driven programming model Event-driven programming on X Windows Event-driven programming on Microsoft Windows Event-driven programming under Qt Event-driven programming under MFC Other event-driven programming models Limitations of classical event processing models Reactive programming model Functional reactive programming The key interfaces of a reactive program Pull-versus push-based reactive programming Converting events to IObservable The philosophy of our book Summary The IEnumerable/IObservable duality Chapter 2: A Tour of Modern C++ and its Key Idioms The key concerns of the C++ programming language Zero cost abstraction Expressivity Substitutability Enhancements to C++ for writing better code Type deduction and inference Uniform initialization of variables Variadic templates Rvalue references Move semantics Smart pointers Lambda functions Functors and Lambdas Composition, currying, and partial function application Function wrappers Composing functions together with the pipe operator Miscellaneous features Fold expressions Variant type 1 6 7 7 9 11 12 13 13 14 14 15 17 17 21 25 27 28 29 29 29 32 33 33 35 36 38 39 41 43 44 47 49 50 52 52 53
Table of Contents Other important topics Range-based for loops and observables Summary Chapter 3: Language-Level Concurrency and Parallelism in C++ What is concurrency? Hello World of concurrency (using std::thread) Managing threads Thread launch Thread join Passing arguments into a thread Using Lambdas Sharing data between threads Ownership management Mutexes Avoiding deadlock Locking with std::unique_lock Condition variables A thread-safe stack data structure Summary Chapter 4: Asynchronous and Lock-Free Programming in C++ Task-based parallelism in C++ Future and promise std::packaged_task std::async C++ memory model Memory access and concurrency The modification contract Atomic operations and types in C++ Atomic types std::atomic_flag std::atomic std::atomic – pointer arithmetic std::atomic<> primary class template Standard atomic integral types Memory ordering Sequential consistency Acquire-release ordering Relaxed ordering A lock-free data structure queue Summary Chapter 5: Introduction to Observables The GoF Observer pattern [ ii ] 54 54 58 59 60 61 62 63 64 65 68 69 71 73 75 78 80 82 85 86 87 87 90 91 93 94 95 95 96 98 100 102 103 104 105 105 107 109 110 112 114 115
分享到:
收藏