| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #pragma once |
| 4 | |
| 5 | #include "Prerequisites/BsPlatformDefines.h" |
| 6 | #include "String/BsString.h" |
| 7 | #include "Prerequisites/BsTypes.h" |
| 8 | #include "Prerequisites/BsRTTIPrerequisites.h" |
| 9 | #include "Utility/BsUtil.h" |
| 10 | |
| 11 | namespace bs |
| 12 | { |
| 13 | /** @addtogroup Utility-Core |
| 14 | * @{ |
| 15 | */ |
| 16 | |
| 17 | /** Represents a universally unique identifier. */ |
| 18 | struct BS_UTILITY_EXPORT UUID |
| 19 | { |
| 20 | /** Initializes an empty UUID. */ |
| 21 | constexpr UUID() = default; |
| 22 | |
| 23 | /** Initializes an UUID using framework's UUID representation. */ |
| 24 | constexpr UUID(UINT32 data1, UINT32 data2, UINT32 data3, UINT32 data4) |
| 25 | : mData{data1, data2, data3, data4} |
| 26 | { } |
| 27 | |
| 28 | /** Initializes an UUID using its string representation. */ |
| 29 | explicit UUID(const String& uuid); |
| 30 | |
| 31 | constexpr bool operator==(const UUID& rhs) const |
| 32 | { |
| 33 | return mData[0] == rhs.mData[0] && mData[1] == rhs.mData[1] && mData[2] == rhs.mData[2] && mData[3] == rhs.mData[3]; |
| 34 | } |
| 35 | |
| 36 | constexpr bool operator!=(const UUID& rhs) const |
| 37 | { |
| 38 | return !(*this == rhs); |
| 39 | } |
| 40 | |
| 41 | constexpr bool operator<(const UUID& rhs) const |
| 42 | { |
| 43 | for(UINT32 i = 0; i < 4; i++) |
| 44 | { |
| 45 | if (mData[i] < rhs.mData[i]) |
| 46 | return true; |
| 47 | else if (mData[i] > rhs.mData[i]) |
| 48 | return false; |
| 49 | |
| 50 | // Move to next element if equal |
| 51 | } |
| 52 | |
| 53 | // They're equal |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | /** Checks has the UUID been initialized to a valid value. */ |
| 58 | constexpr bool empty() const |
| 59 | { |
| 60 | return mData[0] == 0 && mData[1] == 0 && mData[2] == 0 && mData[3] == 0; |
| 61 | } |
| 62 | |
| 63 | /** Converts the UUID into its string representation. */ |
| 64 | String toString() const; |
| 65 | |
| 66 | static UUID EMPTY; |
| 67 | private: |
| 68 | friend struct std::hash<UUID>; |
| 69 | |
| 70 | UINT32 mData[4] = {0, 0, 0, 0}; |
| 71 | }; |
| 72 | |
| 73 | BS_ALLOW_MEMCPY_SERIALIZATION(UUID) |
| 74 | |
| 75 | /** |
| 76 | * Utility class for generating universally unique identifiers. |
| 77 | * |
| 78 | * @note Thread safe. |
| 79 | */ |
| 80 | class BS_UTILITY_EXPORT UUIDGenerator |
| 81 | { |
| 82 | public: |
| 83 | /** Generate a new random universally unique identifier. */ |
| 84 | static UUID generateRandom(); |
| 85 | }; |
| 86 | |
| 87 | /** @} */ |
| 88 | } |
| 89 | |
| 90 | /** @cond STDLIB */ |
| 91 | /** @addtogroup Utility |
| 92 | * @{ |
| 93 | */ |
| 94 | |
| 95 | namespace std |
| 96 | { |
| 97 | /** Hash value generator for UUID. */ |
| 98 | template<> |
| 99 | struct hash<bs::UUID> |
| 100 | { |
| 101 | size_t operator()(const bs::UUID& value) const |
| 102 | { |
| 103 | size_t hash = 0; |
| 104 | bs::bs_hash_combine(hash, value.mData[0]); |
| 105 | bs::bs_hash_combine(hash, value.mData[1]); |
| 106 | bs::bs_hash_combine(hash, value.mData[2]); |
| 107 | bs::bs_hash_combine(hash, value.mData[3]); |
| 108 | |
| 109 | return hash; |
| 110 | } |
| 111 | }; |
| 112 | } |
| 113 | |
| 114 | /** @} */ |
| 115 | /** @endcond */ |
| 116 | |