Book Image

C++ Reactive Programming

By : Praseed Pai, Peter Abraham
Book Image

C++ Reactive Programming

By: Praseed Pai, Peter Abraham

Overview of this book

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).
Table of Contents (20 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Preface

This book will help you learn how to implement the reactive programming paradigm with C++ for building asynchronous and concurrent applications. The reactive programming model requires tremendous amount of pre-requisites in terms of proficiency in programming models (OOP/FP), event-driven GUI programming, language-level concurrency, lock free programming , design Patterns, and event stream programming. The first six chapters cover the these topics in a detailed manner. For the remaining chapters, we have based our discussions on industrial-strength RxCpp library. The topics covered include introduction to the RxCpp programming model, five key elements of the RxCpp programming model, GUI programming using Qt, writing custom operators, Rx design patterns, reactive microservices, and advanced exceptions/operators. By the end of the book, you will be able to confidently embed Rx constructs in your programs to write better concurrent and parallel applications using C++.

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. We cover the modern C++ constructs necessary to write reactive programs in Chapter 2A Tour of Modern C++ and its Key Idioms, Chapter 3, Language-Level Concurrency and Parallelism in C++, and Chapter 4, Asynchronous and Lock-Free Programming in C++. Any C++ programmer with reasonable familiarity with Classic C++ can easily follow the book without any difficulty.

What this book covers

Chapter 1, Reactive Programming Model – Overview and History, takes a look at the various event handling techniques implemented by GUI toolkits such as Windows API, XLib API, Qt, and MFC. This chapter also introduces some key data structures of the Rx programming model in the context of writing cross platform Console applications and GUI applications using the MFC library.

Chapter 2, A Tour of Modern C++ and its Key Idioms, covers the Modern C++ constructs necessary for writing reactive programs. The chapter focuses on new C++ features, type inference, variadic templates, rvalue references, move semantics, lambda functions, elementary functional programming, pipeable operators, implementation of iterators, and observers.

Chapter 3, Language-Level Concurrency and Parallelism in C++, discusses the threading library available with the C++ standard. You will learn how to launch and manage a thread. We will discuss different aspects of the threading library. This chapter lays a good foundation for concurrency support introduced in Modern C++.

Chapter 4, Asynchronous and Lock-Free Programming in C++, discusses the facilities provided by the standard library for implementing task-based parallelism. It also discusses the new multithreading-aware memory model that is available with the modern C++ language.

 Chapter 5, Introduction to Observables, talks about the GoF Observer pattern and explains its shortcomings. You will learn how to transform a program which implements the GoF Composite/Visitor pattern to Observable streams, using a technique devised by us, in the context of modelling an expression tree.

Chapter 6, Introduction to Event Stream Programming Using C++, focuses on the topic of Event Stream programming. We will also look at the Streamulus library, which provides a Domain Specific Embedded Language (DSEL) approach to the manipulation of event Streams.

Chapter 7, Introduction to Data Flow Computation and the RxCpp Library, starts with a conceptual overview of the data flow computing paradigm and moves quickly to writing some basic RxCpp-based programs. You will learn about the set of operators supported by the RxCpp library.

Chapter 8,  RxCpp – the Key Elements, gives you an understanding of how pieces of the Rx programming fit together in the context of Rx programming model in general and RxCpp library in particular. The topics covered in detail are Observables, Observer, Operators, Subscribers, Schedulers (five key elements of the Rx programming model).

Chapter 9, Reactive GUI Programming Using Qt/C++, deals with the topic of reactive GUI programming using the Qt framework. You will learn about concepts in the Qt framework, such as Qt object hierarchy, meta-object system, signals, and slots. Finally, you will write an application to handle mouse events and filter them in a reactive way using the RxCpp library.

Chapter 10, Creating Custom Operators in RxCpp, covers the advanced topic of how we can  create custom reactive operators in RxCpp, should an existing set of Operators not suffice for the purpose. We cover how to leverage Lift Meta Operator and adding Operators to the RxCpp library. This topic also helps you create Composite Operators by composing existing Operators.

 

 

Chapter 11, Design Patterns and Idioms for C++ Rx Programming,delves into the wonderful world of design patterns and idioms. Starting with GOF design patterns, we will move on to reactive programming patterns. We will cover Composite/Visitor/Iterator (from GoF catalogue), Active Object, Cell, Resource Loan, and the Event Bus Pattern.

Chapter 12Reactive Microservices Using C++, covers how the Rx programming model can be used to write reactive microservices using C++. It introduces you to the Microsoft C++ REST SDK and its programming model. You will learn how to leverage the RxCpp library to write aggregate services and access HTTP based services in a reactive manner.

Chapter 13, Advanced Streams and Handling Errors, discusses error handling in RxCpp, along with some of the advanced constructs and Operators that handle Streams in the RxCpp library. We will discuss how to continue Streams when an error comes, how to wait for the producer of the Stream to correct the error and continue the sequence, and how to perform common operations that are applicable to both success and error paths.

 

To get the most out of this book

In order to follow the topics in this book, you need to have knowledge of C++ programming. All other topics have been covered in this self-contained book. Of course, one need to search the web or read additional material to have an expert level understanding on some of the topics (which is true for any subject).

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

 

 

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/CPP-Reactive-Programming. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/CPPReactiveProgramming_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "The preceding code snippet initializes a structure by the name of WNDCLASS (or WNDCLASSEX for modern systems) with a necessary template for a window."

A block of code is set as follows:

/* close connection to server */
XCloseDisplay(display);

return 0;
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

/* close connection to server */
XCloseDisplay(display);

return 0;
}

Any command-line input or output is written as follows:

$ mkdir css
$ cd css

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "In Windowing parlance, it is called a message loop."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.