Never Nester, Goto, RAII, and Defer

Coding when you are using an OS or library that requires a series of calls that build on each other to get the final result requires a set of checks to make sure each stage is successful. If some of the stages also require it partner call to free/close/destroy the items, then the programming logic requires you to be careful in how you tear down the items if there is an error in the middle of the process.

This can be coded several ways, and this post discusses a series of approaches in C++. The code selected for the example is performing Windows OS calls. These are done at the lowest API level without any other library or framework. The code has three places where tear down is required. The flow is complex enough to stop a simple reorganisation of the code to perform the same task. There is no addition error management present so make it easier to read. It is a fully working program and it gets a list of USB devices that the program is allowed access to.


Reasons for Using C++ 17

Using the logic that we should use tools that are as simple as possible and only adopt complexity where it is absolutely necessary, it feels like I could use a very early version of C++. What follows are the reasons why I have increased that to C++17. The idea will be to only use these feature and no more. Any new use will be add to this page.

#if !((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
    #error Requires C++17 or higher

Screen Find – Seeing the Wood in the Trees

I hope you have a nice big screen – or several of them. Nice huh?

Well all this real estate comes with a price. Some times the screens can hold so much information it is hard to find what you are looking for. Would it not be nice to be able to search for the word you are looking for in a huge list of dense text?

Sure almost every app has search, but can you search the dialogue boxes, the drop down menus, the image of a poster?

So why not have a system wide special key combo that triggered a find dialogue box. Type in ‘comment’ and the screens are captured, OCR’ed and then if the text is found an overlay highlight shows you where the word is.


Animating Alberto Garcia-Alvarez’s Phenakistoscopes using Magic

Tim Melville Gallery had an exhibition of Alberto Garcia-Alvarez’s work. I love the strong and bold colours. I guess with circles and a subject of ‘Time’ it was hard for me not to be enamoured. I explained to Tim that it would be easy to take the static images of the Phenakistoscopes and turn them into a movie to show what they really looked like if they were being used as intended. He said for me to try it and this story is the outcome.

With a little messing about, I managed this with the minimum of tools. Just a CMD batch file to loop through the commands, ImageMagick commands, and Gimp to find the centre and size of the circles.


Configuring WordPress

I wanted to use one of the default blogging themes, so picked a simple one from WordPress themselves. This is Twenty Seventeen.

However a few changes were needed to get it working for posts with code in it.

The main single post width needed increasing. This does not help on the mobile media, but it helps a lot when viewing wide code snippets on a full size browser window.


Visual Studio C++ IDE settings

Or makefiles to anyone else…

This is just to make sure I can reproduce the exact builds when using Visual Studio’s IDE. The settings tend to be in dialogue boxes that require manual changes. These instructions show where to find the key changes.

Two main sections. There is the IDE settings themselves and also the projects Configuration Properties. Note that debugging is in with the IDE and not with the compiler configuration.

Further down will be a discussion on how to update Visual Studio directly via its XML files.


C++ Coding Standards

The following is not intended to generate a holy war, but merely a place for me to remind myself of the decisions I took along the way to get to a consistent style for any new code that I write.

#pragma warning( push, 3 )
#include <stdio.h>
#pragma warning( pop )

// /Wall Used. Compiler warnings turned off.
#pragma warning (disable :  5045) // Spectre code insertion warning.

class entity
    static constexpr size_t     _maxEntities = 1000;
    entity(char* name)
        _name = name;
    void set()
    char    _name[8];

inline float _floor(const float& a)
    if (a == 0.0f) return 0.0f;
    return floorf(a);

C Strings in C++ and Deep Copies

I was told on Stack Overflow:

“A pointer is just a pointer.”

And that is true of course. To go beyond that we have structures and classes which have explicit construction and operators. However, C++ has inherited C strings which are just a pointer to the first char, but they also have a long history where the construction and operations are well known. So, when a C String is given to a map or vector, it may be thought that it could be doing a deep copy. After all, it is known (by implicit rules) how this could be completed. The size is defined. It is just not stored. Also, the storage is clear – just not managed.


Buying a New Keyboard – The Mechanical Renascence

So you have an old Microsoft Wireless Desktop 3000 Keyboard and Mouse. You get used to it and it becomes your ‘personal standard’ for keyboards. It has full travel keypress and all the keys laid out the way you want them – the large Enter Key and the Media Mute key. But then it gets old and breaks and they don’t make that version anymore, so you look for the closest replacement. In this case the cheaper Microsoft Wireless Keyboard 2000. It’s OK and you put up with it, but the mouse starts to break as well. What to do…

Microsoft Wireless Desktop 3000 Reviews | TechSpot
Microsoft Wireless Desktop 3000 Keyboard (with the full size UK Enter Key)