NewNet/nnbaseptr.h

00001 /*  NewNet - A networking framework in C++
00002     Copyright (C) 2006 Ingmar K. Steen (iksteen@gmail.com)
00003 
00004     This program 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 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program 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 this program; if not, write to the Free Software
00016     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018  */
00019 
00020 #ifndef NEWNET_BASEPTR_H
00021 #define NEWNET_BASEPTR_H
00022 
00023 #ifdef NN_PTR_DEBUG
00024  #include <assert.h>
00025  #ifdef NN_PTR_DEBUG_ASSERT
00026   #define NN_PTR_CHECK { assert(m_Ptr != 0); }
00027  #else
00028   #include <iostream>
00029   #define NN_PTR_CHECK { if(! m_Ptr) std::cerr << "Warning: Dereferencing NULL pointer" << std::endl; }
00030  #endif
00031 #else
00032  #define NN_PTR_CHECK
00033 #endif
00034 
00035 namespace NewNet
00036 {
00037   class Object;
00038 
00040 
00043   template<class T> class BasePtr
00044   {
00045   public:
00047 
00048     BasePtr() : m_Ptr(0)
00049     {
00050     }
00051 
00053 
00054     BasePtr(T * t) : m_Ptr(t)
00055     {
00056     }
00057 
00059 
00060     T * ptr() const
00061     {
00062       return m_Ptr;
00063     }
00064 
00066 
00067     operator T*() const
00068     {
00069       return m_Ptr;
00070     }
00071 
00073 
00075     T & operator*()
00076     {
00077       NN_PTR_CHECK
00078       return *m_Ptr;
00079     }
00080 
00082 
00084     const T & operator*() const
00085     {
00086       NN_PTR_CHECK
00087       return *m_Ptr;
00088     }
00089 
00091 
00093     T * operator->()
00094     {
00095       NN_PTR_CHECK
00096       return m_Ptr;
00097     }
00098 
00100 
00102     const T * operator->() const
00103     {
00104       NN_PTR_CHECK
00105       return m_Ptr;
00106     }
00107 
00109 
00110     bool isValid() const
00111     {
00112       return m_Ptr != 0;
00113     }
00114 
00116 
00117     bool operator!() const
00118     {
00119       return m_Ptr == 0;
00120     }
00121 
00122   protected:
00124 
00125     T * m_Ptr;
00126   };
00127 }
00128 
00129 #undef NN_PTR_CHECK
00130   
00131 #endif // NEWNET_REFPTR_H

Generated on Sun Jan 7 14:00:00 2007 for NewNet by  doxygen 1.5.1