Advertisement
expired6978

Smart Ptr

Jun 11th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. template <class T>
  2. class NiPointer
  3. {
  4. public:
  5.     T   * m_pObject;
  6.  
  7.     inline NiPointer(T* pObject)
  8.     {
  9.         m_pObject = pObject;
  10.         if (m_pObject)
  11.             m_pObject->IncRef();
  12.     };
  13.     inline ~NiPointer()
  14.     {
  15.         if (m_pObject)
  16.             m_pObject->DecRef();
  17.     };
  18.     inline operator T*() const
  19.     {
  20.         return m_pObject;
  21.     };
  22.     inline T& operator*() const
  23.     {
  24.         return *m_pObject;
  25.     };
  26.     inline T* operator->() const
  27.     {
  28.         return m_pObject;
  29.     };
  30.     inline NiPointer<T>& operator=(const NiPointer& ptr)
  31.     {
  32.         if (m_pObject != ptr.m_pObject)
  33.         {
  34.             if (m_pObject)
  35.                 m_pObject->DecRef();
  36.  
  37.             m_pObject = ptr.m_pObject;
  38.  
  39.             if (m_pObject)
  40.                 m_pObject->IncRef();
  41.         }
  42.         return *this;
  43.     };
  44.  
  45.     inline NiPointer<T>& operator=(T* pObject)
  46.     {
  47.         if (m_pObject != pObject)
  48.         {
  49.             if (m_pObject)
  50.                 m_pObject->DecRef();
  51.  
  52.             m_pObject = pObject;
  53.  
  54.             if (m_pObject)
  55.                 m_pObject->IncRef();
  56.         }
  57.         return *this;
  58.     };
  59.     inline bool operator==(T* pObject) const
  60.     {
  61.         return (m_pObject == pObject);
  62.     };
  63.     inline bool operator!=(T* pObject) const
  64.     {
  65.         return (m_pObject != pObject);
  66.     };
  67.     inline bool operator==(const NiPointer& ptr) const
  68.     {
  69.         return (m_pObject == ptr.m_pObject);
  70.     };
  71.     inline bool operator!=(const NiPointer& ptr) const
  72.     {
  73.         return (m_pObject != ptr.m_pObject);
  74.     };
  75. };
  76.  
  77. #define NiSmartPointer(classname) \
  78.     class classname; \
  79.     typedef NiPointer<classname> classname##Ptr
  80.  
  81. #define NiSmartPointerCast(type, smartptr) ((type*) (void*) (smartptr))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement