• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/WeakReference.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef _weakreference_h
00018 #define _weakreference_h
00019 
00020 #include <spl/types.h>
00021 #include <spl/Debug.h>
00022 #include <spl/RefCountPtr.h>
00023 
00033 template<class BASE, class BASEPTR>
00034 class WeakReference : public IMemoryValidate
00035 {
00036 private:
00037     _PtrHolder<BASE> *m_ptr;
00038 
00039 public:
00040         inline WeakReference()
00041         : m_ptr(NULL)
00042         {
00043         }
00044 
00045         inline WeakReference(BASEPTR& rptr)
00046         : m_ptr(rptr.GetHolder())
00047         {
00048         }
00049 
00050         virtual ~WeakReference()
00051         {
00052         }
00053 
00054         inline operator BASEPTR() const
00055         {
00056                 return BASEPTR(m_ptr);
00057         }
00058 
00059         inline WeakReference& operator =(BASEPTR& rptr)
00060         {
00061                 m_ptr = rptr.GetHolder();
00062                 return *this;
00063         }
00064 
00065     // value access
00066     inline BASE* Get() const
00067     {
00068                 if (NULL == m_ptr)
00069                 {
00070                         return NULL;
00071                 }
00072                 ASSERT(m_ptr->Count() > 0);
00073         return m_ptr->ptr;
00074     }
00075 
00076     inline BASE* operator->() const
00077     {
00078                 ASSERT(NULL != m_ptr);
00079                 ASSERT(m_ptr->Count() > 0);
00080         return m_ptr->ptr;
00081     }
00082 
00083         inline operator BASE&() const
00084         {
00085                 if (NULL == m_ptr)
00086                 {
00087                         throw new Exception("NULL pointer");
00088                 }
00089                 ASSERT(m_ptr->Count() > 0);
00090         return *m_ptr->ptr;
00091         }
00092 
00093         inline void Clear()
00094         {
00095                 m_ptr = NULL;
00096         }
00097 
00098         inline bool IsNull() const
00099         {
00100                 return m_ptr == NULL;
00101         }
00102 
00103         inline bool IsNotNull() const
00104         {
00105                 return ! IsNull();
00106         }
00107 
00108 #if defined(DEBUG) || defined(_DEBUG)
00109         void CheckMem() const
00110         {
00111         }
00112 
00113         void ValidateMem() const
00114         {
00115                 if (NULL != m_ptr)
00116                 {
00117                         m_ptr->ValidateThis();
00118                         ASSERT_MEM(m_ptr->ptr, sizeof(BASE));
00119                         ValidateType( m_ptr->ptr );
00120                 }
00121         }
00122 #endif
00123 };
00124 
00127 #endif