C++ For Embedded Developers (2023-04-18 - 2023-04-20)¶
Goal¶
Give an idea how an experienced C programmer can benefit from the C++ toolcase. The following aspects are covered, accompanied with many exercises.
C++ is an object oriented language. That term alone has many facets, and a solid understanding of each is given. Constructors, destructors, automatic pointer/base-class conversion, late binding (“abstract”?), and all that.
C++ also brings a rich toolset in form of its standard library. After a short conceptual introduction, containers and algorithms are something that is immediately useful for non-OO programmers.
C++ does not stop there; starting from its 2011 definition, the language has undergone a major revolution. An overview is given.
Work Environment¶
Preferred: Ubuntu under Windows (WSL)¶
I suggest you use the Windows Subsystem For Linux (WSL2) (Microsoft documentation here). This sets up a virtualized Ubuntu inside Windows, together with all interoperability wazoo.
When installed, open a Ubuntu terminal, and install the software that is required for this training.
$ sudo apt update
$ sudo apt install git build-essential cmake libgtest-dev libgmock-dev
Skeleton Project Setup¶
Follow the instructions in the course project README to setup the initial version of the course project.
Day 1¶
Introduction¶
-
Outcome, including a quit-flag for the threads: intro.cpp
Some Details¶
A little
constexpr
: constexpr.cppFrom Lambda
ChatGPT bashing (“How would you compute the squares of a list of numbers with a lambda, like you would in Python?”): lambda-transform.cpp
More about lambda: lambda.cpp
A little threading (more: Multithreading)
And Python’s
asyncio
? (maybe watch the video in Pointless Blinking 🤘)
Standard Template Library¶
OO¶
From Data Encapsulation
Day 2¶
Warm-Up¶
std::atomic<>
. Revisit Ludwig’s ChatGPT findings, and introduce a signal handler in intro.cpp.Memory visibility across multiple CPUs?
Cache invalidation?
Memory model?
A little threading (from Multithreading)
Load-modify-store conflict: race-condition.cpp
Fixed with mutex (and a nice little class encapsulating all that): race-condition-fixed-mutex.cpp
Fixed with
std::atomic<>
: race-condition-fixed-atomic.cpp
Sensor Exercise, Resolved¶
Implement (live-hacked) test-sensor.cpp
Discussing OO issues: OO Design Principles
OO, Cont’d¶
Exercise: Fix Issues In Project
Smart Pointers¶
With this, fix memleak
Day 3¶
Smart Pointers (Continued)¶
Morphed our “registry” universe to use those …
Heavy Weight OO 💪¶
Complete the pointless point
class, by quickly moving across the
following topics from
Data Encapsulation
From Inheritance And Object Oriented Design
See live-hacked code in interfaces.cpp.
Exception Handling¶
From Exceptions:
Exercises: Containers And Algorithms¶
Exercises: OO And Interfaces¶
Exercise: Sensor Interface (polymorphic use of different sensors)
Exercise: OneWire Sensor Class (OneWire sensor - in the Linux OneWire style)
Exercise: OneWire Sensor Factory (factory pattern - a class that creates OneWire sensors by address)