nx_server_plugin_sdk 1.0 Server Plugin 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 <iostream>
7 #include <string>
8
9 #include <nx/sdk/uuid.h>
10
11 namespace nx::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 nx::sdk
44
45 //-------------------------------------------------------------------------------------------------
46 // Functions that need to be in namespace std for compatibility with STL features.
47
48 namespace std {
49
50 inline std::ostream& operator<<(std::ostream& os, const nx::sdk::Uuid& uuid)
51 {
52 return os << nx::sdk::UuidHelper::toStdString(uuid);
53 }
54
55 template<>
56 struct hash<nx::sdk::Uuid>
57 {
58 size_t operator()(const nx::sdk::Uuid& uuid) const
59 {
60 size_t h = 0;
61 for (const auto b: uuid)
62 h = (h + (324723947 + b)) ^ 93485734985;
63 return h;
64 }
65 };
66
67 } // namespace std
std
Definition: to_string.h:49
nx::sdk::Uuid
Definition: uuid.h:22
nx
Definition: apple_utils.h:6
nx::sdk
Definition: device_agent.h:12