C++ GUID for the rest of us

So little C++ so much good!

In case you want to know why is Universally unique identifier incredibly useful for you click on that link. UUID and GUID mean the same “thing”.

In case you need little resilient modern C++ compile-time  GUID read on.

Not patient? Not a problem. The repository is here. Code related to this post is dbj_guid and uuid4 folders. The rest is about the previous post that uses dbj::GUID too.

The usage first.  You will need in your project  dbj_guid.h and the code from the uuid4 folder. The code.

Compile-time

Obviously for compile-time usage, you will generate string GUID’s using one of the many tools available.  Note: WIN32 does not offer compile-time GUID API at all. We do.

As almost all dbj C++ code, we do not use exceptions. There are many reasons but they are outside the scope of this post. Instead of throwing an exception, we do return null_guid , after setting the errno to EINVAL and after printing to the stderr the reason for failure.

We provide a function to check on this.

Code inside is very simple (and resilient). You might perhaps elect to change it to exit on error. It depends on your project policy.

Run time

There is one factory function for run time dbj GUID creation.

That function is fully portable. For WIN32 aficionados  It offers pure WIN32 implementation without actually including rpc.h .  And for the rest of the world, it used the small but fully functional uuid4 lib.

It offers win and not-win OS functionality and generates completely standard GUID’s ib both cases. But in case your project manager does not trust what he does not understand, I have provided standard WIN32 code for you too. Jut peep in there.

Again, code is very simple and that makes it resilient. Go ahead use it, and possibly change to your liking. Just please respect the copyright.