Guest User

Untitled

a guest
Oct 17th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <cstddef>
  5.  
  6. template <typename T>
  7. class TSharedPtr {
  8. public:
  9.     TSharedPtr():
  10.         ptr_(nullptr),
  11.         cnt_(nullptr)
  12.     {}
  13.  
  14.     TSharedPtr(T* ptr):
  15.         ptr_(ptr),
  16.         cnt_(new size_t(1))
  17.     {}
  18.  
  19.     TSharedPtr(const TSharedPtr &other):
  20.         ptr_(nullptr),
  21.         cnt_(nullptr)
  22.     {
  23.         ptr_ = other.ptr_;
  24.         cnt_ = other.cnt_;
  25.  
  26.         if (cnt_) {
  27.             ++*cnt_;
  28.         }
  29.     }
  30.  
  31.     ~TSharedPtr() {
  32.         if (!cnt_) {
  33.             return;
  34.         }
  35.         --*cnt_;
  36.         if (*cnt_) {
  37.             return;
  38.         }
  39.         delete cnt_;
  40.         delete ptr_;
  41.     }
  42.  
  43.     TSharedPtr& operator = (const TSharedPtr &other) {
  44.         if (this == &other) {
  45.             return *this;
  46.         }
  47.  
  48.         TSharedPtr tmp(*this);
  49.  
  50.         if (cnt_) {
  51.             --*cnt_;
  52.             ptr_ = nullptr;
  53.         }
  54.  
  55.         ptr_ = other.ptr_;
  56.         cnt_ = other.cnt_;
  57.         if (cnt_) {
  58.             ++*cnt_;
  59.         }
  60.  
  61.         return *this;
  62.     }
  63.  
  64.     T& operator * () {
  65.         return *ptr_;
  66.     }
  67.  
  68.     const T& operator * () const {
  69.         return *ptr_;
  70.     }
  71.  
  72.     T* operator -> () {
  73.         return ptr_;
  74.     }
  75.  
  76.     const T* operator -> () const {
  77.         return ptr_;
  78.     }
  79.  
  80.     T* Get() {
  81.         return ptr_;
  82.     }
  83.  
  84.     const T* Get() const {
  85.         return ptr_;
  86.     }
  87.  
  88.     size_t* Count() const {
  89.         return cnt_;
  90.     }
  91.  
  92.     operator bool () const {
  93.         return ptr_;
  94.     }
  95. private:
  96.     T* ptr_;
  97.     size_t* cnt_;
  98. };
  99.  
  100. template <typename T>
  101. bool Equal(const T& left, const T& right, std::string name) {
  102.     if (left == right) {
  103.         std::cout << name << " OK: " << left << " == " << right << std::endl;
  104.         return true;
  105.     } else {
  106.         std::cout << name << " FAIL: " << left << " == " << right << std::endl;
  107.         return false;
  108.     }
  109. }
  110.  
  111. bool test() {
  112.     bool ok = true;
  113.     {
  114.         TSharedPtr<int> ptr;
  115.         ok &= Equal<int *>(ptr.Get(), nullptr, "default constructor ptr");
  116.         ok &= Equal<size_t *>(ptr.Count(), nullptr, "default constructor cnt");
  117.     }
  118.     {
  119.         struct CheckDestruct {
  120.             CheckDestruct(bool &destruct):
  121.                 destruct_(destruct)
  122.             {}
  123.             ~CheckDestruct() {
  124.                 destruct_ = true;
  125.             }
  126.             bool &destruct_;
  127.         };
  128.         bool destruct = false;
  129.         {
  130.             TSharedPtr<CheckDestruct> ptr(new CheckDestruct(destruct));
  131.         }
  132.         ok &= Equal<bool>(destruct, true, "default destructor");
  133.     }
  134.     return ok;
  135. }
  136.  
  137. int main(int argc, char **argv) {
  138.     if (argc == 2 && !strcmp(argv[1], "--test")) {
  139.         return !test();
  140.     }
  141. }
  142.  
  143. struct Uncopyable {
  144.     Uncopyable(const Uncopyable &) = delete;
  145.     Uncopyable& operator = (const Uncopyable &) = delete;
  146. };
  147.  
  148.  
  149. class MyClass {
  150.     ...
  151. };
  152.  
  153. typedef std::unique_ptr<MyClass> MyClassRef;
Advertisement
Add Comment
Please, Sign In to add comment