PeriDyno 1.2.1
Loading...
Searching...
No Matches
guid.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <iostream>
5#include <array>
6#include <sstream>
7#include <string_view>
8#include <utility>
9#include <iomanip>
10
11namespace Wt
12{
13 class Guid
14 {
15 public:
16 explicit Guid(const std::array<unsigned char, 16>& bytes);
17 explicit Guid(std::array<unsigned char, 16>&& bytes);
18
19 Guid();
20
21 Guid(const Guid& other) = default;
22 Guid& operator=(const Guid& other) = default;
23 Guid(Guid&& other) = default;
24 Guid& operator=(Guid&& other) = default;
25
26 bool operator==(const Guid& other) const;
27 bool operator!=(const Guid& other) const;
28
29 std::string str() const;
30 operator std::string() const;
31 const std::array<unsigned char, 16>& bytes() const;
32 void swap(Guid& other);
33 bool isValid() const;
34
35 private:
36 std::array<unsigned char, 16> _bytes;
37
38 friend std::ostream& operator<<(std::ostream& s, const Guid& guid);
39 friend bool operator<(const Guid& lhs, const Guid& rhs);
40 };
41
42 Guid newGuid();
43
44 namespace details
45 {
46 template <typename...> struct hash;
47
48 template<typename T>
49 struct hash<T> : public std::hash<T>
50 {
51 using std::hash<T>::hash;
52 };
53
54
55 template <typename T, typename... Rest>
56 struct hash<T, Rest...>
57 {
58 inline std::size_t operator()(const T& v, const Rest&... rest) {
59 std::size_t seed = hash<Rest...>{}(rest...);
60 seed ^= hash<T>{}(v)+0x9e3779b9 + (seed << 6) + (seed >> 2);
61 return seed;
62 }
63 };
64 }
65}
66
67namespace std
68{
69 // Template specialization for std::swap<Guid>() --
70 // See guid.cpp for the function definition
71 template <>
72 void swap(Wt::Guid& guid0, Wt::Guid& guid1) noexcept;
73
74 // Specialization for std::hash<Guid> -- this implementation
75 // uses std::hash<std::string> on the stringification of the guid
76 // to calculate the hash
77 template <>
78 struct hash<Wt::Guid>
79 {
80 std::size_t operator()(Wt::Guid const& guid) const
81 {
82 const uint64_t* p = reinterpret_cast<const uint64_t*>(guid.bytes().data());
83 return Wt::details::hash<uint64_t, uint64_t>{}(p[0], p[1]);
84 }
85 };
86}
bool isValid() const
Definition guid.cpp:64
bool operator!=(const Guid &other) const
Definition guid.cpp:17
Guid & operator=(const Guid &other)=default
std::array< unsigned char, 16 > _bytes
Definition guid.hpp:36
const std::array< unsigned char, 16 > & bytes() const
Definition guid.cpp:53
Guid(const std::array< unsigned char, 16 > &bytes)
Definition guid.cpp:6
Guid(const Guid &other)=default
void swap(Guid &other)
Definition guid.cpp:58
friend bool operator<(const Guid &lhs, const Guid &rhs)
Definition guid.cpp:98
Guid(Guid &&other)=default
Guid & operator=(Guid &&other)=default
std::string str() const
Definition guid.cpp:22
Guid()
Definition guid.cpp:10
friend std::ostream & operator<<(std::ostream &s, const Guid &guid)
Definition guid.cpp:70
bool operator==(const Guid &other) const
Definition guid.cpp:12
#define T(t)
Definition guid.cpp:5
Guid newGuid()
Definition guid.cpp:103
Definition guid.cpp:136
void swap(Wt::Guid &lhs, Wt::Guid &rhs) noexcept
Definition guid.cpp:138
std::size_t operator()(const T &v, const Rest &... rest)
Definition guid.hpp:58
std::size_t operator()(Wt::Guid const &guid) const
Definition guid.hpp:80