unique-pointer.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. namespace nall {
  3. template<typename T>
  4. struct unique_pointer {
  5. template<typename... P> static auto create(P&&... p) {
  6. return unique_pointer<T>{new T{forward<P>(p)...}};
  7. }
  8. using type = T;
  9. T* pointer = nullptr;
  10. function<void (T*)> deleter;
  11. unique_pointer(const unique_pointer&) = delete;
  12. auto operator=(const unique_pointer&) -> unique_pointer& = delete;
  13. unique_pointer(T* pointer = nullptr, const function<void (T*)>& deleter = {}) : pointer(pointer), deleter(deleter) {}
  14. ~unique_pointer() { reset(); }
  15. auto operator=(T* source) -> unique_pointer& {
  16. reset();
  17. pointer = source;
  18. return *this;
  19. }
  20. explicit operator bool() const { return pointer; }
  21. auto operator->() -> T* { return pointer; }
  22. auto operator->() const -> const T* { return pointer; }
  23. auto operator*() -> T& { return *pointer; }
  24. auto operator*() const -> const T& { return *pointer; }
  25. auto operator()() -> T& { return *pointer; }
  26. auto operator()() const -> const T& { return *pointer; }
  27. auto data() -> T* { return pointer; }
  28. auto data() const -> const T* { return pointer; }
  29. auto release() -> T* {
  30. auto result = pointer;
  31. pointer = nullptr;
  32. return result;
  33. }
  34. auto reset() -> void {
  35. if(pointer) {
  36. if(deleter) {
  37. deleter(pointer);
  38. } else {
  39. delete pointer;
  40. }
  41. pointer = nullptr;
  42. }
  43. }
  44. };
  45. template<typename T>
  46. struct unique_pointer<T[]> {
  47. using type = T;
  48. T* pointer = nullptr;
  49. function<auto (T*) -> void> deleter;
  50. unique_pointer(const unique_pointer&) = delete;
  51. auto operator=(const unique_pointer&) -> unique_pointer& = delete;
  52. unique_pointer(T* pointer = nullptr, const function<void (T*)>& deleter = {}) : pointer(pointer), deleter(deleter) {}
  53. ~unique_pointer() { reset(); }
  54. auto operator=(T* source) -> unique_pointer& {
  55. reset();
  56. pointer = source;
  57. return *this;
  58. }
  59. explicit operator bool() const { return pointer; }
  60. auto operator()() -> T* { return pointer; }
  61. auto operator()() const -> T* { return pointer; }
  62. alwaysinline auto operator[](uint offset) -> T& { return pointer[offset]; }
  63. alwaysinline auto operator[](uint offset) const -> const T& { return pointer[offset]; }
  64. auto data() -> T* { return pointer; }
  65. auto data() const -> const T* { return pointer; }
  66. auto release() -> T* {
  67. auto result = pointer;
  68. pointer = nullptr;
  69. return result;
  70. }
  71. auto reset() -> void {
  72. if(pointer) {
  73. if(deleter) {
  74. deleter(pointer);
  75. } else {
  76. delete[] pointer;
  77. }
  78. pointer = nullptr;
  79. }
  80. }
  81. };
  82. }