Crascit

Caffeine-powered software

Page 2 of 3

OnLeavingScope: The sequel

In a previous article, the OnLeavingScope class was presented as a technique for robustly and concisely handling scenarios involving multi-step setup, run and cleanup stages. It focused on ease of learning and wide applicability rather than performance, so while the implementation was complete, it was not necessarily optimal. This article picks up where the previous article left off and deals with some of the more advanced aspects to provide some improvements.

Read More

Let your compiler do your housekeeping

A common sequence of steps we mortal software developers frequently find ourselves implementing goes something like this:

  1. Perform some sort of setup or acquire some sort of resource.
  2. Carry out some arbitrary sequence of actions.
  3. Tear down things we setup or release resources we acquired in step 1.

There are well-known patterns for implementing this scenario robustly, but when there are multiple sub-steps to be performed in the setup phase and where any of those sub-steps can each fail individually, things get more complicated. This article presents a concise, self-documenting and robust way to handle these more complicated cases. A follow-up article will extend this further to improve some performance characteristics and ends up having a lot in common with the ScopeGuard11 pattern described in various places online.

The multi-step setup problem

Conceptually, the problem we want to solve can be described as follows:

  1. For each setup sub-step:
    • Perform sub-step.
    • If sub-step fails, stop and release/clean up after all previous setup sub-steps.
  2. Carry out some arbitrary sequence of actions.
  3. Tear down things we setup or release resources we acquired in all sub-steps of step 1.

    Read More

Handling binary assets and test data with CMake

A common situation facing many projects is how to incorporate large binary assets into the main source code and its build process. Examples of such assets include firmware binaries for embedded products, videos, user manuals, test data and so on. These binary assets often have their own workflow for managing source materials, change history and building the binaries. This article demonstrates an approach to handling this situation with CMake builds.

Read More

Enabling C++11 And Later In CMake

With the recent evolution of C++, build systems have had to deal with the complication of selecting the relevant compiler and linker flags. If your project targets multiple platforms and compilers, this can be a headache to set up. Happily, with features added in CMake 3.1, it is trivial to handle this in a generic way.

Read More

Practical uses for variadic templates

You might not have even heard of parameter packs or variadic templates before, but if you’ve worked with C++ templates for a while, chances are at some point you’ve wanted the functionality they provide. This post provides a few practical examples showing some of the problems they solve and techniques they enable.

Read More

Lambdas for lunch

Alright, so lambdas in C++ are cool and we’ve been coding with one arm tied behind our back all this time. C++11 brought us this wonderful goodness, which is great, but just how do you actually use them? No messing around, let’s jump right in!

Read More

CMake targets with detailed dependencies

If only software build systems would do what we intuitively expect! I’m sure many of you have your own horror stories of having to unravel convoluted scripts, project settings, compiler bugs, etc. in order to get code to build, despite the project requirements seeming to be relatively simple. If you work with cross-platform software, this is probably a pain point you are particularly familiar with. This article demonstrates some more recent features of CMake which greatly reduce that pain.

Read More

C++ compiler status

With my recent posts about C++11 features, it may be useful to pause and ask whether the compiler you are using supports the features you need. Kudos to Christophe for doing a great job of summarising that info in a very concise document, complete with links to details about each compiler feature!

Iterating over an enum

The enum feature of C/C++ is far from new. It is very useful for defining a specific set of values that a simple integer may take, which can lead to clearer, more concise code when used appropriately. Many compilers are capable of warning about common errors associated with enum use, such as not including case statements for all possible enum values in a switch statement that has no default clause. In many respects, an enum acts like a set, but being essentially just a glorified int, it lacks any of the container features of something like std::set.

The developer typically faces a tradeoff between performance and functionality when deciding between an enum or some kind of set-like container. There are some (often common) situations, however, where an enum can still be treated like a container, thanks to features made available in C++11.

Read More

C++ list initialization

Containers

Consider this very typical piece of C/C++ code for creating an array of integers from a list of specified values (called a braced initializer):

const int[] someInts = {23, 10, 5, 17};

This is simple and works well where a plain int array is needed, but sometimes we want to do something similar with STL containers.

Read More

Page 2 of 3

Powered by WordPress & Theme by Anders Norén