A Recursive Variant Type in C++

Because one of my goals is to rely on as few external libraries as possible, I am not using Boost. As a result, there comes times when I need to roll my own, extremely useful, library-like code that isn’t a part of the C++ standard yet.

One of these, is the Variant. A tagged union / discriminated union / sum type, that allows you to store multiple data types in one section of memory. A sort of type-erasure gimmick.

I need a type like his for two reasons so far. The first is that my scripting language, space script, is quasi-type safe. You define variables using your usual data types, but any variable type can be cast to any other type.

I did it this way mostly because I was curious to see how a language like that would behave. Probably not the best idea.

I’ve also allowed my Variant type to be recursive. This means that it can contain members of itself. I’ve done this specifically because my data format, space format, is JSON-esque, and basically every node can contain any number of numbers, strings and other objects etc. That’s the second reason I need a variant type.

This took me quite a bit of screwing around to get right. A lot of the code for the base Variant was taken from someone else’s code that I found online. This code was fundamentally broken though; so I’ve fixed it up and allowed for recursion.

I’ll probably do a write up on this at some point. I like it over the boost::variant primarily because I don’t want a never-empty guarantee. I can’t imagine why I’d want a type that doesn’t allow for null-states.

EDIT: Fixed up some of that code.

EDIT 20/05/2016: Fixed up some more of that code.

Coupled the scripting engine with the UI engine

I’ve now managed to get the scripting engine working with the UI engine. This means that the UI can now be completely defined outside of the game-code, in run-time language files. Should result in quick changes to the game UI when it comes to it; and even modding etc.