CommonLibSSE (Parapets fork)
BSTPointerAndFlags.h
Go to the documentation of this file.
1 #pragma once
2 
3 namespace RE
4 {
5  // 0x8
6  template <class T, std::uintptr_t MASK = 1>
8  {
9  public:
10  using value_type = T;
11  using element_type = typename T::element_type;
12 
13  constexpr BSTPointerAndFlags() noexcept = default;
14 
15  BSTPointerAndFlags(const BSTPointerAndFlags& a_rhs) { _storage.ptr = a_rhs.get(); }
16 
17  constexpr BSTPointerAndFlags(BSTPointerAndFlags&& a_rhs) noexcept
18  {
19  _storage.address = a_rhs._storage.address;
20  a_rhs._storage.address = 0;
21  }
22 
23  constexpr BSTPointerAndFlags(std::nullptr_t) noexcept {}
24 
26  {
27  clear_flags();
28  _storage.ptr.~value_type();
29  _storage.address = 0;
30  }
31 
33  {
34  if (this != std::addressof(a_rhs)) {
35  clear_flags();
36  _storage.ptr = a_rhs.get();
37  }
38  return *this;
39  }
40 
42  {
43  if (this != std::addressof(a_rhs)) {
44  clear_flags();
45  a_rhs.clear_flags();
46  _storage.address = a_rhs._storage.address;
47  a_rhs.storage.address = 0;
48  }
49  return *this;
50  }
51 
52  void reset() noexcept
53  {
54  clear_flags();
55  _storage.ptr.reset();
56  }
57 
58  [[nodiscard]] element_type* get() const noexcept
59  {
60  auto ptr = _storage.address;
61  return reinterpret_cast<element_type*>(ptr & ~FLAG_MASK);
62  }
63 
64  [[nodiscard]] element_type& operator*() const noexcept
65  {
66  assert(get() != nullptr);
67  return *get();
68  }
69 
70  [[nodiscard]] element_type* operator->() const noexcept { return get(); }
71 
72  [[nodiscard]] explicit operator bool() const noexcept { return get() != nullptr; }
73 
74  protected:
75  union Storage
76  {
77  ~Storage() noexcept {}
78 
80  std::uintptr_t address{ 0 };
81  };
82 
83  constexpr void clear_flags() noexcept { _storage.address &= ~FLAG_MASK; }
84 
85  static constexpr std::uintptr_t FLAG_MASK = MASK;
86 
87  // members
89  };
90 }
Definition: BSTPointerAndFlags.h:8
constexpr BSTPointerAndFlags(BSTPointerAndFlags &&a_rhs) noexcept
Definition: BSTPointerAndFlags.h:17
element_type * get() const noexcept
Definition: BSTPointerAndFlags.h:58
constexpr void clear_flags() noexcept
Definition: BSTPointerAndFlags.h:83
~BSTPointerAndFlags()
Definition: BSTPointerAndFlags.h:25
Storage _storage
Definition: BSTPointerAndFlags.h:88
constexpr BSTPointerAndFlags() noexcept=default
element_type & operator*() const noexcept
Definition: BSTPointerAndFlags.h:64
constexpr BSTPointerAndFlags(std::nullptr_t) noexcept
Definition: BSTPointerAndFlags.h:23
T value_type
Definition: BSTPointerAndFlags.h:10
typename T::element_type element_type
Definition: BSTPointerAndFlags.h:11
BSTPointerAndFlags & operator=(const BSTPointerAndFlags &a_rhs)
Definition: BSTPointerAndFlags.h:32
void reset() noexcept
Definition: BSTPointerAndFlags.h:52
BSTPointerAndFlags & operator=(BSTPointerAndFlags &&a_rhs) noexcept
Definition: BSTPointerAndFlags.h:41
element_type * operator->() const noexcept
Definition: BSTPointerAndFlags.h:70
static constexpr std::uintptr_t FLAG_MASK
Definition: BSTPointerAndFlags.h:85
Definition: AbsorbEffect.h:6
Definition: BSTPointerAndFlags.h:76
value_type ptr
Definition: BSTPointerAndFlags.h:79
std::uintptr_t address
Definition: BSTPointerAndFlags.h:80
~Storage() noexcept
Definition: BSTPointerAndFlags.h:77