Chatterino
NullablePtr.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <type_traits>
4 
5 namespace chatterino {
6 
7 template <typename T>
9 {
10 public:
12  : element_(nullptr)
13  {
14  }
15 
16  NullablePtr(T *element)
17  : element_(element)
18  {
19  }
20 
21  T *operator->() const
22  {
23  assert(this->hasElement());
24 
25  return element_;
26  }
27 
28  typename std::add_lvalue_reference<T>::type operator*() const
29  {
30  assert(this->hasElement());
31 
32  return *element_;
33  }
34 
35  T *get() const
36  {
37  assert(this->hasElement());
38 
39  return this->element_;
40  }
41 
42  bool isNull() const
43  {
44  return this->element_ == nullptr;
45  }
46 
47  bool hasElement() const
48  {
49  return this->element_ != nullptr;
50  }
51 
52  operator bool() const
53  {
54  return this->hasElement();
55  }
56 
57  bool operator!() const
58  {
59  return !this->hasElement();
60  }
61 
62  template <typename X = T,
63  typename = std::enable_if_t<!std::is_const<X>::value>>
64  operator NullablePtr<const T>() const
65  {
66  return NullablePtr<const T>(this->element_);
67  }
68 
69 private:
70  T *element_;
71 };
72 
73 } // namespace chatterino
NullablePtr()
Definition: NullablePtr.hpp:11
Definition: Application.cpp:48
T * operator->() const
Definition: NullablePtr.hpp:21
Definition: NullablePtr.hpp:8
bool operator!() const
Definition: NullablePtr.hpp:57
bool hasElement() const
Definition: NullablePtr.hpp:47
NullablePtr(T *element)
Definition: NullablePtr.hpp:16
std::add_lvalue_reference< T >::type operator*() const
Definition: NullablePtr.hpp:28
bool isNull() const
Definition: NullablePtr.hpp:42