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 #include <iostream>
8 
9 #include <nx/sdk/uuid.h>
10 
11 namespace nx {
12 namespace sdk {
13 
14 namespace UuidHelper
15 {
17  template<typename Byte>
18  Uuid fromRawData(const Byte* data)
19  {
20  static_assert(sizeof(Byte) == 1, "Expected pointer to array of byte-sized items");
21  Uuid result;
22  memcpy(&result, data, sizeof(result));
23  return result;
24  }
25 
27  Uuid fromStdString(const std::string& str);
28 
29  enum FormatOptions
30  {
31  none = 0,
32  uppercase = 1 << 0,
33  hyphens = 1 << 1,
34  braces = 1 << 2,
35  all = 0xFF
36  };
37 
39  std::string toStdString(const Uuid& uuid, FormatOptions formatOptions = FormatOptions::all);
40 
41  Uuid randomUuid();
42 }
43 
44 } // namespace sdk
45 } // namespace nx
46 
47 //-------------------------------------------------------------------------------------------------
48 // Functions that need to be in namespace std for compatibility with STL features.
49 
50 namespace std {
51 
52 inline std::ostream& operator<<(std::ostream& os, const nx::sdk::Uuid& uuid)
53 {
54  return os << nx::sdk::UuidHelper::toStdString(uuid);
55 }
56 
57 template<>
58 struct hash<nx::sdk::Uuid>
59 {
60  size_t operator()(const nx::sdk::Uuid& uuid) const
61  {
62  size_t h = 0;
63  for (const auto b: uuid)
64  h = (h + (324723947 + b)) ^ 93485734985;
65  return h;
66  }
67 };
68 
69 } // namespace std
Definition: json.h:39
Definition: uuid.h:22
Definition: apple_utils.h:6