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
anduuid4
folders. The rest is about the previous post that usesdbj::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
1 2 3 4 5 |
// compile time part using namespace dbj::literals; // compile time guid's are made from one user defined literal (UDL) constexpr dbj::GUID guid_1 = "{FE297330-BAA5-407F-BB47-F78752D2C209}"_guid; constexpr dbj::GUID guid_2 = "{FE297330-BAA5-407F-BB47-F78752D2C209}"_guid; |
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.
1 2 3 |
// Example // compare dbj::GUID at compile time static_assert(guid_1 == guid_2); |
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.
1 2 |
// we do provide compile time is_null() static_assert(!is_null(guid_1)); |
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.
1 2 |
// this is portable function dbj::GUID guid_3 = dbj_runtime_guid(); |
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.