C++

[C++] A safer array size macro

Introduction Array size macros in C++ are typically ported from C, and so often end up looking something like this. #define ARRAY_SIZE_UNSAFE(A) (sizeof(A)/sizeof(*(A))) This will work, however it has no type safety, so you can pass it both an array or a pointer to an array. This post shows how templates can be used in C++ to create a safer array size macro (this is the same technique that _countof uses in MSVC).

[C++] An exercise in optimisation #1 - ImmutableString

Introduction This post outlines a problem I had to solve regarding using a string as a key for a hash table, and the steps that were taken to optimise the problem by applying the solution of an immutable string type. I may make this into a series if I can find interesting enough examples that I can suitably simplify the code for. The Problem I was creating a key -> value pair map for use with a C++ serialisation system; the keys had to be strings as they were used as identifiers for JSON, and around 99% of the time the key would be a string literal.

[C++] Bit-flags

Introduction Bit-flags are used in C and C++ to save memory by allowing you to pack multiple boolean variables into a single integer variable. While they are very good at this, the syntax for using them isn’t exactly the easiest to read, so I thought I’d share a trick we use at work to deal with bit-flags. The Code struct Flags8 { union { uint8_t allFlags; struct { bool flag1 : 1; bool flag2 : 1; bool flag3 : 1; bool flag4 : 1; bool flag5 : 1; bool flag6 : 1; bool flag7 : 1; bool flag8 : 1; }; }; }; The code above shows a struct containing a union between an 8-bit unsigned integer, and another struct containing a series of bit-wide booleans (that’s what the : 1 does).

Templates and Streams; the perfect couple

Recently I’ve been doing some work templatifying my two I/O classes, and from that have come to the conclusion that templates and streams make for the ultimate generic programming tool. Previously I had a load of old duplicate bloat code in these classes, that was all removed and replaced by one templated member function, and two specialised template functions from it, and best of all since this code is now able to take external streams as input/output sources, it means that I no longer have one code path for handing files, and another code path for serialising to memory.

[C++] Anything to/from a Hex String

Introduction I recently needed to be able to convert any instance of an object in C++ to a file so it could be serialised, and then restored later; I did most of this by writing out each member variable of the object individually at the most basic level. This works fine and is easy enough to implement; but then came the time to do the “generic” version, the version that could write out any object (such as a struct) as a whole.

[C++] Pointer Fun – SafeDelete

Introduction SafeDelete and SafeArrayDelete are functions that will delete the data a pointer points to before setting it to NULL. The idea being that they leave the pointer in a safe NULL state so that you don’t try and use a pointer which points to invalid memory. How to - With a Macro You can implement a SafeDelete set-up using a macro, it might well look something like this: