summaryrefslogtreecommitdiffhomepage
path: root/Private/NewKit/OwnPtr.hpp
blob: e6d006ed0f2e7172e9299c999cfe1e5314977f67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

/* -------------------------------------------

    Copyright Mahrouss Logic

------------------------------------------- */

#pragma once

#include <NewKit/Defines.hpp>
#include <NewKit/KernelCheck.hpp>
#include <NewKit/Ref.hpp>

namespace NewOS {
template <typename T>
class OwnPtr;

template <typename T>
class NonNullRefPtr;

template <typename T>
class OwnPtr final {
 public:
  OwnPtr() {}
  ~OwnPtr() { this->Delete(); }

  OwnPtr &operator=(const OwnPtr &) = default;
  OwnPtr(const OwnPtr &) = default;

 public:
  template <typename... Args>
  bool New(Args &&...arg) {
    if (m_Cls) {
      return false;
    }

    m_Cls = new T(arg...);
    return m_Cls;
  }

  void Delete() {
    if (m_Cls) delete m_Cls;

    m_Cls = nullptr;
  }

  T *operator->() const { return m_Cls; };
  T *Raw() { return m_Cls; }

  Ref<T> AsRef() { return Ref<T>(m_Cls); }

  operator bool() { return m_Cls; }
  bool operator!() { return !m_Cls; }

 private:
  T *m_Cls;
};

template <typename T, typename... Args>
OwnPtr<T> make_ptr(Args... args) {
  OwnPtr<T> ret;
  ret.template New<Args...>(forward(args)...);
  MUST_PASS(ret);

  return ret;
}
}  // namespace NewOS