MxEngine
UUID.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include <utility>
32 #include <ostream>
33 #include <istream>
34 
35 #include "Utilities/STL/MxString.h"
36 
37 namespace boost::uuids
38 {
39  struct uuid;
40  class random_generator_pure;
41 }
42 
43 namespace MxEngine
44 {
45  class UUID
46  {
47  std::aligned_storage_t<16> uuidImpl;
48 
49  friend class UUIDGenerator;
50  public:
51  boost::uuids::uuid& GetImpl();
52  const boost::uuids::uuid& GetImpl() const;
53  size_t GetHashCode() const;
54  bool operator==(const UUID& other) const;
55  bool operator< (const UUID& other) const;
56  bool operator!=(const UUID& other) const;
57  bool operator> (const UUID& other) const;
58  bool operator<=(const UUID& other) const;
59  bool operator>=(const UUID& other) const;
60 
61  operator MxString() const;
62  };
63 
64  std::ostream& operator<<(std::ostream& out, const UUID& uuid);
65  std::istream& operator>>(std::istream& in, UUID& uuid);
66 
68  {
69  std::aligned_storage_t<8> generator;
70  boost::uuids::random_generator_pure& GetGeneratorImpl();
71  };
72 
74  {
75  static inline UUIDGeneratorImpl* storage;
76  public:
77  static void Init();
78  static UUID Get();
79  static UUID GetNull();
80  static void Clone(UUIDGeneratorImpl* other);
81  static UUIDGeneratorImpl* GetImpl();
82  };
83 }
84 
85 #define HASH_ALGORITHM(NAMESPACE)\
86 namespace NAMESPACE\
87 {\
88  template<typename T> struct hash;\
89  template <>\
90  struct hash<MxEngine::UUID>\
91  {\
92  std::size_t operator()(const MxEngine::UUID& Id) const\
93  {\
94  return Id.GetHashCode();\
95  }\
96  };\
97 }
98 
99 HASH_ALGORITHM(std)
100 HASH_ALGORITHM(eastl)
101 #undef HASH_ALGORITHM
Definition: UUID.h:67
Definition: UUID.h:73
Definition: UUID.h:45
Definition: Application.cpp:49
Definition: UUID.h:37