CommonLibSSE (powerof3)
BSPointerHandle.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "RE/N/NiSmartPointer.h"
4 
5 namespace RE
6 {
7  class Actor;
8  class HandleManager;
9  class Projectile;
10  class TESObjectREFR;
11 
12  template <std::uint32_t = 21, std::uint32_t = 5>
13  class BSUntypedPointerHandle;
14 
15  template <class, class = BSUntypedPointerHandle<>>
16  class BSPointerHandle;
17 
18  template <class, class = HandleManager>
19  class BSPointerHandleManagerInterface;
20 
21  template <std::uint32_t FREE_LIST_BITS, std::uint32_t AGE_SHIFT>
23  {
24  public:
25  using value_type = std::uint32_t;
26 
27  enum : std::uint32_t
28  {
29  kFreeListBits = FREE_LIST_BITS,
30  kAgeShift = AGE_SHIFT,
31  };
32 
33  BSUntypedPointerHandle() noexcept = default;
34  BSUntypedPointerHandle(const BSUntypedPointerHandle&) noexcept = default;
35 
37  _handle(a_rhs._handle)
38  {
39  a_rhs.reset();
40  }
41 
42  explicit BSUntypedPointerHandle(value_type a_handle) noexcept :
43  _handle(a_handle)
44  {}
45 
46  ~BSUntypedPointerHandle() noexcept { reset(); }
47 
49 
51  {
52  if (this != std::addressof(a_rhs)) {
53  _handle = a_rhs._handle;
54  a_rhs.reset();
55  }
56  return *this;
57  }
58 
60  {
61  _handle = a_rhs;
62  return *this;
63  }
64 
65  [[nodiscard]] explicit operator bool() const noexcept { return has_value(); }
66  [[nodiscard]] bool has_value() const noexcept { return _handle != 0; }
67 
68  [[nodiscard]] value_type value() const noexcept { return _handle; }
69 
70  void reset() noexcept { _handle = 0; }
71 
72  [[nodiscard]] friend bool operator==(const BSUntypedPointerHandle& a_lhs, const BSUntypedPointerHandle& a_rhs) noexcept
73  {
74  return a_lhs.value() == a_rhs.value();
75  }
76 
77  [[nodiscard]] friend bool operator!=(const BSUntypedPointerHandle& a_lhs, const BSUntypedPointerHandle& a_rhs) noexcept
78  {
79  return !(a_lhs == a_rhs);
80  }
81 
82  private:
83  // members
84  value_type _handle{ 0 }; // 0
85  };
86 
87  extern template class BSUntypedPointerHandle<>;
88 
89  template <class T, class Handle>
91  {
92  public:
93  using native_handle_type = typename Handle::value_type;
94 
95  BSPointerHandle() noexcept = default;
96  BSPointerHandle(const BSPointerHandle&) noexcept = default;
97  BSPointerHandle(BSPointerHandle&&) noexcept = default;
98 
99  template <
100  class Y,
101  std::enable_if_t<
102  std::is_convertible_v<
103  Y*,
104  T*>,
105  int> = 0>
106  explicit BSPointerHandle(Y* a_rhs)
107  {
108  get_handle(a_rhs);
109  }
110 
111  template <
112  class Y,
113  std::enable_if_t<
114  std::is_convertible_v<
115  Y*,
116  T*>,
117  int> = 0>
119  _handle(a_rhs._handle)
120  {}
121 
122  BSPointerHandle& operator=(const BSPointerHandle&) noexcept = default;
123  BSPointerHandle& operator=(BSPointerHandle&&) noexcept = default;
124 
125  template <
126  class Y,
127  std::enable_if_t<
128  std::is_convertible_v<
129  Y*,
130  T*>,
131  int> = 0>
132  BSPointerHandle& operator=(Y* a_rhs)
133  {
134  get_handle(a_rhs);
135  return *this;
136  }
137 
138  template <
139  class Y,
140  std::enable_if_t<
141  std::is_convertible_v<
142  Y*,
143  T*>,
144  int> = 0>
146  {
147  _handle = a_rhs._handle;
148  return *this;
149  }
150 
151  ~BSPointerHandle() noexcept = default;
152 
153  void reset() noexcept { _handle.reset(); }
154 
155  [[nodiscard]] NiPointer<T> get() const
156  {
157  NiPointer<T> ptr;
158  get_smartptr(ptr);
159  return ptr;
160  }
161 
162  [[nodiscard]] native_handle_type native_handle() const noexcept { return _handle.value(); }
163 
164  [[nodiscard]] explicit operator bool() const noexcept { return _handle.has_value(); }
165 
166  [[nodiscard]] friend bool operator==(const BSPointerHandle& a_lhs, const BSPointerHandle& a_rhs) noexcept
167  {
168  return a_lhs._handle == a_rhs._handle;
169  }
170 
171  [[nodiscard]] friend bool operator!=(const BSPointerHandle& a_lhs, const BSPointerHandle& a_rhs) noexcept
172  {
173  return !(a_lhs == a_rhs);
174  }
175 
176  private:
177  template <class, class>
178  friend class BSPointerHandle;
179 
180  void get_handle(T* a_ptr);
181  bool get_smartptr(NiPointer<T>& a_smartPointerOut) const;
182 
183  Handle _handle;
184  };
185 
186  extern template class BSPointerHandle<Actor>;
187  extern template class BSPointerHandle<Projectile>;
188  extern template class BSPointerHandle<TESObjectREFR>;
189 
193 
194  template <class T>
196  {
197  public:
198  [[nodiscard]] std::uint32_t operator()(const BSPointerHandle<T>& a_handle) const noexcept
199  {
200  return BSCRC32<typename BSPointerHandle<T>::native_handle_type>()(a_handle.native_handle());
201  }
202  };
203 
204  template <class T, class Manager>
206  {
207  public:
208  using value_type = T;
209 
210  static BSPointerHandle<T> GetHandle(T* a_ptr)
211  {
213  static REL::Relocation<func_t> func{ RELOCATION_ID(15967, 16212) };
214  return func(a_ptr);
215  }
216 
217  static bool GetSmartPointer(const BSPointerHandle<T>& a_handle, NiPointer<T>& a_smartPointerOut)
218  {
220  static REL::Relocation<func_t> func{ RELOCATION_ID(12204, 12332) };
221  return func(a_handle, a_smartPointerOut);
222  }
223  };
224 
225  extern template class BSPointerHandleManagerInterface<Actor>;
226  extern template class BSPointerHandleManagerInterface<Projectile>;
227  extern template class BSPointerHandleManagerInterface<TESObjectREFR>;
228 
229  template <class T, class Handle>
230  void BSPointerHandle<T, Handle>::get_handle(T* a_ptr)
231  {
233  }
234 
235  template <class T, class Handle>
236  bool BSPointerHandle<T, Handle>::get_smartptr(NiPointer<T>& a_smartPointerOut) const
237  {
238  return BSPointerHandleManagerInterface<T>::GetSmartPointer(*this, a_smartPointerOut);
239  }
240 }
#define RELOCATION_ID(SE, AE)
Definition: PCH.h:505
Definition: Relocation.h:210
Definition: BSPointerHandle.h:206
static bool GetSmartPointer(const BSPointerHandle< T > &a_handle, NiPointer< T > &a_smartPointerOut)
Definition: BSPointerHandle.h:217
static BSPointerHandle< T > GetHandle(T *a_ptr)
Definition: BSPointerHandle.h:210
T value_type
Definition: BSPointerHandle.h:208
Definition: BSPointerHandle.h:91
BSPointerHandle & operator=(const BSPointerHandle< Y, Handle > &a_rhs) noexcept
Definition: BSPointerHandle.h:145
BSPointerHandle & operator=(BSPointerHandle &&) noexcept=default
NiPointer< T > get() const
Definition: BSPointerHandle.h:155
friend bool operator==(const BSPointerHandle &a_lhs, const BSPointerHandle &a_rhs) noexcept
Definition: BSPointerHandle.h:166
~BSPointerHandle() noexcept=default
BSPointerHandle & operator=(const BSPointerHandle &) noexcept=default
native_handle_type native_handle() const noexcept
Definition: BSPointerHandle.h:162
friend bool operator!=(const BSPointerHandle &a_lhs, const BSPointerHandle &a_rhs) noexcept
Definition: BSPointerHandle.h:171
typename Handle::value_type native_handle_type
Definition: BSPointerHandle.h:93
BSPointerHandle() noexcept=default
BSPointerHandle(const BSPointerHandle< Y, Handle > &a_rhs) noexcept
Definition: BSPointerHandle.h:118
Definition: BSPointerHandle.h:23
friend bool operator==(const BSUntypedPointerHandle &a_lhs, const BSUntypedPointerHandle &a_rhs) noexcept
Definition: BSPointerHandle.h:72
~BSUntypedPointerHandle() noexcept
Definition: BSPointerHandle.h:46
friend bool operator!=(const BSUntypedPointerHandle &a_lhs, const BSUntypedPointerHandle &a_rhs) noexcept
Definition: BSPointerHandle.h:77
bool has_value() const noexcept
Definition: BSPointerHandle.h:66
value_type value() const noexcept
Definition: BSPointerHandle.h:68
BSUntypedPointerHandle() noexcept=default
BSUntypedPointerHandle & operator=(BSUntypedPointerHandle &&a_rhs) noexcept
Definition: BSPointerHandle.h:50
@ kAgeShift
Definition: BSPointerHandle.h:30
@ kFreeListBits
Definition: BSPointerHandle.h:29
BSUntypedPointerHandle & operator=(value_type a_rhs) noexcept
Definition: BSPointerHandle.h:59
BSUntypedPointerHandle & operator=(const BSUntypedPointerHandle &) noexcept=default
BSUntypedPointerHandle(value_type a_handle) noexcept
Definition: BSPointerHandle.h:42
void reset() noexcept
Definition: BSPointerHandle.h:70
std::uint32_t value_type
Definition: BSPointerHandle.h:25
Definition: NiSmartPointer.h:9
Definition: AbsorbEffect.h:6
Definition: EffectArchetypes.h:65
std::uint32_t operator()(const BSPointerHandle< T > &a_handle) const noexcept
Definition: BSPointerHandle.h:198
Definition: CRC.h:104