PageRenderTime 140ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/lluuid.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 331 lines | 203 code | 54 blank | 74 comment | 18 complexity | 1a66fb78109c235a5ae28de8a3dccb8d MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lluuid.h
  3. *
  4. * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  5. * Second Life Viewer Source Code
  6. * Copyright (C) 2010, Linden Research, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License only.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  23. * $/LicenseInfo$
  24. */
  25. #ifndef LL_LLUUID_H
  26. #define LL_LLUUID_H
  27. #include <iostream>
  28. #include <set>
  29. #include "stdtypes.h"
  30. #include "llpreprocessor.h"
  31. const S32 UUID_BYTES = 16;
  32. const S32 UUID_WORDS = 4;
  33. const S32 UUID_STR_LENGTH = 37; // actually wrong, should be 36 and use size below
  34. const S32 UUID_STR_SIZE = 37;
  35. const S32 UUID_BASE85_LENGTH = 21; // including the trailing NULL.
  36. struct uuid_time_t {
  37. U32 high;
  38. U32 low;
  39. };
  40. class LL_COMMON_API LLUUID
  41. {
  42. public:
  43. //
  44. // CREATORS
  45. //
  46. LLUUID();
  47. explicit LLUUID(const char *in_string); // Convert from string.
  48. explicit LLUUID(const std::string& in_string); // Convert from string.
  49. LLUUID(const LLUUID &in);
  50. LLUUID &operator=(const LLUUID &rhs);
  51. ~LLUUID();
  52. //
  53. // MANIPULATORS
  54. //
  55. void generate(); // Generate a new UUID
  56. void generate(const std::string& stream); //Generate a new UUID based on hash of input stream
  57. static LLUUID generateNewID(std::string stream = ""); //static version of above for use in initializer expressions such as constructor params, etc.
  58. BOOL set(const char *in_string, BOOL emit = TRUE); // Convert from string, if emit is FALSE, do not emit warnings
  59. BOOL set(const std::string& in_string, BOOL emit = TRUE); // Convert from string, if emit is FALSE, do not emit warnings
  60. void setNull(); // Faster than setting to LLUUID::null.
  61. S32 cmpTime(uuid_time_t *t1, uuid_time_t *t2);
  62. static void getSystemTime(uuid_time_t *timestamp);
  63. void getCurrentTime(uuid_time_t *timestamp);
  64. //
  65. // ACCESSORS
  66. //
  67. BOOL isNull() const; // Faster than comparing to LLUUID::null.
  68. BOOL notNull() const; // Faster than comparing to LLUUID::null.
  69. // JC: This is dangerous. It allows UUIDs to be cast automatically
  70. // to integers, among other things. Use isNull() or notNull().
  71. // operator bool() const;
  72. // JC: These must return real bool's (not BOOLs) or else use of the STL
  73. // will generate bool-to-int performance warnings.
  74. bool operator==(const LLUUID &rhs) const;
  75. bool operator!=(const LLUUID &rhs) const;
  76. bool operator<(const LLUUID &rhs) const;
  77. bool operator>(const LLUUID &rhs) const;
  78. // xor functions. Useful since any two random uuids xored together
  79. // will yield a determinate third random unique id that can be
  80. // used as a key in a single uuid that represents 2.
  81. const LLUUID& operator^=(const LLUUID& rhs);
  82. LLUUID operator^(const LLUUID& rhs) const;
  83. // similar to functions above, but not invertible
  84. // yields a third random UUID that can be reproduced from the two inputs
  85. // but which, given the result and one of the inputs can't be used to
  86. // deduce the other input
  87. LLUUID combine(const LLUUID& other) const;
  88. void combine(const LLUUID& other, LLUUID& result) const;
  89. friend LL_COMMON_API std::ostream& operator<<(std::ostream& s, const LLUUID &uuid);
  90. friend LL_COMMON_API std::istream& operator>>(std::istream& s, LLUUID &uuid);
  91. void toString(char *out) const; // Does not allocate memory, needs 36 characters (including \0)
  92. void toString(std::string& out) const;
  93. void toCompressedString(char *out) const; // Does not allocate memory, needs 17 characters (including \0)
  94. void toCompressedString(std::string& out) const;
  95. std::string asString() const;
  96. std::string getString() const;
  97. U16 getCRC16() const;
  98. U32 getCRC32() const;
  99. static BOOL validate(const std::string& in_string); // Validate that the UUID string is legal.
  100. static const LLUUID null;
  101. static U32 getRandomSeed();
  102. static S32 getNodeID(unsigned char * node_id);
  103. static BOOL parseUUID(const std::string& buf, LLUUID* value);
  104. U8 mData[UUID_BYTES];
  105. };
  106. typedef std::vector<LLUUID> uuid_vec_t;
  107. // Construct
  108. inline LLUUID::LLUUID()
  109. {
  110. setNull();
  111. }
  112. // Faster than copying from memory
  113. inline void LLUUID::setNull()
  114. {
  115. U32 *word = (U32 *)mData;
  116. word[0] = 0;
  117. word[1] = 0;
  118. word[2] = 0;
  119. word[3] = 0;
  120. }
  121. // Compare
  122. inline bool LLUUID::operator==(const LLUUID& rhs) const
  123. {
  124. U32 *tmp = (U32 *)mData;
  125. U32 *rhstmp = (U32 *)rhs.mData;
  126. // Note: binary & to avoid branching
  127. return
  128. (tmp[0] == rhstmp[0]) &
  129. (tmp[1] == rhstmp[1]) &
  130. (tmp[2] == rhstmp[2]) &
  131. (tmp[3] == rhstmp[3]);
  132. }
  133. inline bool LLUUID::operator!=(const LLUUID& rhs) const
  134. {
  135. U32 *tmp = (U32 *)mData;
  136. U32 *rhstmp = (U32 *)rhs.mData;
  137. // Note: binary | to avoid branching
  138. return
  139. (tmp[0] != rhstmp[0]) |
  140. (tmp[1] != rhstmp[1]) |
  141. (tmp[2] != rhstmp[2]) |
  142. (tmp[3] != rhstmp[3]);
  143. }
  144. /*
  145. // JC: This is dangerous. It allows UUIDs to be cast automatically
  146. // to integers, among other things. Use isNull() or notNull().
  147. inline LLUUID::operator bool() const
  148. {
  149. U32 *word = (U32 *)mData;
  150. return (word[0] | word[1] | word[2] | word[3]) > 0;
  151. }
  152. */
  153. inline BOOL LLUUID::notNull() const
  154. {
  155. U32 *word = (U32 *)mData;
  156. return (word[0] | word[1] | word[2] | word[3]) > 0;
  157. }
  158. // Faster than == LLUUID::null because doesn't require
  159. // as much memory access.
  160. inline BOOL LLUUID::isNull() const
  161. {
  162. U32 *word = (U32 *)mData;
  163. // If all bits are zero, return !0 == TRUE
  164. return !(word[0] | word[1] | word[2] | word[3]);
  165. }
  166. // Copy constructor
  167. inline LLUUID::LLUUID(const LLUUID& rhs)
  168. {
  169. U32 *tmp = (U32 *)mData;
  170. U32 *rhstmp = (U32 *)rhs.mData;
  171. tmp[0] = rhstmp[0];
  172. tmp[1] = rhstmp[1];
  173. tmp[2] = rhstmp[2];
  174. tmp[3] = rhstmp[3];
  175. }
  176. inline LLUUID::~LLUUID()
  177. {
  178. }
  179. // Assignment
  180. inline LLUUID& LLUUID::operator=(const LLUUID& rhs)
  181. {
  182. // No need to check the case where this==&rhs. The branch is slower than the write.
  183. U32 *tmp = (U32 *)mData;
  184. U32 *rhstmp = (U32 *)rhs.mData;
  185. tmp[0] = rhstmp[0];
  186. tmp[1] = rhstmp[1];
  187. tmp[2] = rhstmp[2];
  188. tmp[3] = rhstmp[3];
  189. return *this;
  190. }
  191. inline LLUUID::LLUUID(const char *in_string)
  192. {
  193. if (!in_string || in_string[0] == 0)
  194. {
  195. setNull();
  196. return;
  197. }
  198. set(in_string);
  199. }
  200. inline LLUUID::LLUUID(const std::string& in_string)
  201. {
  202. if (in_string.empty())
  203. {
  204. setNull();
  205. return;
  206. }
  207. set(in_string);
  208. }
  209. // IW: DON'T "optimize" these w/ U32s or you'll scoogie the sort order
  210. // IW: this will make me very sad
  211. inline bool LLUUID::operator<(const LLUUID &rhs) const
  212. {
  213. U32 i;
  214. for( i = 0; i < (UUID_BYTES - 1); i++ )
  215. {
  216. if( mData[i] != rhs.mData[i] )
  217. {
  218. return (mData[i] < rhs.mData[i]);
  219. }
  220. }
  221. return (mData[UUID_BYTES - 1] < rhs.mData[UUID_BYTES - 1]);
  222. }
  223. inline bool LLUUID::operator>(const LLUUID &rhs) const
  224. {
  225. U32 i;
  226. for( i = 0; i < (UUID_BYTES - 1); i++ )
  227. {
  228. if( mData[i] != rhs.mData[i] )
  229. {
  230. return (mData[i] > rhs.mData[i]);
  231. }
  232. }
  233. return (mData[UUID_BYTES - 1] > rhs.mData[UUID_BYTES - 1]);
  234. }
  235. inline U16 LLUUID::getCRC16() const
  236. {
  237. // A UUID is 16 bytes, or 8 shorts.
  238. U16 *short_data = (U16*)mData;
  239. U16 out = 0;
  240. out += short_data[0];
  241. out += short_data[1];
  242. out += short_data[2];
  243. out += short_data[3];
  244. out += short_data[4];
  245. out += short_data[5];
  246. out += short_data[6];
  247. out += short_data[7];
  248. return out;
  249. }
  250. inline U32 LLUUID::getCRC32() const
  251. {
  252. U32 *tmp = (U32*)mData;
  253. return tmp[0] + tmp[1] + tmp[2] + tmp[3];
  254. }
  255. // Helper structure for ordering lluuids in stl containers.
  256. // eg: std::map<LLUUID, LLWidget*, lluuid_less> widget_map;
  257. struct lluuid_less
  258. {
  259. bool operator()(const LLUUID& lhs, const LLUUID& rhs) const
  260. {
  261. return (lhs < rhs) ? true : false;
  262. }
  263. };
  264. typedef std::set<LLUUID, lluuid_less> uuid_list_t;
  265. /*
  266. * Sub-classes for keeping transaction IDs and asset IDs
  267. * straight.
  268. */
  269. typedef LLUUID LLAssetID;
  270. class LL_COMMON_API LLTransactionID : public LLUUID
  271. {
  272. public:
  273. LLTransactionID() : LLUUID() { }
  274. static const LLTransactionID tnull;
  275. LLAssetID makeAssetID(const LLUUID& session) const;
  276. };
  277. #endif