Advertisement
raffa50

C++ Ref

Feb 12th, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #pragma once
  2. /*simple and basic reference by Aldrigo Raffaele
  3. this source it's under license and used for a library
  4. you must give attribution to Aldrigo Raffaele if you want to use it.
  5. Commercial use it's prohibited, you must use that only for Accademic purposes
  6. */
  7. #include "Common.h"
  8. #include "Exceptions.h"
  9.  
  10. class MemWrp {
  11.     private:
  12.     uint numref;
  13.     public:
  14.     void* content;
  15.  
  16.     MemWrp(void* content) {
  17.         this->content = content;
  18.         numref = 1;
  19.     }
  20.  
  21.     void AddRef() { numref++; }
  22.     uint RemoveRef() { return --numref; }}
  23. };
  24.  
  25. template< class T >
  26. class Ref {
  27.     private:
  28.     MemWrp* wrp = NULL;
  29.     public:
  30.     Ref() {}
  31.     Ref(T* ptr) {
  32.         wrp = NULL;
  33.         if (ptr != NULL) wrp = new MemWrp(ptr);
  34.     }
  35.     Ref(nullptr_t) {}
  36.     Ref(const Ref<T>& r) {
  37.         wrp = r.wrp;
  38.         if (wrp != NULL) wrp->AddRef();
  39.     }
  40.     Ref(MemWrp* w) {
  41.         wrp = w;
  42.         wrp->AddRef();
  43.     }
  44.  
  45.     T* Get() const {
  46.         if (wrp == NULL) return NULL;
  47.         return (T*)wrp->content;
  48.     }
  49.  
  50.     template<class F> bool is() const { return dynamic_cast<F*>(Get()) != NULL; }
  51.  
  52.     template<class F> Ref<F> as() {
  53.         if (!this->is<F>()) return Ref<F>::Ref();
  54.         return Ref<F>::Ref(this->wrp);
  55.     }
  56.  
  57.     Ref<T>& operator=(Ref<T>& rhs) {
  58.         if (this == &rhs) return *this;
  59.         if (rhs.wrp == NULL && wrp != NULL && wrp->RemoveRef() == 0) { delete Get(); delete wrp; }
  60.         wrp = rhs.wrp;
  61.         if (wrp != NULL) wrp->AddRef();
  62.         return *this;
  63.     }
  64.  
  65.     Ref<T>& operator=(T* rhs) {
  66.         if (Get() == rhs) return *this;
  67.         if (rhs == NULL && wrp != NULL && wrp->RemoveRef() == 0) { delete Get(); delete wrp; }
  68.         else if (rhs != NULL) wrp = new MemWrp(rhs);
  69.         return *this;
  70.     }
  71.  
  72.     bool operator==(const Ref<T>& other) const { return (this->Get() == other.Get()); }
  73.  
  74.     bool operator==(const int other) const { return (this->Get() == (void*)other); }
  75.  
  76.     bool operator!=(const Ref<T>& other) const { return !(*this == other); }
  77.  
  78.     bool operator!=(const int other) const { return !(*this == other); }
  79.  
  80.     T* operator->() {
  81.         if (this->Get() != NULL) return this->Get();
  82.         throw NullPointerException();
  83.     }
  84.     const T* operator->() const {
  85.         if (this->Get() != NULL) return this->Get();
  86.         throw NullPointerException();
  87.     }
  88.  
  89.     destructor Ref() {
  90.         if (wrp != NULL && wrp->RemoveRef() == 0) { delete Get(); delete wrp; }
  91.     }
  92. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement