libUTL++
PointerIntPair.h
1 #pragma once
2 
3 #include <cmath>
4 #include <type_traits>
5 
7 
8 UTL_NS_BEGIN;
9 
11 
22 
24 template <typename PtrT>
26 {
27  typedef typename std::remove_pointer<PtrT>::type T;
28  static constexpr int FreeBits = log2(alignof(T));
29  static auto getPointer(uintptr_t value)
30  {
31  return reinterpret_cast<PtrT>(value);
32  }
33 };
34 
36 
44 
47 {
48 public:
49  PointerIntPairBase(uintptr_t value)
50  : _value(value)
51  {
52  }
53 
54  PointerIntPairBase(const PointerIntPairBase&) = default;
55 
57  auto get() const
58  {
59  return _value;
60  }
61 
63  void
64  serialize(Stream& stream, uint_t io, uint_t mode = ser_default)
65  {
66  ABORT();
67  utl::serialize(_value, stream, io, mode);
68  }
69 
71 
72  inline bool operator<(const PointerIntPairBase& rhs) const
73  {
74  return _value < rhs._value;
75  }
76  inline bool operator<=(const PointerIntPairBase& rhs) const
77  {
78  return _value <= rhs._value;
79  }
80  inline bool operator==(const PointerIntPairBase& rhs) const
81  {
82  return _value == rhs._value;
83  }
84  inline bool operator>(const PointerIntPairBase& rhs) const
85  {
86  return _value > rhs._value;
87  }
88  inline bool operator>=(const PointerIntPairBase& rhs) const
89  {
90  return _value >= rhs._value;
91  }
93 
94 protected:
95  uintptr_t _value;
96 };
97 
99 
110 
112 template <typename PtrT,
113  int IntBits,
114  typename IntT = utl::uint_t,
115  typename PtrTraits = PointerLikeTraits<PtrT>>
117 {
118 public:
120  : PointerIntPairBase(0)
121  {
122  }
123 
124  PointerIntPair(uintptr_t value)
125  : PointerIntPairBase(value)
126  {
127  }
128 
129  PointerIntPair(const PointerIntPair&) = default;
130 
132  void
133  set(PtrT ptr, IntT val)
134  {
135  ASSERTD(((uintptr_t)ptr & PtrMask) == (uintptr_t)ptr);
136  ASSERTD(((uintptr_t)val & IntMask) == (uintptr_t)(val));
137  _value = (uintptr_t)ptr | (uintptr_t)(((intptr_t)val << IntShift));
138  }
139 
141 
142  auto getPointer() const
143  {
144  return PtrTraits::getPointer(_value & PtrMask);
145  }
146 
147  void
148  setPointer(PtrT ptr)
149  {
150  ASSERTD(((uintptr_t)ptr & PtrMask) == (uintptr_t)ptr);
151  _value = (_value & IntMask) | (uintptr_t)ptr;
152  }
153  //}
154 
156 
157  auto getInt() const
158  {
159  return (IntT)((_value >> IntShift) & IntMask);
160  }
161 
162  void
163  setInt(IntT val)
164  {
165  ASSERTD(((uintptr_t)val & IntMask) == (uintptr_t)(val));
166  _value = (_value & PtrMask) | (uintptr_t)(((intptr_t)val << IntShift));
167  }
169 
170 private:
171  static constexpr int IntShift = PtrTraits::FreeBits - IntBits;
172  static constexpr uintptr_t IntMask = (uintptr_t)(((intptr_t)1 << IntBits) - 1);
173  static constexpr uintptr_t PtrMask = ~(uintptr_t)(((intptr_t)1 << PtrTraits::FreeBits) - 1);
174 };
175 
177 
178 UTL_NS_END;
179 
181 
182 template <typename PtrT, int IntBits, typename IntT, typename PtrTraits>
183 inline bool operator==(const utl::PointerIntPair<PtrT, IntBits, IntT, PtrTraits>& lhs,
184  std::nullptr_t rhs)
185 {
186  return (lhs.getPointer() == nullptr);
187 }
188 
189 template <typename PtrT, int IntBits, typename IntT, typename PtrTraits>
190 inline bool operator==(std::nullptr_t lhs,
192 {
193  return (rhs.getPointer() == nullptr);
194 }
195 
196 template <typename PtrT, int IntBits, typename IntT, typename PtrTraits>
197 inline bool operator!=(const utl::PointerIntPair<PtrT, IntBits, IntT, PtrTraits>& lhs,
198  std::nullptr_t rhs)
199 {
200  return (lhs.getPointer() != nullptr);
201 }
202 
203 template <typename PtrT, int IntBits, typename IntT, typename PtrTraits>
204 inline bool operator!=(std::nullptr_t lhs,
206 {
207  return (rhs.getPointer() != nullptr);
208 }
Non-template base class for PointerIntPair.
void serialize(bool &b, Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize a boolean.
(Pointer,Int) pair.
default representation (via getSerializeMode())
Definition: util.h:75
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
#define ABORT()
Immediately terminates the program.
Definition: macros.h:59
Stream I/O abstraction.
Definition: Stream.h:68
void serialize(Stream &stream, uint_t io, uint_t mode=ser_default)
Don&#39;t try to serialize.
Pointer-like traits.
#define ASSERTD
Do an assertion in DEBUG mode only.