libqi-api  2.8.7.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ptruid.hpp
Go to the documentation of this file.
1 #pragma once
2 #ifndef QI_PTRUID_HPP
3 #define QI_PTRUID_HPP
4 
5 #include <array>
6 #include <iosfwd>
7 #include <numeric>
8 #include <boost/config.hpp> // for BOOST_CONSTEXPR, et al.
9 #include <boost/functional/hash.hpp>
10 #include <qi/uuid.hpp>
11 #include <qi/api.hpp>
12 #include <qi/assert.hpp>
13 #include <ka/macroregular.hpp>
14 
15 namespace qi
16 {
33  class QI_API PtrUid
34  {
35  using Digest = std::array<uint32_t, 5>;
36  using U32Iterator = Digest::iterator;
37  using U32ConstIterator = Digest::const_iterator;
38 
39  Digest digest;
40 
41  template<typename T>
42  static uint8_t* toUInt8Ptr(T* a)
43  {
44  return reinterpret_cast<uint8_t*>(a);
45  }
46 
47  template<typename T>
48  static const uint8_t* toUInt8Ptr(const T* a)
49  {
50  return reinterpret_cast<const uint8_t*>(a);
51  }
52 
53  public:
54  explicit PtrUid(const Uuid& machineUuid, const Uuid& processUuid, const void* ptr);
55 
56  // Regular: copy, assignment and destruction by default
57  PtrUid() : digest()
58  {
59  }
60 
61  KA_GENERATE_FRIEND_REGULAR_OPS_1(PtrUid, digest)
62 
63  // OStreamable:
64  friend QI_API std::ostream& operator<<(std::ostream& o, const PtrUid& uid);
65 
66  // Sequence:
67  friend uint8_t* begin(PtrUid& a) BOOST_NOEXCEPT
68  {
69  return toUInt8Ptr(&*beginUInt32(a));
70  }
71 
72  friend uint8_t* end(PtrUid& a) BOOST_NOEXCEPT
73  {
74  return begin(a) + size(a);
75  }
76 
77  friend const uint8_t* begin(const PtrUid& a) BOOST_NOEXCEPT
78  {
79  return begin(const_cast<PtrUid&>(a)); // const_cast is safe here.
80  }
81 
82  friend const uint8_t* end(const PtrUid& a) BOOST_NOEXCEPT
83  {
84  return end(const_cast<PtrUid&>(a)); // const_cast is safe here.
85  }
86 
87  friend BOOST_CONSTEXPR size_t size(const PtrUid&) BOOST_NOEXCEPT
88  {
89  return sizeof(Digest) / sizeof(uint8_t);
90  }
91 
92  friend BOOST_CONSTEXPR bool empty(const PtrUid& a) BOOST_NOEXCEPT
93  {
94  return false;
95  }
96 
98  const uint8_t& operator[](size_t n) const BOOST_NOEXCEPT
99  {
100  QI_ASSERT(n < size(*this));
101  return begin(*this)[n];
102  }
103 
104  // Custom:
105  friend U32Iterator beginUInt32(PtrUid& a) BOOST_NOEXCEPT
106  {
107  return begin(a.digest);
108  }
109 
110  friend U32Iterator endUInt32(PtrUid& a) BOOST_NOEXCEPT
111  {
112  return end(a.digest);
113  }
114 
115  friend U32ConstIterator beginUInt32(const PtrUid& a) BOOST_NOEXCEPT
116  {
117  return begin(a.digest);
118  }
119 
120  friend U32ConstIterator endUInt32(const PtrUid& a) BOOST_NOEXCEPT
121  {
122  return end(a.digest);
123  }
124  };
125 
126 } // namespace qi
127 
128 // Hashable:
129 namespace std
130 {
131  template<>
132  struct hash<qi::PtrUid>
133  {
134  std::size_t operator()(const qi::PtrUid& p) const
135  {
136  static_assert(sizeof(std::size_t) >= sizeof(*beginUInt32(p)),
137  "A std::size_t must be large enough to store a uint32_t.");
138  return boost::hash_range(beginUInt32(p), endUInt32(p));
139  }
140  };
141 
142 } // namespace std
143 
144 #endif
friend U32ConstIterator endUInt32(const PtrUid &a) BOOST_NOEXCEPT
Definition: ptruid.hpp:120
#define QI_API
Definition: api.hpp:33
friend U32ConstIterator beginUInt32(const PtrUid &a) BOOST_NOEXCEPT
Definition: ptruid.hpp:115
friend uint8_t * end(PtrUid &a) BOOST_NOEXCEPT
Definition: ptruid.hpp:72
#define QI_ASSERT(expr__)
Definition: assert.hpp:27
const uint8_t & operator[](size_t n) const BOOST_NOEXCEPT
Return a const ref to allow the address of the element to be taken.
Definition: ptruid.hpp:98
dll import/export and compiler message
friend U32Iterator beginUInt32(PtrUid &a) BOOST_NOEXCEPT
Definition: ptruid.hpp:105
friend U32Iterator endUInt32(PtrUid &a) BOOST_NOEXCEPT
Definition: ptruid.hpp:110
friend const uint8_t * begin(const PtrUid &a) BOOST_NOEXCEPT
Definition: ptruid.hpp:77
uint8_t uint8_t
Definition: types.hpp:63
friend BOOST_CONSTEXPR bool empty(const PtrUid &a) BOOST_NOEXCEPT
Definition: ptruid.hpp:92
boost::uuids::uuid Uuid
Definition: uuid.hpp:12
friend const uint8_t * end(const PtrUid &a) BOOST_NOEXCEPT
Definition: ptruid.hpp:82
friend BOOST_CONSTEXPR size_t size(const PtrUid &) BOOST_NOEXCEPT
Definition: ptruid.hpp:87
std::size_t operator()(const qi::PtrUid &p) const
Definition: ptruid.hpp:134