Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. #include <iostream>
  2. //#include <memory.h>
  3. #define DEBUG
  4.  
  5. using namespace std;
  6.  
  7. template <typename Derived>
  8. class RelOps {
  9. public:
  10.     friend bool operator > (const Derived & left, const Derived &right) {
  11.         return right < left;
  12.     }
  13.     friend bool operator <= (const Derived & left, const Derived &right) {
  14.         return !(right < left);
  15.     }
  16.     friend bool operator >= (const Derived & left, const Derived &right) {
  17.         return right <= left;
  18.     }
  19.     friend bool operator == (const Derived & left, const Derived &right) {
  20.         return left <= right && left >= right;
  21.     }
  22.     friend bool operator != (const Derived & left, const Derived &right) {
  23.         !(left == right);
  24.     }
  25. };
  26.  
  27. class Standard_deleter {
  28. public:
  29.     Standard_deleter() {}
  30.     template <class T>
  31.     void operator()(T* p) {
  32.         delete p;
  33.     }
  34. };
  35.  
  36. template <typename T, typename Deleter = Standard_deleter>
  37. struct Proxy {
  38.     T* ptr;
  39.     Deleter dltr;
  40.     int ref_cnt = 0;
  41.     Proxy() {
  42.     };
  43.    
  44.     Proxy(T* ptr) {
  45.         dltr = Deleter();
  46.         ref_cnt++;
  47.         this->ptr = ptr;
  48.         #ifdef DEBUG
  49.         cout << *(this->ptr) << endl;
  50.         #endif
  51.     };
  52.     ~Proxy() {
  53.         dltr(ptr);
  54.     };
  55. };
  56.  
  57. template <typename T>
  58. class Shared_ptr_concept {
  59. public:
  60.     Proxy<T>* proxy;
  61.     virtual ~Shared_ptr_concept() = default;
  62.     virtual bool operator < (const Shared_ptr_concept& other) const = 0;
  63.     virtual explicit operator bool() const = 0;
  64.     virtual void swap(Shared_ptr_concept& other) noexcept = 0;
  65.     virtual T* operator->()  = 0;
  66.     virtual T& operator*() = 0;
  67.     virtual Shared_ptr_concept& operator = (Shared_ptr_concept&& other) noexcept = 0;
  68.     virtual Shared_ptr_concept& operator = (Shared_ptr_concept& other) noexcept = 0;
  69. };
  70.  
  71. template <typename T>
  72. class Shared_ptr_model: public Shared_ptr_concept<T>, RelOps<Shared_ptr_model<T>> {
  73. public:
  74.     Proxy<T>* proxy;
  75.     explicit  Shared_ptr_model(T* obj) {
  76.         proxy = new Proxy<T>(obj);
  77.         #ifdef DEBUG
  78.         cout << "shared_ptr_model construcetor complete. object:" << *obj << endl;
  79.         cout << *(proxy->ptr) << endl;
  80.         #endif
  81.     }
  82.     ~Shared_ptr_model() {
  83.         proxy->ref_cnt--;
  84.         if (proxy->ref_cnt == 0)
  85.             delete proxy;
  86.     }
  87.     virtual bool operator < (const Shared_ptr_concept<T>& other) const override {
  88.         return proxy->ptr < other.proxy->ptr;
  89.     }
  90.     explicit operator bool() const {
  91.         return proxy->ptr != 0;
  92.     }
  93.     virtual void swap(Shared_ptr_concept<T>& other) noexcept override{
  94.         std::swap(proxy->ptr, other.proxy->ptr);
  95.     }
  96.     T* operator->() { return proxy->ptr; }
  97.     T& operator* () { return *(proxy->ptr); }
  98.     Shared_ptr_model& operator = (Shared_ptr_concept<T>&& other) noexcept override{
  99.         swap(other);
  100.         other.~Shared_ptr_concept();//seriously
  101.         return *this;
  102.     }
  103.     virtual Shared_ptr_model& operator = (Shared_ptr_concept<T>& other) noexcept override{
  104.         proxy = other.proxy;
  105.         proxy->ref_cnt++;
  106.         return *this;
  107.     }
  108. };
  109.  
  110. template <typename T>
  111. class Shared_ptr {
  112. private:
  113.     std::unique_ptr<Shared_ptr_concept<T>> object_ptr;
  114. public:
  115.     Shared_ptr(T* obj):object_ptr(new Shared_ptr_model<T>(obj)) {
  116.         #ifdef DEBUG
  117.         cout << "shared_ptr constructor comlete" << endl;
  118.         cout << *(object_ptr->proxy->ptr) << endl;
  119.         #endif
  120.     };
  121.     bool operator < (const Shared_ptr_concept<T>& other) const {
  122.         return object_ptr->operator < (other);
  123. //        return object_ptr->proxy->ptr < other->proxy->ptr;
  124. //        ???? опять не понятно как < вызвать
  125.     }
  126.     explicit operator bool() const {
  127. //        return object_ptr->;
  128. //        ???? непонятно
  129.     }
  130.     void swap(Shared_ptr_concept<T>& other) noexcept {
  131.         return object_ptr->swap(other);
  132.     }
  133.     T* operator ->() {
  134. //        return object_ptr->->;
  135. //    ???? непонятно
  136.     }
  137.     T& operator*() {
  138.         return *(object_ptr->proxy->ptr);
  139. //        return (object_ptr->*);
  140. //    ???? непонятно
  141.     }
  142.     Shared_ptr_concept<T>& operator = (Shared_ptr_concept<T>& other) noexcept {
  143. //        object_ptr->= other;
  144.     }
  145. };
  146.  
  147.  
  148.  
  149. int main() {
  150.     int* a = new int(44);
  151.     Shared_ptr<int> sh_p(a);
  152.     cout << *sh_p << endl;
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement