UID.h 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /******************************************************************************/
  2. struct UID // 128-bit Unique Identifier
  3. {
  4. union
  5. {
  6. struct{Byte b[16];};
  7. struct{UInt i[ 4];};
  8. struct{ULong l[ 2];};
  9. };
  10. Bool valid ()C {return l[0] || l[1] ; } // if the identifier is valid, not a zero
  11. UID& zero () { l[0] = l[1] = 0; return T;} // zero the identifier
  12. UID& randomize (); // randomize the identifier
  13. UID& randomizeValid(); // randomize the identifier and make sure that it's valid, not a zero
  14. UID& set(UInt a, UInt b, UInt c, UInt d) {i[0]=a; i[1]=b; i[2]=c; i[3]=d; return T;} // set identifier from custom values
  15. UID& set(ULong a, ULong b ) {l[0]=a; l[1]=b; return T;} // set identifier from custom values
  16. Str asHex( )C; // return in hexadecimal format of 32 character length, for example "F2FD9C88E4E754AA4632C6D884D67DE8"
  17. Bool fromHex(C Str &text) ; // set from hexadecimal format of 32 character length, for example "F2FD9C88E4E754AA4632C6D884D67DE8", sets to zero and returns false on fail (if 'text' is of invalid format), 'text' must contain exactly 32 hexadecimal characters (which can be a digit "0123456789", lower case character "abcdef" or upper case character "ABCDEF")
  18. Str asCanonical( )C; // return in canonical string format, for example "00000000-0000-0000-0000-000000000000"
  19. Bool fromCanonical(C Str &text) ; // set from canonical string format, for example "00000000-0000-0000-0000-000000000000", sets to zero and returns false on fail (if 'text' is of invalid format)
  20. Str asCString( )C; // return in C string format, for example "UID(534740238, 76182471, 691827931, 239847293)"
  21. Bool fromCString(C Str &text) ; // set from C string format, for example "UID(534740238, 76182471, 691827931, 239847293)", sets to zero and returns false on fail (if 'text' is of invalid format)
  22. Str asFileName( )C; // return in file name format of 24 character length, for example "h3kv^1fvcwe4ri0#ll#761m7"
  23. Bool fromFileName(C Str &text) ; // set from file name format of 24 character length, for example "h3kv^1fvcwe4ri0#ll#761m7", sets to zero and returns false on fail (if 'text' is of invalid format)
  24. Bool fromText(C Str &text); // set from text, sets to zero and returns false on fail, this function will try to decode the ID from different kinds of formats (hex, C string, DecodeFileName)
  25. UID& operator++() {T+=1u; return T;} void operator++(int) {T+=1u;} // use unsigned because those methods are faster
  26. UID& operator--() {T-=1u; return T;} void operator--(int) {T-=1u;} // use unsigned because those methods are faster
  27. UID& operator+=(Int i); UID& operator+=(UInt i);
  28. UID& operator-=(Int i); UID& operator-=(UInt i);
  29. friend UID operator+(C UID &uid, Int i) {return UID(uid)+=i;}
  30. friend UID operator-(C UID &uid, Int i) {return UID(uid)-=i;}
  31. Bool operator==(C UID &uid)C {return l[0]==uid.l[0] && l[1]==uid.l[1];} // if identifiers are equal
  32. Bool operator!=(C UID &uid)C {return l[0]!=uid.l[0] || l[1]!=uid.l[1];} // if identifiers are different
  33. #if EE_PRIVATE
  34. #if WINDOWS
  35. GUID& guid() {ASSERT(SIZE(T)==SIZE(GUID)); return (GUID&)T;}
  36. C GUID& guid()C {ASSERT(SIZE(T)==SIZE(GUID)); return (GUID&)T;}
  37. #endif
  38. #endif
  39. UID() {}
  40. UID(UInt a, UInt b, UInt c, UInt d) {set(a, b, c, d);}
  41. UID(ULong a, ULong b ) {set(a, b );}
  42. };extern
  43. const UID UIDZero; // constant UID with zero values
  44. /******************************************************************************/
  45. struct IDGenerator // this class manages generating unique UInt ID's (starting from 0 and increasing by 1), an ID can be returned to the generator when it is no longer used, allowing the generator to reuse it in the future when requesting a new ID
  46. {
  47. UInt New ( ); // create a new ID
  48. void Return(UInt id); // return an unused ID so it can be re-used later (returning an invalid ID will result in incorrect behavior of this class, an invalid ID is an ID that was not generated by this class or an ID that is already returned)
  49. ~IDGenerator();
  50. IDGenerator();
  51. private:
  52. UInt _created;
  53. Memc<UInt> _returned;
  54. };
  55. /******************************************************************************/
  56. inline Int Compare(C UID &a, C UID &b)
  57. {
  58. if(a.l[1]<b.l[1])return -1; if(a.l[1]>b.l[1])return +1;
  59. if(a.l[0]<b.l[0])return -1; if(a.l[0]>b.l[0])return +1;
  60. return 0;
  61. }
  62. /******************************************************************************/