opennurbs_uuid.h
1 /* $NoKeywords: $ */
2 /*
3 //
4 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
5 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
6 // McNeel & Associates.
7 //
8 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
9 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
10 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
11 //
12 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
13 //
14 ////////////////////////////////////////////////////////////////
15 */
16 
17 #if !defined(OPENNURBS_UUID_INC_)
18 #define OPENNURBS_UUID_INC_
19 
20 // ON_UUID is a 16 byte universally unique identifier
21 #if !defined(ON_UUID_DEFINED)
22  // This code also exists in version.h, but needs to be here
23  // to solve include ordering problems, and for standalone
24  // opennurbs.dll builds.
25  #define ON_UUID_DEFINED
26  // ON_UUID is a 16 byte universally unique identifier
27  #if defined(UUID_DEFINED)
28  typedef UUID ON_UUID;
29  #elif defined(GUID_DEFINED)
30  typedef GUID ON_UUID;
31  #else
32  typedef struct ON_UUID_struct
33  {
34  unsigned int Data1; // 32 bit unsigned integer
35  unsigned short Data2; // 16 bit unsigned integer
36  unsigned short Data3; // 16 bit unsigned integer
37  unsigned char Data4[8];
38  } ON_UUID;
39  #endif
40 #endif
41 
42 
43 ON_BEGIN_EXTERNC
44 
45 // All bits are zero in ON_nil_uuid and
46 // ON_UuidCompare( ON_nil_uuid, U ) < 0 if U != ON_nil_uuid.
47 extern ON_EXTERN_DECL const ON_UUID ON_nil_uuid;
48 
49 // All bits are one in ON_max_uuid and
50 // ON_UuidCompare( U, ON_max_uuid ) < 0 if U != ON_max_uuid.
51 extern ON_EXTERN_DECL const ON_UUID ON_max_uuid;
52 
53 // Application ids for the versions of Rhino that
54 // write 3dm files. All userdata classed defined
55 // in the core Rhino.exe should use these ids
56 // as the application id.
57 // In situations where you want to use the id
58 // for the current version of Rhino, use
59 // ON_rhino_id and you won't have to update
60 // your code when Rhino versions roll.
61 extern ON_EXTERN_DECL const ON_UUID ON_rhino2_id;
62 extern ON_EXTERN_DECL const ON_UUID ON_rhino3_id;
63 extern ON_EXTERN_DECL const ON_UUID ON_rhino4_id;
64 extern ON_EXTERN_DECL const ON_UUID ON_rhino5_id;
65 extern ON_EXTERN_DECL const ON_UUID ON_rhino6_id;
66 extern ON_EXTERN_DECL const ON_UUID ON_rhino_id;
67 
68 // Application ids for usedata written by versions
69 // of opennurbs before userdata had application ids.
70 extern ON_EXTERN_DECL const ON_UUID ON_v2_userdata_id;
71 extern ON_EXTERN_DECL const ON_UUID ON_v3_userdata_id;
72 extern ON_EXTERN_DECL const ON_UUID ON_v4_userdata_id;
73 
74 // Application id for the versions of openNURBS that
75 // write userdata in 3dm files. User data whose class
76 // definition is in opennurbs should use these
77 // ids as the user data application id.
78 // No other user data should use these ids.
79 //
80 // In situations where you want to use the id
81 // for the current version of opennurbs, use
82 // ON_opennurbs_id and you won't have to update
83 // your code when opennurbs versions roll.
84 #if defined(ON_COMPILING_OPENNURBS)
85 extern const ON_UUID ON_opennurbs4_id;
86 extern const ON_UUID ON_opennurbs5_id;
87 extern const ON_UUID ON_opennurbs6_id;
88 #endif
89 extern ON_EXTERN_DECL const ON_UUID ON_opennurbs_id;
90 
91 /*
92 Description:
93  Determine if an id is a Rhino application id.
94 Parameters:
95  id - [in]
96  id to test
97 Returns:
98  0: id is not a Rhino application id
99  N: id == ON_rhinoN_id, where "N" = 2,3,4,5,6,...
100 */
101 unsigned int ON_IsRhinoApplicationId(
102  ON_UUID id
103  );
104 
105 /*
106 Description:
107  Determine if an id is an opennurbs application id.
108 Parameters:
109  id - [in]
110  id to test
111 Returns:
112  0: id is not an opennurbs application id
113  N: id == ON_opennurbsN_id, where "N" = 4,5,6,...
114 */
115 unsigned int ON_IsOpennurbsApplicationId(
116  ON_UUID id
117  );
118 
119 ON_END_EXTERNC
120 
121 #if defined(ON_CPLUSPLUS)
122 
123 /*
124 Description:
125  Creates a new uuid.(&a,&b) compares two uuids.
126 Parameters:
127  new_uuid - [out]
128 Returns:
129  True if successful.
130 */
131 ON_DECL
132 bool ON_CreateUuid( ON_UUID& uuid );
133 
134 ON_DECL
135 ON_UUID ON_CreateId();
136 
137 /*
138 Description:
139  This function is used for in testing situations when an
140  ON_UUID value needs to be repeatable.
141 Parameter:
142  index_64_bit - [in]
143  index_64_bit > 0 and index_64_bit <= 0X0000FFFFFFFFFFFF.
144  Calling this function with the same index will create
145  an ON_UUID with the same value.
146 Returns:
147  If index_64_bit = 0 or >0X0000FFFFFFFFFFFF,
148  then ON_nil_uuid is returned.
149  Otherwise, a ON_UUID with the value
150  xxxxxxxx-yyyy-11dc-9885-001372C33878
151  where xxxxxxxxyyyy = the hexadecimal representation
152  of index_64_bit.
153  The returned id is not unique.
154 */
155 ON_DECL
156 ON_UUID ON_NotUniqueIdFromIndex(
157  ON__UINT64 index_64_bit
158 );
159 
160 /*
161 Description:
162  This function is used for in testing situations when an
163  ON_UUID value needs to be repeatable.
164 Parameter:
165  index_32_bit - [in]
166  index_32_bit > 0.
167  Calling this function with the same index will create
168  an ON_UUID with the same value.
169 Returns:
170  If index_32_bit = 0, then ON_nil_uuid is returned.
171  Otherwise, a ON_UUID with the value
172  xxxxxxxx-yyyy-11dc-9885-001372C33878
173  where xxxxxxxxyyyy = the hexadecimal representation
174  of index_32_bit.
175  The returned id is not unique.
176 */
177 ON_DECL
178 ON_UUID ON_NotUniqueIdFromIndex(
179  ON__UINT32 index_32_bit
180 );
181 
182 
183 /*
184 Description:
185  This function is used for in testing situations when an
186  ON_UUID value needs to be repeatable.
187 Parameter:
188  not_unique_id - [in]
189  A not unique id created by ON_NotUniqueIdFromIndex() or ON_NextNotUniqueId().
190 Returns:
191  If not_unique_id was created by on of the NotUniqueId functions,
192  then a ON_UUID with the value
193  xxxxxxxx-yyyy-11dc-9885-001372C33878
194  where xxxxxxxxyyyy = the hexadecimal representation
195  of the int id's index + 1 is returned.
196  Otherwise, NO_nil_uuid is returned.
197  The returned id is not unique.
198 */
199 ON_DECL
200 ON_UUID ON_NextNotUniqueId(
201  ON_UUID not_unique_id
202 );
203 
204 /*
205 Description:
206  This function is used for in testing situations when an
207  ON_UUID value needs to be repeatable.
208 Parameter:
209  not_unique_id - [in]
210  A not unique id created by ON_NotUniqueIdFromIndex() or ON_NextNotUniqueId().
211 Returns:
212  If not_unique_id was created by on of the NotUniqueId functions,
213  then the index of that id is returned.
214  Otherwise, 0 is return if not_unique_id is nil and
215  0xFFFF00000000 is returned in all other cases.
216 */ON_DECL
217 ON__UINT64 ON_IndexFromNotUniqueId(
218  ON_UUID not_unique_id
219 );
220 
221 
222 
223 //class ON_CLASS ON_Id : public ON_UUID
224 //{
225 //public:
226 // ON_Id() = default;
227 // ~ON_Id() = default;
228 // ON_Id(const ON_Id&) = default;
229 // ON_Id& operator=(const ON_Id&) = default;
230 //
231 // ON_Id(const ON_UUID&);
232 // ON_Id& operator=(const ON_UUID&);
233 //
234 // operator ON_UUID&();
235 // operator const ON_UUID&() const;
236 //
237 // static ON_Id Create();
238 // static const ON_Id nil;
239 //
240 // static int Compare(
241 // const ON_Id& a,
242 // const ON_Id& b
243 // );
244 //
245 // static int ComparePtr(
246 // const ON_Id* a,
247 // const ON_Id* b
248 // );
249 //
250 // static int CompareUUID(
251 // const ON_UUID& a,
252 // const ON_UUID& b
253 // );
254 //
255 // static int CompareUUIDPtr(
256 // const ON_UUID* a,
257 // const ON_UUID* b
258 // );
259 //
260 // bool IsNil() const;
261 // bool IsNotNil() const;
262 //};
263 //
264 //bool operator==(const ON_Id& a, const ON_Id& b );
265 //bool operator==(const ON_UUID& a, const ON_Id& b );
266 //bool operator==(const ON_Id& a, const ON_UUID& b );
267 //
268 //bool operator!=(const ON_Id& a, const ON_Id& b );
269 //bool operator!=(const ON_UUID& a, const ON_Id& b );
270 //bool operator!=(const ON_Id& a, const ON_UUID& b );
271 //
272 //bool operator<=(const ON_Id& a, const ON_Id& b );
273 //bool operator<=(const ON_UUID& a, const ON_Id& b );
274 //bool operator<=(const ON_Id& a, const ON_UUID& b );
275 //
276 //bool operator>=(const ON_Id& a, const ON_Id& b );
277 //bool operator>=(const ON_UUID& a, const ON_Id& b );
278 //bool operator>=(const ON_Id& a, const ON_UUID& b );
279 //
280 //bool operator<(const ON_Id& a, const ON_Id& b );
281 //bool operator<(const ON_UUID& a, const ON_Id& b );
282 //bool operator<(const ON_Id& a, const ON_UUID& b );
283 //
284 //
285 //bool operator>(const ON_Id& a, const ON_Id& b );
286 //bool operator>(const ON_UUID& a, const ON_Id& b );
287 //bool operator>(const ON_Id& a, const ON_UUID& b );
288 
289 
290 /*
291 Description:
292  This class is used by ON_UuidIndexList. It is used when
293  uuids are used to search for items that can be found by
294  an integer index.
295 */
296 class ON_CLASS ON_UuidIndex
297 {
298 public:
299  static const ON_UuidIndex NilIndex; // uuid = nil, index = 0
300 
301  ON_UuidIndex() = default;
302  ~ON_UuidIndex() = default;
303  ON_UuidIndex(const ON_UuidIndex&) = default;
304  ON_UuidIndex& operator=(const ON_UuidIndex&) = default;
305 
306  /*
307  Dictionary compare m_id and then m_i.
308  */
309  static
310  int CompareIdAndIndex( const ON_UuidIndex* a, const ON_UuidIndex* b );
311 
312  /*
313  Dictionary compare m_id and then m_i.
314  */
315  static
316  int CompareIndexAndId( const ON_UuidIndex* a, const ON_UuidIndex* b );
317 
318  /*
319  Compare m_id and ignore m_i.
320  */
321  static
322  int CompareId( const ON_UuidIndex* a, const ON_UuidIndex* b );
323 
324  /*
325  Compare m_i and ignore m_id.
326  */
327  static
328  int CompareIndex( const ON_UuidIndex* a, const ON_UuidIndex* b );
329 
330  // In cases when there is a discrepancy between the m_id and
331  // m_i, m_id is assumed to be valid unless comments where this
332  // class is used indicate otherwise.
333  ON_UUID m_id = ON_nil_uuid;
334  int m_i = 0;
335 };
336 
337 
338 /*
339 Description:
340  This class is used by ON_UuidIndexList. It is used when
341  uuids are used to search for items that can be found by
342  an integer index.
343 */
344 class ON_CLASS ON_UuidPtr
345 {
346 public:
347  static const ON_UuidPtr NilPtr; // uuid = nil, ptr = 0;
348 
349  ON_UuidPtr() = default;
350  ~ON_UuidPtr() = default;
351  ON_UuidPtr(const ON_UuidPtr&) = default;
352  ON_UuidPtr& operator=(const ON_UuidPtr&) = default;
353 
354  /*
355  Dictionary compare m_id and then m_ptr.
356  */
357  static
358  int CompareIdAndPtr( const ON_UuidPtr* a, const ON_UuidPtr* b );
359 
360  /*
361  Dictionary compare m_id and then m_ptr.
362  */
363  static
364  int ComparePtrAndId( const ON_UuidPtr* a, const ON_UuidPtr* b );
365 
366  /*
367  Compare m_id and ignore m_ptr.
368  */
369  static
370  int CompareId( const ON_UuidPtr* a, const ON_UuidPtr* b );
371 
372  /*
373  Compare m_ptr and ignore m_id.
374  */
375  static
376  int ComparePtr( const ON_UuidPtr* a, const ON_UuidPtr* b );
377 
378  // In cases when there is a discrepancy between the m_id and
379  // m_ptr, m_id is assumed to be valid unless comments where this
380  // class is used indicate otherwise.
381  ON_UUID m_id = ON_nil_uuid;
382  ON__UINT_PTR m_ptr = 0;
383 };
384 
385 /*
386 Description:
387  ON_UuidCompare(&a,&b) compares two uuids.
388 Parameters:
389  a - [in]
390  b - [in]
391 Returns:
392  @untitled table
393  -1 a < b
394  0 a == b
395  +1 a > b
396 Remarks:
397  A nullptr pointer is considered < a non-nullptr pointer.
398 */
399 ON_DECL
400 int ON_UuidCompare(
401  const ON_UUID* a,
402  const ON_UUID* b
403  );
404 
405 /*
406 Description:
407  ON_UuidCompare(a,b) compares two uuids.
408 Parameters:
409  a - [in]
410  b - [in]
411 Returns:
412  @untitled table
413  -1 a < b
414  0 a == b
415  +1 a > b
416 */
417 ON_DECL
418 int ON_UuidCompare(
419  const ON_UUID& a,
420  const ON_UUID& b
421  );
422 
423 /*
424 Description:
425  Test uuid to see if it is nil (identically zero).
426 Parameters:
427  uuid - [in]
428 Returns:
429  true if uuid is nil.
430 */
431 ON_DECL
432 bool ON_UuidIsNil(
433  const ON_UUID& uuid
434  );
435 
436 /*
437 Description:
438  Test uuid to see if it is not nil (not identically zero).
439 Parameters:
440  uuid - [in]
441 Returns:
442  true if uuid is not nil (non zero)
443 */
444 ON_DECL
445 bool ON_UuidIsNotNil(
446  const ON_UUID& uuid
447  );
448 
449 /*
450 Description:
451  Converts a string like
452  "{85A08515-f383-11d3-BFE7-0010830122F0}"
453  into a uuid.
454  The brackets are optional and are ignored.
455  Hyphens can appear anywhere or be missing.
456  The hex digits can be upper or lower case.
457 Parameters:
458  s - [in]
459 Returns:
460  uuid.
461  If the string is not a uuid, then ON_nil_uuid is returnd.
462 */
463 ON_DECL
464 ON_UUID ON_UuidFromString( const char* s );
465 
466 /*
467 Description:
468  Converts a string like
469  "{85A08515-f383-11d3-BFE7-0010830122F0}"
470  into a uuid.
471  The brackets are optional and are ignored.
472  Hyphens can appear anywhere or be missing.
473  The hex digits can be upper or lower case.
474 Parameters:
475  s - [in]
476 Returns:
477  uuid.
478  If the string is not a uuid, then ON_nil_uuid is returnd.
479 */
480 ON_DECL
481 ON_UUID ON_UuidFromString( const wchar_t* s );
482 
483 /*
484 Description:
485  Converts a uuid to a null termintated ASCII string like
486  "85a08515-f383-11d3-bfe7-0010830122f0".
487 Parameters:
488  uuid - [in]
489  s - [out] The s[] char array must have length >= 37.
490  The returned char array will have a 36
491  character uuid in s[0..35] and a null in s[36].
492 Returns:
493  The pointer to the array is returned.
494 */
495 ON_DECL
496 char* ON_UuidToString( const ON_UUID& uuid, char* s );
497 
498 
499 /*
500 Description:
501  Converts a uuid to a null termintated UNICODE string like
502  "85a08515-f383-11d3-bfe7-0010830122f0".
503 Parameters:
504  uuid - [in]
505  s - [out] The s[] wchar_t array must have length >= 37.
506  The returned char array will have a 36
507  character uuid in s[0..35] and a null in s[36].
508 Returns:
509  The pointer to the array is returned.
510 */
511 ON_DECL
512 wchar_t* ON_UuidToString( const ON_UUID& uuid, wchar_t* s );
513 
514 /*
515 Description:
516  Parses a string like "85a08515-f383-11d3-bfe7-0010830122f0"
517  and returns the value as a uuid. Hyphens can appear anywhere
518  and are ignored. Parsing stops at any character that is not
519  a hexidecimal digit or hyphen. Parsing stops after 32 hexidecimal
520  digits are read;
521 Parameters:
522  sUuid - [in]
523  uuid - [out]
524 Returns:
525  null
526  Parsing failed and uuid is set to ON_nil_uuid.
527  non null
528  Parsing succeeded and uuid is set to the parsed value.
529  The returned pointer is the character in sUUID immediately
530  after the last parsed character.
531 */
532 ON_DECL
533 const char* ON_ParseUuidString(
534  const char* sUUID,
535  ON_UUID* uuid
536  );
537 
538 class ON_String;
539 
540 /*
541 Description:
542  Converts a uuid to a null termintated string like
543  "85a08515-f383-11d3-bfe7-0010830122f0".
544 Parameters:
545  uuid - [in]
546  s - [out]
547 Returns:
548  The pointer to the array is returned.
549 */
550 ON_DECL
551 const char* ON_UuidToString( const ON_UUID& uuid, ON_String& s);
552 
553 class ON_wString;
554 
555 /*
556 Description:
557  Converts a uuid to a null termintated string like
558  "85a08515-f383-11d3-bfe7-0010830122f0".
559 Parameters:
560  uuid - [in]
561  s - [out]
562 Returns:
563  The pointer to the array is returned.
564 */
565 ON_DECL
566 const wchar_t* ON_UuidToString( const ON_UUID& uuid, ON_wString& s);
567 
568 ON_DECL
569 const ON_wString ON_IdToString(
570  ON_UUID id
571 );
572 
573 ON_DECL
574 const ON_wString ON_AddIdPrefixToString(
575  const ON_UUID id,
576  const wchar_t* separator,
577  const wchar_t* source
578  );
579 
580 ON_DECL
581 const ON_wString ON_RemoveIdPrefixFromString(
582  const ON_UUID id,
583  const wchar_t* separator,
584  const wchar_t* source
585 );
586 
587 ON_DECL
588 const ON_wString ON_AddIdSuffixToString(
589  const wchar_t* source,
590  const wchar_t* separator,
591  const ON_UUID id
592 );
593 
594 ON_DECL
595 const ON_wString ON_RemoveIdSuffixFromString(
596  const wchar_t* source,
597  const wchar_t* separator,
598  const ON_UUID id
599 );
600 
601 
602 #endif
603 
604 #endif
ON_UUID is a 16 byte universally unique identifier.
Definition: opennurbs_uuid.h:32
Definition: opennurbs_string.h:2020
unsigned short Data3
Definition: opennurbs_uuid.h:36
unsigned short Data2
Definition: opennurbs_uuid.h:35
Definition: opennurbs_string.h:852
unsigned int Data1
Definition: opennurbs_uuid.h:34
unsigned char Data4[8]
Definition: opennurbs_uuid.h:37