C++ Reactive Programming
Design concurrent and asynchronous applications using the RxCpp library and Modern C++17
Praseed Pai, Peter Abraham
- 328 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
C++ Reactive Programming
Design concurrent and asynchronous applications using the RxCpp library and Modern C++17
Praseed Pai, Peter Abraham
About This Book
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.
Frequently asked questions
Information
A Tour of Modern C++ and its Key Idioms
- Key concerns for C++ programming language design
- Some enhancements to C++ for writing better code
- Better memory management through rvalue references and move semantics
- Better object lifetime management using an enhanced set of smart pointers
- Behavioral parameterization using Lambda functions and expressions
- Function Wrappers (the std::function type)
- Miscellaneous features
- Writing Iterators and Observers (to put everything together)
The key concerns of the C++ programming language
- Zero Cost Abstraction - No performance penalty for higher level abstraction
- Expressivity - A user defined type (UDT) or class should be as expressive as built-in types
- Substitutability - A UDT can be substituted wherever built-in-types are expected (as in generic data structures and algorithms)
Zero cost abstraction
Expressivity
//---- SmartFloat.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class SmartFloat {
double _value; // underlying store
public:
SmartFloat(double value) : _value(value) {}
SmartFloat() : _value(0) {}
SmartFloat( const SmartFloat& other ) { _value = other._value; }
SmartFloat& operator = ( const SmartFloat& other ) {
if ( this != &other ) { _value = other._value;}
return *this;
}
SmartFloat& operator = (double value )
{ _value = value; return *this;}
~SmartFloat(){ }
SmartFloat& operator ++ () { _value++; return *this; }
SmartFloat operator ++ (int) { // postfix operator
SmartFloat nu(*this); ++_value; return nu;
}
SmartFloat& operator -- () { _value--; return *this; }
SmartFloat operator -- (int) {
SmartFloat nu(*this); --_value; return nu;
}
SmartFloat& operator += ( double x ) { _value += x; return *this;}
SmartFloat& operator -= ( double x ) { _value -= x;return *this; }
SmartFloat& operator *= ( double x ) { _value *= x; return *this;}
SmartFloat& operator /= ( double x ) { _value /= x; return *this;}
bool operator > ( const SmartFloat& other )
{ return _value > other._value; }
bool operator < ( const SmartFloat& other )
{return _value < other._value;}
bool operator == ( const SmartFloat& other )
{ return _value == other._value;}
bool operator != ( const SmartFloat& other )
{ return _value != other._value;}
bool operator >= ( const SmartFloat& other )
{ return _value >= other._value;}
bool operator <= ( const SmartFloat& other )
{ return _value <= other._value;}
operator int () { return _value; }
operator double () { return _value;}
};
double Accumulate( double a[] , int count ){
double value = 0;
for( int i=0; i<count; ++i) { value += a[i]; }
return value;
}
double Accumulate( SmartFloat a[] , int count ){
SmartFloat value = 0;
for( int i=0; i<count; ++i) { value += a[i]; }
return value;
}
int main() {
// using C++ 1z's initializer list
double x[] = { 10.0,20.0,30,40 };
SmartFloat y[] = { 10,20.0,30,40 };
double res = Accumulate(x,4); // will call the double version
cout << res << endl;
res = Accumulate(y,4); // will call the SmartFloat version
cout << res << endl;
}
Substitutability
//------------- from SmartValue.cpp
template <class T>
T Accumulate( T a[] , int count ) {
T value = 0;
for( int i=0; i<count; ++i) { value += a[i]; }
return value;
}
int main(){
//----- Templated version of SmartFloat
SmartValue<double> y[] = { 10,20.0,30,40 };
double res = Accumulate(y,4);
cout << res << endl;
}