BsUUID.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPlatformDefines.h"
  5. #include "String/BsString.h"
  6. #include "Prerequisites/BsTypes.h"
  7. #include "Prerequisites/BsRTTIPrerequisites.h"
  8. #include "Utility/BsUtil.h"
  9. namespace bs
  10. {
  11. /** @addtogroup Utility-Core
  12. * @{
  13. */
  14. /** Represents a universally unique identifier. */
  15. struct BS_UTILITY_EXPORT UUID
  16. {
  17. /** Initializes an empty UUID. */
  18. UUID()
  19. : mData()
  20. { }
  21. /** Initializes an UUID using Banshee's UUID representation. */
  22. UUID(UINT32 data1, UINT32 data2, UINT32 data3, UINT32 data4)
  23. {
  24. mData[0] = data1;
  25. mData[1] = data2;
  26. mData[2] = data3;
  27. mData[3] = data4;
  28. }
  29. /** Initializes an UUID using its string representation. */
  30. explicit UUID(const String& uuid);
  31. 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. bool operator!=(const UUID& rhs) const
  36. {
  37. return !(*this == rhs);
  38. }
  39. bool operator<(const UUID& rhs) const
  40. {
  41. for(UINT32 i = 0; i < 4; i++)
  42. {
  43. if (mData[i] < rhs.mData[i])
  44. return true;
  45. else if (mData[i] > rhs.mData[i])
  46. return false;
  47. // Move to next element if equal
  48. }
  49. // They're equal
  50. return false;
  51. }
  52. /** Checks has the UUID been initialized to a valid value. */
  53. bool empty() const
  54. {
  55. return mData[0] == 0 && mData[1] == 0 && mData[2] == 0 && mData[3] == 0;
  56. }
  57. /** Converts the UUID into its string representation. */
  58. String toString() const;
  59. static UUID EMPTY;
  60. private:
  61. friend struct std::hash<UUID>;
  62. UINT32 mData[4];
  63. };
  64. BS_ALLOW_MEMCPY_SERIALIZATION(UUID)
  65. /**
  66. * Utility class for generating universally unique identifiers.
  67. *
  68. * @note Thread safe.
  69. */
  70. class BS_UTILITY_EXPORT UUIDGenerator
  71. {
  72. public:
  73. /** Generate a new random universally unique identifier. */
  74. static UUID generateRandom();
  75. };
  76. /** @} */
  77. }
  78. /** @cond STDLIB */
  79. /** @addtogroup Utility
  80. * @{
  81. */
  82. namespace std
  83. {
  84. /** Hash value generator for UUID. */
  85. template<>
  86. struct hash<bs::UUID>
  87. {
  88. size_t operator()(const bs::UUID& value) const
  89. {
  90. size_t hash = 0;
  91. bs::hash_combine(hash, value.mData[0]);
  92. bs::hash_combine(hash, value.mData[1]);
  93. bs::hash_combine(hash, value.mData[2]);
  94. bs::hash_combine(hash, value.mData[3]);
  95. return hash;
  96. }
  97. };
  98. }
  99. /** @} */
  100. /** @endcond */