uuid.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. namespace Torque
  28. {
  29. /// A universally unique identifier.
  30. class UUID
  31. {
  32. public:
  33. typedef void Parent;
  34. protected:
  35. U32 a;
  36. U16 b;
  37. U16 c;
  38. U8 d;
  39. U8 e;
  40. U8 f[ 6 ];
  41. static UUID smNull;
  42. public:
  43. UUID()
  44. {
  45. dMemset( this, 0, sizeof( UUID ) );
  46. }
  47. ///
  48. bool isNull() const { return ( *this == smNull ); }
  49. /// Generate a new universally unique identifier (UUID).
  50. void generate();
  51. /// Convert the given universally unique identifier to a printed string
  52. /// representation.
  53. String toString() const;
  54. /// Parse a text string generated by UUIDToString back into a
  55. /// universally unique identifier (UUID).
  56. bool fromString( const char* str );
  57. /// Return a hash value for this UUID.
  58. U32 getHash() const;
  59. bool operator ==( const UUID& uuid ) const
  60. {
  61. return ( dMemcmp( this, &uuid, sizeof( UUID ) ) == 0 );
  62. }
  63. bool operator !=( const UUID& uuid ) const
  64. {
  65. return !( *this == uuid );
  66. }
  67. };
  68. }
  69. namespace DictHash
  70. {
  71. inline U32 hash( const Torque::UUID& uuid )
  72. {
  73. return uuid.getHash();
  74. }
  75. }
  76. #endif // !_TORQUE_UUID_H_