summaryrefslogtreecommitdiffstats
path: root/src/UUID.h
blob: bec4611825e9339f28bb5ab05f7f97f000b2780d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
	{
		// tolua_end
		return (m_UUID == std::array<Byte, 16> {{0}});
		// tolua_begin
	}

	/** 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<Byte, 16> ToRaw() const;

	/** Assigns from raw memory representation, respecting UUID variant. */
	void FromRaw(const std::array<Byte, 16> & a_Raw);

  private:
	/** Binary UUID stored big-endian. */
	std::array<Byte, 16> 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);
}