Thomas Thomas

How to Write a C++ Test Framework

A long time ago I had a question about how Googletest or Catch2 implement their macros to run all the unit tests they write. While writing the implementation for CWT-Cucumber: A C++ Cucumber Interpreter, I needed the same mechanism to achieve this particular behaviour. Registering functions before main is executed. In this article I write a proof of concept for a C++ test framework.

Read More
Thomas Thomas

[C++] A std::tuple To Concatenate Validation Criteria

I recently had this problem that I need to store multiple validation functions which returned a bool and I didn’t want to use std::vector or any other runtime container. Concatenating if statements weren’t a good solution either. Ultimately I came up with a solution where I used std::tuple in combination with std::apply

Read More
Thomas Thomas

[C++] User-Defined Literals To Handle Units

Create a good notation to convert units, like seconds or ms. I used C++11 User-Defined Literals and a specific time type to handle my time units. This approach can also be applied to other units, like meters. Now the code appears more clear because I have a specific notation to distinguish times and can also compare my values correctly without casting it on clients side.

Read More
Thomas Thomas

[C++] An Entity-Component-System From Scratch

ECS is the abbreviation for Entity Component System. Here we’ll create an ECS from scratch with a SDL example in C++. We create a window and we’ll be able to render characters to it. By adding components we are also able to move this character arounde by pressing A, S, D and W. So let’s get started with Entity Component Systems with a C++ example.

Read More
Thomas Thomas

[C++] CRTP For Mixin Classes

I had an article about CRTP and C++20 concepts for static polymorphic behaviour. This demonstrated that concepts can replace CRTP but this doesn’t mean that we don’t need CRTP anymore. In this article we create a strong type and extend it with different “Skills” by using CRTP.

Read More
Thomas Thomas

[C++] Combine Type Erasure And Strategies

In this example we combine type erasure with a strategy like pattern. We continue with a character type from my first article about type erasure and we’ll add a rendering method to them without modifying the concrete classes. This increases encapsulation on our example and demonstrates how powerful this combination could be.

Read More
Thomas Thomas

[C++] Use Type Erasure To Get Dynamic Polymorphic Behavior

This is a simple and short example to understand the idea of type erasure. In this article we create a simple class which can erase your concrete types and illustrates the general behavior and what it requres to have polymorphic behavior at runtime. For a more comprehensive understanding and include tests, I recommend watching Klaus Iglbergers talk about this topic from CppCon 2022.

Read More
Thomas Thomas

[Typescript/C++]: Coroutines: What are they and what we have in C++20

Coroutines are available with C++20. Unfortunately they aren’t ready to use like in other languages. We still need a generator type, which we implement and create a first, fairly simple coroutine. But before we do so, we take a look on Typescript to understand what coroutines are and how they are implemented there.

Read More
Thomas Thomas

[C++] A Boost Asio Server-Client Example

A simple server-client example in C++ with Boost Asio. Here I create a server which can accept multiple clients. For the demonstration purpose we send a simple string to the server which is then printed to stdout. This is a really simple example but it helps especially when you start with Boost Asio and networking.

Read More
Thomas Thomas

[C++] Static, Dynamic Polymorphism, CRTP and C++20’s Concepts

C++20 offers really nice features and a very great one is concepts. In this article I compare existing techniques to implement interfaces (dynamic polymorphism, CRTP) to a new approach by using concepts. With concepts we have a lot of advantages and it affects the current way we write code. It’s clearer and it’s better to understand.

Read More
Thomas Thomas

[C++] A C++17 Statemachine using std::tuple and std::variant

One of my favorite design patterns is the state machine pattern. I used to do this pattern with an abstract where then all concrete states inherit from. During runtime I was able to assign new states to a state machine, let’s do this in another way to get rid of dynamic allocation with C++17

Read More
Thomas Thomas

[C++] Tag Dispatching To Overload Functions

Sometimes we want to have different behaviour in templated functions according to a given type. Since we can’t overload templated functions with same arguments in C++, we can use tag dispatching. With tag dispatching we can call certain implementations depending on a given type.

Read More
Thomas Thomas

[C++] Typed Tests For Interfaces With Googletest

Typed tests with googletest are useful when we want to test interfaces. Typed testing avoids writing redundand tests for each implementation and guarantee that the all derived classes will run the same tests.

Read More