kolbka_

Untitled

Jan 29th, 2022
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #ifndef SHARED_PTR_GUARD
  2. #define SHARED_PTR_GUARD
  3. #include <algorithm>
  4. #include <iostream>
  5. namespace ptrs::shared {
  6.  
  7. template <typename T>
  8. struct shared_ptr {
  9. private:
  10.     T *ptr_on_object = nullptr;
  11.     int *use_count = nullptr;
  12.     void delete_data() {
  13.         if (use_count == nullptr) {
  14.             return;
  15.         }
  16.         if (*use_count == 1) {
  17.             delete use_count;
  18.             delete ptr_on_object;
  19.         } else {
  20.             --*use_count;
  21.         }
  22.     }
  23.  
  24. public:
  25.     // invariant: if user give ptr == nullptr, then counter=nullptr, else
  26.     // counter=1
  27.     shared_ptr() = default;
  28.     explicit shared_ptr(T *cur_ptr)
  29.         : ptr_on_object(std::exchange(cur_ptr, nullptr)),
  30.           use_count(new int{1}) {
  31.     }
  32.     explicit shared_ptr(std::nullptr_t) {
  33.     }
  34.     shared_ptr(const shared_ptr &other)
  35.         : ptr_on_object(other.ptr_on_object),
  36.           // cppcheck-suppress copyCtorPointerCopying
  37.           use_count(other.use_count) {
  38.         if (use_count != nullptr) {
  39.             ++*use_count;
  40.         }
  41.     }
  42.     shared_ptr(shared_ptr &&other)
  43.         : ptr_on_object(std::exchange(other.ptr_on_object, nullptr)),
  44.           use_count(std::exchange(other.use_count, nullptr)) {
  45.     }
  46.     shared_ptr &operator=(const shared_ptr &other) {
  47.         if (other.ptr_on_object == ptr_on_object) {
  48.             return *this;
  49.         }
  50.         delete_data();
  51.         ptr_on_object = other.ptr_on_object;
  52.         use_count = other.use_count;
  53.         if (use_count != nullptr) {
  54.             ++*use_count;
  55.         }
  56.         return *this;
  57.     }
  58.     shared_ptr &operator=(shared_ptr &&other) {
  59.         if (other.ptr_on_object == ptr_on_object) {
  60.             return *this;
  61.         }
  62.         delete_data();
  63.         ptr_on_object = std::exchange(other.ptr_on_object, nullptr);
  64.         use_count = std::exchange(other.use_count, nullptr);
  65.         return *this;
  66.     }
  67.     ~shared_ptr() {
  68.         delete_data();
  69.     }
  70.  
  71.     [[nodiscard]] T *get() const {
  72.         return ptr_on_object;
  73.     }
  74.  
  75.     explicit operator bool() const {
  76.         return ptr_on_object != nullptr;
  77.     }
  78.  
  79.     friend void swap(shared_ptr<T> &a, shared_ptr<T> &b) {
  80.         std::swap(a.ptr_on_object, b.ptr_on_object);
  81.         std::swap(a.use_count, b.use_count);
  82.     }
  83.  
  84.     [[nodiscard]] T *release() {
  85.         if (use_count != nullptr) {
  86.             --*use_count;
  87.         }
  88.         use_count = nullptr;
  89.         return std::exchange(ptr_on_object, nullptr);
  90.     }
  91.  
  92.     void reset() {
  93.         delete_data();
  94.         use_count = nullptr;
  95.         ptr_on_object = nullptr;
  96.     }
  97.  
  98.     void reset(T *other) {
  99.         delete_data();
  100.         if (other != nullptr) {
  101.             use_count = new int{1};
  102.         } else {
  103.             use_count = nullptr;
  104.         }
  105.         ptr_on_object = std::exchange(other, nullptr);
  106.     }
  107.     [[nodiscard]] T &operator*() const {
  108.         return *get();
  109.     }
  110.  
  111.     [[nodiscard]] T *operator->() const {
  112.         return get();
  113.     }
  114.  
  115.     [[nodiscard]] bool operator==(const shared_ptr &other) const {
  116.         return ptr_on_object == other.ptr_on_object;
  117.     }
  118.  
  119.     [[nodiscard]] bool operator!=(const shared_ptr &other) const {
  120.         return ptr_on_object != other.ptr_on_object;
  121.     }
  122. };
  123.  
  124. }  // namespace ptrs::shared
  125. #endif
Advertisement
Add Comment
Please, Sign In to add comment