SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
UUID.h
1 //=========================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //=========================================================================
10 #ifndef smtk_common_UUID_h
11 #define smtk_common_UUID_h
12 
13 #include "smtk/CoreExports.h"
14 #include "smtk/SystemConfig.h"
15 
16 #include "smtk/common/CompilerInformation.h"
17 
18 SMTK_THIRDPARTY_PRE_INCLUDE
19 #include <boost/uuid/uuid.hpp>
20 SMTK_THIRDPARTY_POST_INCLUDE
21 
22 #include <cstring>
23 #include <iosfwd>
24 #include <set>
25 #include <string>
26 #include <vector>
27 
28 namespace smtk
29 {
30 namespace common
31 {
32 
33 //.NAME UUID - An RFC4122-compliant Universally Unique IDentifier (UUID)
34 //.SECTION Description
35 // This class uses Boost's UUID class to implement smtk::common::UUID.
36 // Unlike the Boost version, there is some shorthand to invoke the
37 // various generators.
38 class SMTKCORE_EXPORT UUID
39 {
40 public:
41  static constexpr const char* const type_name = "uuid";
42  typedef ::boost::uint8_t value_type;
43  typedef ::boost::uint8_t* iterator;
44  typedef ::boost::uint8_t const* const_iterator;
45 
46  typedef std::size_t size_type;
47 
48  UUID();
49  UUID(const UUID& other);
50  UUID(const_iterator begin, const_iterator end);
51  UUID(const std::string& txt);
52  UUID(const boost::uuids::uuid& data);
53 
54  static UUID random();
55  static UUID null();
56 
57  enum
58  {
59  SIZE = 16
60  };
61  static size_type size() { return SIZE; }
62  bool isNull() const;
63 
64  iterator begin();
65  const_iterator begin() const;
66  iterator end();
67  const_iterator end() const;
68 
69  std::string toString() const;
70 
71  bool operator!=(UUID const& other) const;
72  bool operator==(UUID const& other) const;
73  bool operator<(UUID const& other) const;
74 
75  UUID& operator=(UUID const& other);
76 
77  operator bool() const;
78 
79  std::size_t hash() const;
80 
81 protected:
82  // Implemented using Boost's UUID library.
83  boost::uuids::uuid m_data;
84 };
85 
86 typedef std::set<UUID> UUIDs;
87 typedef std::vector<UUID> UUIDArray;
88 typedef std::vector<UUIDArray> UUIDArrays;
89 
90 SMTKCORE_EXPORT std::ostream& operator<<(std::ostream& stream, const UUID& uid);
91 SMTKCORE_EXPORT std::istream& operator>>(std::istream& stream, UUID& uid);
92 
93 } // namespace common
94 } // namespace smtk
95 
96 namespace std
97 {
98 
99 // Specialize std::hash<UUID> functor
100 template<>
101 struct hash<smtk::common::UUID>
102 {
103  size_t operator()(const smtk::common::UUID& uid) const
104  {
105  size_t hash;
106  // Use the last sizeof(size_t) bytes as the hash since UUIDs
107  // put their version number in the 4 LSBs of the 8th byte, which
108  // causes collisions when sizeof(size_t) == 8.
109  // This will need to be revisited if we switch to node-based
110  // UUIDs, but for random UUIDs it works well.
111  memmove(&hash, uid.begin() + smtk::common::UUID::size() - sizeof(hash), sizeof(hash));
112  return hash;
113  }
114 };
115 
116 } // namespace std
117 
118 #endif // smtk_common_UUID_h
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
smtk::common::UUIDs
std::set< smtk::common::UUID > UUIDs
Definition: PublicPointerDefs.h:31
smtk::common::UUID
Definition: UUID.h:38
smtk::common::UUID::begin
iterator begin()
Return an iterator to the start of a UUID's raw data.
Definition: UUID.cxx:91
smtk::common::UUIDArray
std::vector< smtk::common::UUID > UUIDArray
Definition: PublicPointerDefs.h:35