nx_metadata_sdk  1.0
Metadata SDK
uuid_helper.h
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2 
3 #pragma once
4 
5 #include <cstring>
6 #include <string>
7 
8 #include <nx/sdk/uuid.h>
9 
10 namespace nx {
11 namespace sdk {
12 
13 namespace UuidHelper
14 {
16  template<typename Byte>
17  Uuid fromRawData(const Byte* data)
18  {
19  static_assert(sizeof(Byte) == 1, "Expected pointer to array of byte-sized items");
20  Uuid result;
21  memcpy(&result, data, sizeof(result));
22  return result;
23  }
24 
26  Uuid fromStdString(const std::string& str);
27 
28  enum FormatOptions
29  {
30  none = 0,
31  uppercase = 1 << 0,
32  hyphens = 1 << 1,
33  braces = 1 << 2,
34  all = 0xFF
35  };
36 
38  std::string toStdString(const Uuid& uuid, FormatOptions formatOptions = FormatOptions::all);
39 
40  Uuid randomUuid();
41 }
42 
43 } // namespace sdk
44 } // namespace nx
45 
46 //-------------------------------------------------------------------------------------------------
47 // Functions that need to be in namespace std for compatibility with STL features.
48 
49 namespace std {
50 
51 inline std::ostream& operator<<(std::ostream& os, const nx::sdk::Uuid& uuid)
52 {
53  return os << nx::sdk::UuidHelper::toStdString(uuid);
54 }
55 
56 template<>
57 struct hash<nx::sdk::Uuid>
58 {
59  size_t operator()(const nx::sdk::Uuid& uuid) const
60  {
61  size_t h = 0;
62  for (const auto b: uuid)
63  h = (h + (324723947 + b)) ^ 93485734985;
64  return h;
65  }
66 };
67 
68 } // namespace std
Definition: to_string.h:51
Definition: uuid.h:21
Definition: apple_utils.h:6