uuid.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _TORQUE_UUID_H_
  23. #define _TORQUE_UUID_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #include "console/engineTypeInfo.h"
  28. typedef unsigned int unsigned32;
  29. typedef unsigned short unsigned16;
  30. typedef unsigned char unsigned8;
  31. class xuuid_t
  32. {
  33. public:
  34. unsigned32 time_low;
  35. unsigned16 time_mid;
  36. unsigned16 time_hi_and_version;
  37. unsigned8 clock_seq_hi_and_reserved;
  38. unsigned8 clock_seq_low;
  39. unsigned8 node[6];
  40. };
  41. namespace Torque
  42. {
  43. /// A universally unique identifier.
  44. class UUID : public xuuid_t
  45. {
  46. friend class UUIDEngineExport;
  47. public:
  48. typedef void Parent;
  49. protected:
  50. U32 a;
  51. U16 b;
  52. U16 c;
  53. U8 d;
  54. U8 e;
  55. U8 f[ 6 ];
  56. static UUID smNull;
  57. public:
  58. UUID()
  59. {
  60. a = b = c = d = e = 0;
  61. for (U32 i = 0; i < 6; i++)
  62. {
  63. f [i]=0;
  64. }
  65. }
  66. ///
  67. bool isNull() const { return ( *this == smNull ); }
  68. /// Generate a new universally unique identifier (UUID).
  69. void generate();
  70. /// Convert the given universally unique identifier to a printed string
  71. /// representation.
  72. String toString() const;
  73. /// Parse a text string generated by UUIDToString back into a
  74. /// universally unique identifier (UUID).
  75. bool fromString( const char* str );
  76. /// Return a hash value for this UUID.
  77. U32 getHash() const;
  78. bool operator ==( const UUID& uuid ) const
  79. {
  80. return ( dMemcmp( this, &uuid, sizeof( UUID ) ) == 0 );
  81. }
  82. bool operator !=( const UUID& uuid ) const
  83. {
  84. return !( *this == uuid );
  85. }
  86. };
  87. class UUIDEngineExport
  88. {
  89. public:
  90. static EngineFieldTable::Field getAField();
  91. static EngineFieldTable::Field getBField();
  92. static EngineFieldTable::Field getCField();
  93. static EngineFieldTable::Field getDField();
  94. static EngineFieldTable::Field getEField();
  95. static EngineFieldTable::Field getFField();
  96. };
  97. }
  98. namespace DictHash
  99. {
  100. inline U32 hash( const Torque::UUID& uuid )
  101. {
  102. return uuid.getHash();
  103. }
  104. }
  105. #endif // !_TORQUE_UUID_H_