Advertisement
kolbka_

Untitled

Jan 14th, 2022
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #ifndef UNIQUE_PTR_GUARD
  2. #define UNIQUE_PTR_GUARD
  3. #include <algorithm>
  4.  
  5. namespace ptrs::unique {
  6. template <typename T = int>
  7. struct default_deleter {
  8.     void operator()(T *ptr) {
  9.         delete ptr;
  10.     }
  11. };
  12. template <typename T, typename Deleter = struct default_deleter<T>>
  13. struct unique_ptr {
  14. private:
  15.     T *ptr_on_object;
  16.     Deleter my_deleter;
  17.  
  18. public:
  19.     unique_ptr(T *cur_ptr, Deleter tmp)
  20.         : ptr_on_object(std::move(cur_ptr)), my_deleter(std::move(tmp)) {
  21.     }
  22.     unique_ptr() : ptr_on_object(nullptr) {
  23.     }
  24.     explicit unique_ptr(T *cur_ptr) : ptr_on_object(std::move(cur_ptr)) {
  25.     }
  26.     explicit unique_ptr(const unique_ptr &other) = delete;
  27.     unique_ptr(unique_ptr &&other)
  28.         : ptr_on_object(std::exchange(other.ptr_on_object, nullptr)),
  29.           my_deleter(std::move(other.my_deleter)) {
  30.     }
  31.     unique_ptr &operator=(const unique_ptr &other) = delete;
  32.     unique_ptr &operator=(unique_ptr &&other) {
  33.         auto tmp = std::exchange(other.ptr_on_object, nullptr);
  34.         if (ptr_on_object != nullptr) {
  35.             my_deleter(ptr_on_object);
  36.         }
  37.         ptr_on_object = std::move(tmp);
  38.         my_deleter = std::move(other.my_deleter);
  39.         return *this;
  40.     }
  41.     ~unique_ptr() {
  42.         my_deleter(ptr_on_object);
  43.     };
  44.  
  45.     T *get() const {
  46.         return ptr_on_object;
  47.     }
  48.  
  49.     explicit operator bool() const {
  50.         return ptr_on_object != nullptr;
  51.     }
  52.  
  53.     friend void swap(unique_ptr<T, Deleter> &a, unique_ptr<T, Deleter> &b) {
  54.         std::swap(a.ptr_on_object, b.ptr_on_object);
  55.         std::swap(a.my_deleter, b.my_deleter);
  56.     }
  57.  
  58.     T *release() {
  59.         auto tmp = std::move(ptr_on_object);
  60.         ptr_on_object = nullptr;
  61.         return tmp;
  62.     }
  63.  
  64.     void reset() {
  65.         my_deleter(ptr_on_object);
  66.         ptr_on_object = nullptr;
  67.     }
  68.  
  69.     void reset(T * const other) {
  70.         my_deleter(ptr_on_object);
  71.         ptr_on_object = std::move(other);
  72.     }
  73.  
  74.     T &operator*() const {
  75.         return *get();
  76.     }
  77.  
  78.     T *operator->() const {
  79.         return get();
  80.     }
  81.  
  82.     bool operator==(const unique_ptr &other) const {
  83.         return ptr_on_object == other.ptr_on_object;
  84.     }
  85.  
  86.     bool operator!=(const unique_ptr &other) const {
  87.         return ptr_on_object != other.ptr_on_object;
  88.     }
  89. };
  90.  
  91. }  // namespace ptrs::unique
  92. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement