Advertisement
soandos

Untitled

Nov 29th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 KB | None | 0 0
  1. #include<atomic>
  2. #include<iostream>
  3. namespace cs540
  4. {
  5.     static int refcount2;
  6.     void dummy(void * a)
  7.     {
  8.     }
  9.     template <class T> void DefaultDeleter(void* p)
  10.     { delete static_cast<T*>(p);}
  11.    
  12.     template<typename T>
  13.     class Sptr
  14.     {
  15.         public:
  16.         /*template<typename U, typename V>
  17.         Sptr(Sptr<U>* op, V deleterFunction)
  18.         {
  19.             if(op!=nullptr)
  20.             {
  21.                 refcount = op->refcount;
  22.                 refcount->fetch_add(1);
  23.                 d = deleterFunction;
  24.                 if(dynamic_cast<T*>(op))
  25.                     obj=dynamic_cast<T*>(op);
  26.             }
  27.             else
  28.             {
  29.                 refcount=new std::atomic<int>(1);
  30.                 refcount2++;
  31.                 d = &dummy;
  32.                 obj = nullptr;
  33.             }
  34.             std::cout<<"template<typename U, typename V>Sptr(Sptr<U>* op, V deleterFunction)"<<std::endl;
  35.         }*/
  36.         template<typename U, typename V>
  37.         Sptr(Sptr<U>* op, V deleterFunction)
  38.         {
  39.             if(op!=nullptr && op->obj!=nullptr)
  40.             {
  41.                 /*if(refcount->fetch_sub(1)-1==0)
  42.                 {
  43.                     d(obj);
  44.                     obj = nullptr;
  45.                     refcount = nullptr;
  46.                     refcount2--;
  47.                 }*/
  48.                 refcount2++;
  49.                 refcount = op->refcount;
  50.                 refcount->fetch_add(1);
  51.                 d = deleterFunction;
  52.                 //if(dynamic_cast<T*>(op))
  53.                     //obj=dynamic_cast<T*>(op);
  54.                 obj = op->obj;
  55.             }
  56.             else
  57.             {
  58.                 refcount=new std::atomic<int>(1);
  59.                 refcount2++;
  60.                 d = &dummy;
  61.                 obj = nullptr;
  62.             }
  63.             std::cout<<"template<typename T, typename V>Sptr(Sptr<T>* op, V deleterFunction)"<<std::endl;
  64.         }
  65.         public:
  66.         void (*d)(void *);
  67.         T* obj;
  68.         std::atomic<int>* refcount;
  69.     //  Sptr(const Sptr&) = delete;
  70.         template<typename U>
  71.         //Sptr(const Sptr<U>&) = delete;
  72.         Sptr() = delete;
  73.        
  74.         Sptr(const Sptr &o): Sptr(const_cast<Sptr*>(&o),o.d)
  75.         {
  76.             std::cout<<"Non-templated"<<std::endl;
  77.         }
  78.        
  79.         Sptr()
  80.         {
  81.             refcount2++;
  82.             refcount = new std::atomic<int>(1);
  83.             d = &dummy;
  84.             obj = nullptr;
  85.         }
  86.         /*
  87.         template<typename U>
  88.         Sptr(const Sptr<U>& a)
  89.         {
  90.             T* b = reinterpret_cast<T*>(a.obj);
  91.             if(b!=nullptr)
  92.             {
  93.                 this->obj = b;
  94.                 this->refcount = a.refcount;
  95.                 refcount->fetch_add(1);
  96.                 this->d = a.d;
  97.             }
  98.             std::cout<<"template<typename U> Sptr(const Sptr<U>& a)"<<std::endl;
  99.         }*/
  100.         Sptr(T* obj)
  101.         {  
  102.             refcount2++;
  103.             refcount = new std::atomic<int>(1);
  104.             this->obj = obj;
  105.             d = &DefaultDeleter<T>;
  106.             std::cout<<"Default ctor. this is a test message"<<std::endl;
  107.         }
  108.         template<typename U>
  109.         Sptr(U* obj)
  110.         {
  111.             this->obj = obj;
  112.             refcount2++;
  113.             refcount = new std::atomic<int>(1);
  114.             d = &DefaultDeleter<U>;
  115.             std::cout<<"Sptr<U> ctor"<<std::endl;
  116.         }
  117.         template <typename U> Sptr &operator=(const Sptr<U> & a)
  118.         {
  119.             if((long)this !=(long)&a)
  120.             {
  121.                 if(this->obj == nullptr)
  122.                 {
  123.                     delete refcount;
  124.                 }
  125.                 else if(refcount->fetch_sub(1)-1==0)
  126.                 {
  127.                     d(obj);
  128.                     delete refcount;   
  129.                 }
  130.                 this->obj = a.obj;
  131.                 this->refcount = a.refcount;
  132.                 this->refcount->fetch_add(1);
  133.                 this->d = a.d; 
  134.             }
  135.             std::cout<<"template <typename U> Sptr &operator=(const Sptr<U> & a)"<<std::endl;
  136.             return *this;
  137.         }
  138.         Sptr &operator=(const Sptr a)
  139.         {
  140.             if((long)this !=(long)&a)
  141.             {
  142.                 int q = refcount->fetch_sub(0);
  143.                 if(this->obj == nullptr)
  144.                 {
  145.                     delete refcount;   
  146.                 }
  147.                 else if(refcount->fetch_sub(1)-1==0)
  148.                 {
  149.                     d(obj);
  150.                     delete refcount;   
  151.                 }
  152.                
  153.                 this->obj = a.obj;
  154.                 this->refcount = a.refcount;
  155.                 this->refcount->fetch_add(1);
  156.                 this->d = a.d;
  157.             }
  158.             std::cout<<"Sptr &operator=(const Sptr & a)"<<std::endl;
  159.             return *this;
  160.         }
  161.         void reset()
  162.         {
  163.             if(refcount->fetch_sub(1)-1==0)
  164.             {
  165.                 //delete obj;
  166.                 d(obj);
  167.                 //free(obj);
  168.             }
  169.             obj = nullptr;
  170.         }
  171.         T &operator*() const
  172.         {
  173.             return *obj;
  174.         }
  175.         T *operator->() const
  176.         {
  177.             return obj;
  178.         }
  179.         T *get() const
  180.         {
  181.             return obj;
  182.         }
  183.         explicit operator bool() const
  184.         {
  185.             std::cout<<"bool"<<std::endl;
  186.             return obj!=nullptr;
  187.         }
  188.         ~Sptr()
  189.         {
  190.             if(*refcount<1 || refcount2<1)
  191.                 std::cout<<"ERROROROROROROR"<<std::endl;
  192.             if(refcount->fetch_sub(1)-1==0)
  193.             {
  194.                 refcount2--;
  195.                 d((void*)obj);
  196.                 delete refcount;
  197.                 refcount = nullptr;
  198.                 obj = nullptr;
  199.                 std::cout<<refcount2<<std::endl;
  200.             }
  201.         }
  202.     };
  203.     template <typename T1, typename T2>
  204.     bool operator==(const Sptr<T1> &a, const Sptr<T2> &b)
  205.     {
  206.         if(!a&& !b)
  207.             return true;
  208.         else if(a && b)
  209.         {
  210.             return a.get()==b.get();
  211.         }
  212.         else return false;
  213.     }
  214.     template <typename T, typename U>
  215.     Sptr<T> static_pointer_cast(const Sptr<U> &sp)
  216.     {
  217.         return Sptr<T>(sp);
  218.     }
  219.     template <typename T, typename U>
  220.     Sptr<T> dynamic_pointer_cast(const Sptr<U> &sp)
  221.     {
  222.         return Sptr<T>(dynamic_cast<T*>(sp.get()));
  223.     }  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement