C++ Reactive Programming

Book description

Learn how to implement the reactive programming paradigm with C++ and build asynchronous and concurrent applications

Key Features

  • Efficiently exploit concurrency and parallelism in your programs
  • Use the Functional Reactive programming model to structure programs
  • Understand reactive GUI programming to make your own applications using Qt

Book Description

Reactive programming is an effective way to build highly responsive applications with an easy-to-maintain code base. This book covers the essential functional reactive concepts that will help you build highly concurrent, event-driven, and asynchronous applications in a simpler and less error-prone way.

C++ Reactive Programming begins with a discussion on how event processing was undertaken by different programming systems earlier. After a brisk introduction to modern C++ (C++17), you'll be taken through language-level concurrency and the lock-free programming model to set the stage for our foray into the Functional Programming model. Following this, you'll be introduced to RxCpp and its programming model. You'll be able to gain deep insights into the RxCpp library, which facilitates reactive programming. You'll learn how to deal with reactive programming using Qt/C++ (for the desktop) and C++ microservices for the Web.

By the end of the book, you will be well versed with advanced reactive programming concepts in modern C++ (C++17).

What you will learn

  • Understand language-level concurrency in C++
  • Explore advanced C++ programming for the FRP
  • Uncover the RxCpp library and its programming model
  • Mix the FP and OOP constructs in C++ 17 to write well-structured programs
  • Master reactive microservices in C++
  • Create custom operators for RxCpp
  • Learn advanced stream processing and error handling

Who this book is for

If you're a C++ developer interested in using reactive programming to build asynchronous and concurrent applications, you'll find this book extremely useful. This book doesn't assume any previous knowledge of reactive programming.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. C++ Reactive Programming
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Reactive Programming Model – Overview and History
    1. Event-driven programming model
      1. Event-driven programming on X Windows
      2. Event-driven programming on Microsoft Windows
      3. Event-driven programming under Qt
      4. Event-driven programming under MFC
      5. Other event-driven programming models
      6. Limitations of classical event processing models
    2. Reactive programming model
    3. Functional reactive programming
    4. The key interfaces of a reactive program
    5. Pull-versus push-based reactive programming
      1. The IEnumerable/IObservable duality
    6. Converting events to IObservable<T>
    7. The philosophy of our book
    8. Summary
  7. A Tour of Modern C++ and its Key Idioms
    1. The key concerns of the C++ programming language
      1. Zero cost abstraction
      2. Expressivity
      3. Substitutability
    2. Enhancements to C++ for writing better code
      1. Type deduction and inference
      2. Uniform initialization of variables
      3. Variadic templates
      4. Rvalue references 
      5. Move semantics
      6. Smart pointers
      7. Lambda functions
      8. Functors and Lambdas
      9. Composition, currying, and partial function application
      10. Function wrappers
    3. Composing functions together with the pipe operator
    4. Miscellaneous features
      1. Fold expressions
      2. Variant type
      3. Other important topics
    5. Range-based for loops and observables
    6. Summary
  8. Language-Level Concurrency and Parallelism in C++
    1. What is concurrency?
    2. Hello World of concurrency (using std::thread)
    3. Managing threads
      1. Thread launch
      2. Thread join
      3. Passing arguments into a thread
    4. Using Lambdas
      1. Ownership management
    5. Sharing data between threads
      1. Mutexes
      2. Avoiding deadlock
      3. Locking with std::unique_lock
      4. Condition variables
    6. A thread-safe stack data structure
    7. Summary
  9. Asynchronous and Lock-Free Programming in C++
    1. Task-based parallelism in C++
      1. Future and promise
      2. std::packaged_task
      3. std::async
    2. C++ memory model
    3. Memory access and concurrency
    4. The modification contract
    5. Atomic operations and types in C++
    6. Atomic types
      1. std::atomic_flag
      2. std::atomic<bool>
        1. Standard atomic integral types
      3. std::atomic<T*> – pointer arithmetic
      4. std::atomic<> primary class template
    7. Memory ordering
      1. Sequential consistency
      2. Acquire-release ordering
      3. Relaxed ordering
    8. A lock-free data structure  queue
    9. Summary
  10. Introduction to Observables
    1. The GoF Observer pattern
    2. The limitations of the GoF Observer pattern
    3. A holistic look at GoF patterns
    4. The OOP programming model and hierarchies
    5. A Composite/Visitor pattern for expression processing
    6. Flattening the composite for iterative processing
    7. Map and filter operations on the list
    8. Reversing the gaze for Observables!
    9. Summary
  11. Introduction to Event Stream Programming Using C++
    1. What is Stream programming model?
      1. Advantages of the Stream programming model
    2. Applied Stream programming using the Streams library
    3. Lazy evaluation
      1. A simple Stream program
      2. Aggregating values using the Stream paradigm
      3. The STL and the Stream paradigm
      4. A word about the Streams library
    4. Event Stream programming
      1. Advantages of Event Stream programming
      2. The Streamulus library and its programming model
        1. The Streamulus library – a peek into its internals
        2. The Streamulus Library – a look into expression processing
      3. The spreadsheet Library — a change-propagation engine
      4. RaftLib – another Stream-processing library
      5. What do these things have to do with Rx programming?
    5. Summary
  12. Introduction to Data Flow Computation and the RxCpp Library
    1. The data flow computation paradigm
    2. An introduction to the RxCpp library
      1. The RxCpp library and its programming model
      2. A simple Observable/Observer interaction
      3. Filters and Transformations with Observables
      4. Streaming values from C++ containers
    3. Creating Observables from the scratch
      1. Concatenating Observable Streams
      2. Unsubscribing from Observable Streams
    4. An introduction to marble diagrams for visual representation
    5. RxCpp (Stream) Operators
      1. The average Operator
      2. The Scan Operator
      3. Composing Operators through the pipe Operator
      4. Working with Schedulers
      5. A tale of two Operators – flatmap versus concatmap
      6. Other Important Operators
    6. A peek into the things we haven't covered yet
    7. Summary
  13. RxCpp – the Key Elements
    1. Observables
      1. What's a Producer?
      2. Hot versus Cold Observables
      3. Hot Observables
      4. Hot Observables and the replay mechanism
    2. Observers and their variants (Subscribers)
    3. Subjects
    4. Schedulers
      1. ObserveOn versus SubscribeOn
      2. The RunLoop Scheduler
    5. Operators
      1. Creational Operators
      2. Transformation Operators
      3. Filtering Operators
      4. Combining Operators
      5. Error-handling Operators
      6. Observable utility Operators
      7. Conditional and Boolean Operators
      8. Mathematical and Aggregate operators
      9. Connectable Observable Operators
    6. Summary
  14. Reactive GUI Programming Using Qt/C++
    1. A quick introduction to Qt GUI programming
      1. Qt object model
      2. Signals and slots
      3. Event system
      4. Event handlers
      5. Sending events
      6. Meta-object system
    2. Hello World – Qt program
    3. Qt event model with signals/slots/MOC – an example
      1. Creating a custom widget
      2. Creating the application dialog
      3. Executing the application
    4. Integrating the RxCpp library with the Qt event model
      1. Qt event filter – a reactive approach
      2. Creating the window – setting layouts and alignments
      3. Event type specific observables
      4. An introduction to RxQt
    5. Summary
  15. Creating Custom Operators in RxCpp
    1. Philosophy of Rx operators
    2. Chaining stock operators
    3. Writing basic RxCpp custom operators
      1. Writing an RxCpp operator as a function
      2. Writing an RxCpp operator as a Lambda
      3. Composing custom RxCpp operators
    4. Different genres of custom operators
      1. Writing a custom creational operator
      2. Writing a custom transformation operator
      3. Writing a custom operator that involves Schedulers
    5. Writing custom operators that can be chained
      1. Using the lift<t> operator to write a custom operator
      2. Converting an arbitrary Lambda to a custom Rx operator
      3. Creating a custom RxCpp operator in the library
    6. Summary
  16. Design Patterns and Idioms for C++ Rx Programming
    1. The OOP and Design patterns movement
    2. Key Pattern catalogs
      1. The GOF catalog
      2. The POSA catalog
    3. The Design pattern redux
    4. From Design patterns to Reactive programming
      1. Flattening the hierarchy to navigate through it
      2. From Iterators to Observables
    5. The Cell pattern
    6. The Active object pattern
    7. The Resource Loan pattern
    8. The Event bus pattern
    9. Summary
  17. Reactive Microservices Using C++
    1. The C++ language and web programming
      1. The REST programming model
    2. The C++ REST SDK
      1. HTTP client programming using the C++ REST SDK
      2. HTTP server programming using the C++ REST SDK
    3. Testing the HTTP server using CURL and POSTMAN
      1. The libcurl and the HTTP client programming
      2. Kirk Shoop's libCURL Wrapper library
    4. The JSON and HTTP protocol
    5. The C++ REST SDK-based REST server
    6. Invoking REST services using the RxCurl library
    7. A word about the Reactive micro-services architecture
      1. Fine-grained services
      2. Polyglot persistence
      3. Independent deployment
      4. Service orchestration and choreography
      5. Reactive web service call
    8. Summary
  18. Advanced Streams and Handling Errors
    1. A short recap of the characteristics of a reactive system
    2. RxCpp error and exception handling Operators
      1. Executing an action on an error
      2. Resuming when an error occurs
      3. Retry when an error occurs
      4. Cleanup with the finally() Operator
    3. Schedulers and error handling
    4. Event-based Stream handling – some examples
      1. Aggregation based on Stream data
      2. Application event handling example
    5. Summary
  19. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: C++ Reactive Programming
  • Author(s): Praseed Pai, Peter Abraham
  • Release date: June 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788629775