GA::kit v0.3
G&A's in-house C++ application framework
Loading...
Searching...
No Matches
crossguid.hpp
Go to the documentation of this file.
1/*
2The MIT License (MIT)
3
4Copyright (c) 2014 Graeme Hill (http://graemehill.ca)
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22THE SOFTWARE.
23*/
24#pragma once
25
26/* platform / guid lib defines:
27GUID_WINDOWS = windows / win32
28GUID_LIBUUID = linux / libuuid
29GUID_CFUUID = apple / core foundation
30GUID_ANDROID = android / Java Native Interface
31*/
32
33// clang-format off
34#if !defined( GUID_WINDOWS ) && !defined( GUID_LIBUUID ) && !defined( GUID_CFUUID ) && !defined( GUID_ANDROID )
35 // try to detect platform:
36 #ifdef _WIN32
37 #define GUID_WINDOWS
38 #elif defined(__APPLE__)
39 #define GUID_CFUUID
40 #elif defined(__ANDROID__)
41 #define GUID_ANDROID
42 #elif defined(__linux) || defined(__posix)
43 #define GUID_LIBUUID
44 #endif
45#endif
46// clang-format on
47
48#ifdef GUID_ANDROID
49#include <thread>
50#include <jni.h>
51#endif
52
53#include <functional>
54#include <iostream>
55#include <array>
56#include <sstream>
57#include <string>
58#include <utility>
59#include <iomanip>
60
61#define BEGIN_XG_NAMESPACE namespace xg {
62#define END_XG_NAMESPACE }
63
65
66// Class to represent a GUID/UUID. Each instance acts as a wrapper around a
67// 16 byte value that can be passed around by value. It also supports
68// conversion to string (via the stream operator <<) and conversion from a
69// string via constructor.
70class Guid
71{
72public:
73 explicit Guid( const std::array<unsigned char, 16>& bytes );
74 explicit Guid( std::array<unsigned char, 16>&& bytes );
75
76 explicit Guid( const std::string& fromString );
77 Guid();
78
79 Guid( const Guid& other ) = default;
80 Guid& operator=( const Guid& other ) = default;
81 Guid( Guid&& other ) = default;
82 Guid& operator=( Guid&& other ) = default;
83
84 bool operator==( const Guid& other ) const;
85 bool operator!=( const Guid& other ) const;
86
87 std::string str() const;
88 operator std::string() const;
89 const std::array<unsigned char, 16>& bytes() const;
90 void swap( Guid& other );
91 bool isValid() const;
92
93private:
94 void zeroify();
95
96 // actual data
97 std::array<unsigned char, 16> _bytes;
98
99 // make the << operator a friend so it can access _bytes
100 friend std::ostream& operator<<( std::ostream& s, const Guid& guid );
101 friend bool operator<( const Guid& lhs, const Guid& rhs );
102};
103
105
106#ifdef GUID_ANDROID
107struct AndroidGuidInfo
108{
109 static AndroidGuidInfo fromJniEnv( JNIEnv* env );
110
111 JNIEnv* env;
112 jclass uuidClass;
113 jmethodID newGuidMethod;
114 jmethodID mostSignificantBitsMethod;
115 jmethodID leastSignificantBitsMethod;
116 std::thread::id initThreadId;
117};
118
119extern AndroidGuidInfo androidInfo;
120
121void initJni( JNIEnv* env );
122
123// overloading for multi-threaded calls
124Guid newGuid( JNIEnv* env );
125#endif
126
127namespace details {
128template <typename...>
129struct hash;
130
131template <typename T>
132struct hash<T> : public std::hash<T>
133{
134 using std::hash<T>::hash;
135};
136
137template <typename T, typename... Rest>
138struct hash<T, Rest...>
139{
140 inline std::size_t operator()( const T& v, const Rest&... rest )
141 {
142 std::size_t seed = hash<Rest...>{}( rest... );
143 seed ^= hash<T>{}( v ) + 0x9e3779b9 + ( seed << 6 ) + ( seed >> 2 );
144 return seed;
145 }
146};
147} // namespace details
148
150
151namespace std {
152// Template specialization for std::swap<Guid>() --
153// See guid.cpp for the function definition
154template <>
155void swap( xg::Guid& guid0, xg::Guid& guid1 ) noexcept;
156
157// Specialization for std::hash<Guid> -- this implementation
158// uses std::hash<std::string> on the stringification of the guid
159// to calculate the hash
160template <>
161struct hash<xg::Guid>
162{
163 std::size_t operator()( xg::Guid const& guid ) const
164 {
165 const uint64_t* p = reinterpret_cast<const uint64_t*>( guid.bytes().data() );
166 return xg::details::hash<uint64_t, uint64_t>{}( p[0], p[1] );
167 }
168};
169} // namespace std
Definition: crossguid.hpp:71
std::string str() const
Definition: crossguid.cpp:114
const std::array< unsigned char, 16 > & bytes() const
Definition: crossguid.cpp:146
Guid(const Guid &other)=default
void swap(Guid &other)
Definition: crossguid.cpp:262
Guid & operator=(const Guid &other)=default
Guid()
Definition: crossguid.cpp:239
friend bool operator<(const Guid &lhs, const Guid &rhs)
bool operator!=(const Guid &other) const
Definition: crossguid.cpp:256
bool isValid() const
Definition: crossguid.cpp:107
bool operator==(const Guid &other) const
Definition: crossguid.cpp:250
Guid(Guid &&other)=default
Guid & operator=(Guid &&other)=default
friend std::ostream & operator<<(std::ostream &s, const Guid &guid)
Definition: crossguid.cpp:74
#define BEGIN_XG_NAMESPACE
Definition: crossguid.hpp:61
#define END_XG_NAMESPACE
Definition: crossguid.hpp:62
Guid newGuid()
Definition: crossguid.hpp:127
Definition: crossguid.cpp:388
void swap(xg::Guid &lhs, xg::Guid &rhs) noexcept
Definition: crossguid.cpp:390
std::size_t operator()(const T &v, const Rest &... rest)
Definition: crossguid.hpp:140
Definition: crossguid.hpp:129
std::size_t operator()(xg::Guid const &guid) const
Definition: crossguid.hpp:163