From f4f2fc7c3d76eb3dc5a91c5eefb36c10597d6cb7 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Fri, 25 Aug 2017 13:43:18 +0100 Subject: Add cUUID class (#3871) --- src/UUID.h | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/UUID.h (limited to 'src/UUID.h') diff --git a/src/UUID.h b/src/UUID.h new file mode 100644 index 000000000..12fef3e39 --- /dev/null +++ b/src/UUID.h @@ -0,0 +1,100 @@ +// UUID.h + +// Declares the cUUID class representing a Universally Unique Identifier + +#pragma once + + +// tolua_begin + +class cUUID +{ +public: + /** Default constructed "nil" UUID */ + cUUID(): + m_UUID() + { + } + + /** Lexicographically compare bytes with another UUID. + Returns: + 0 when equal to a_Other, + < 0 when less than a_Other, + > 0 when greater than a_Other */ + int Compare(const cUUID & a_Other) const + { + return std::memcmp(m_UUID.data(), a_Other.m_UUID.data(), m_UUID.size()); + } + + /** Returns true if this contains the "nil" UUID with all bits set to 0 */ + bool IsNil() const + { + return (m_UUID == std::array{{0}}); + } + + /** Tries to interpret the string as a short or long form UUID and assign from it. + On error, returns false and does not set the value. */ + bool FromString(const AString & a_StringUUID); + + /** Converts the UUID to a short form string (i.e without dashes). */ + AString ToShortString() const; + + /** Converts the UUID to a long form string (i.e. with dashes). */ + AString ToLongString() const; + + /** Returns the version number of the UUID. */ + UInt8 Version() const; + + /** Returns the variant number of the UUID. */ + UInt8 Variant() const; + + /** Generates a version 3, variant 1 UUID based on the md5 hash of a_Name. */ + static cUUID GenerateVersion3(const AString & a_Name); + + // tolua_end + + /** Converts UUID to raw memory representation, respecting UUID variant. */ + std::array ToRaw() const; + + /** Assigns from raw memory representation, respecting UUID variant. */ + void FromRaw(const std::array & a_Raw); + +private: + /** Binary UUID stored big-endian. */ + std::array m_UUID; +}; // tolua_export + + +// Comparison operators: + +inline bool operator == (const cUUID & a_Lhs, const cUUID & a_Rhs) +{ + return (a_Lhs.Compare(a_Rhs) == 0); +} + +inline bool operator != (const cUUID & a_Lhs, const cUUID & a_Rhs) +{ + return (a_Lhs.Compare(a_Rhs) != 0); +} + +inline bool operator < (const cUUID & a_Lhs, const cUUID & a_Rhs) +{ + return (a_Lhs.Compare(a_Rhs) < 0); +} + +inline bool operator <= (const cUUID & a_Lhs, const cUUID & a_Rhs) +{ + return (a_Lhs.Compare(a_Rhs) <= 0); +} + +inline bool operator > (const cUUID & a_Lhs, const cUUID & a_Rhs) +{ + return (a_Lhs.Compare(a_Rhs) > 0); +} + +inline bool operator >= (const cUUID & a_Lhs, const cUUID & a_Rhs) +{ + return (a_Lhs.Compare(a_Rhs) >= 0); +} + + -- cgit v1.2.3