UniquePointer.hpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef CORE_UNIQUE_POINTER_HPP
  2. #define CORE_UNIQUE_POINTER_HPP
  3. namespace Core {
  4. template<typename T>
  5. class UniquePointer final {
  6. T* t;
  7. public:
  8. UniquePointer(T* t_) : t(t_) {
  9. }
  10. UniquePointer() : UniquePointer(nullptr) {
  11. }
  12. ~UniquePointer() {
  13. delete t;
  14. }
  15. UniquePointer(const UniquePointer&) = delete;
  16. UniquePointer& operator=(const UniquePointer&) = delete;
  17. UniquePointer(UniquePointer&& other) : UniquePointer(other.t) {
  18. other.t = nullptr;
  19. }
  20. UniquePointer& operator=(UniquePointer&& other) {
  21. if(this != &other) {
  22. delete t;
  23. t = other.t;
  24. other.t = nullptr;
  25. }
  26. return *this;
  27. }
  28. T* operator->() {
  29. return t;
  30. }
  31. const T* operator->() const {
  32. return t;
  33. }
  34. operator T*() {
  35. return t;
  36. }
  37. operator const T*() const {
  38. return t;
  39. }
  40. };
  41. }
  42. #endif