TCScopedPtr.h

Go to the documentation of this file.
00001 //*******************************************************************************
00002 //
00003 // *******   ***   ***               *
00004 //    *     *     *                  *
00005 //    *    *      *                *****
00006 //    *    *       ***  *   *   **   *    **    ***
00007 //    *    *          *  * *   *     *   ****  * * *
00008 //    *     *         *   *      *   * * *     * * *
00009 //    *      ***   ***    *     **   **   **   *   *
00010 //                        *
00011 //*******************************************************************************
00012 // see http://sourceforge.net/projects/tcsystem/ for details.
00013 // Copyright (C) 2003 - 2008 Thomas Goessler. All Rights Reserved. 
00014 //*******************************************************************************
00015 //
00016 // TCSystem is the legal property of its developers.
00017 // Please refer to the COPYRIGHT file distributed with this source distribution.
00018 // 
00019 // This library is free software; you can redistribute it and/or             
00020 // modify it under the terms of the GNU Lesser General Public                
00021 // License as published by the Free Software Foundation; either              
00022 // version 2.1 of the License, or (at your option) any later version.        
00023 //                                                                           
00024 // This library is distributed in the hope that it will be useful,           
00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of            
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         
00027 // Lesser General Public License for more details.                           
00028 //                                                                           
00029 // You should have received a copy of the GNU Lesser General Public          
00030 // License along with this library; if not, write to the Free Software       
00031 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
00032 //*******************************************************************************
00033 //  $Id: TCScopedPtr.h 863 2008-07-14 20:54:39Z the_____tiger $
00034 //*******************************************************************************
00035 #ifndef _TC_SCOPED_POINTER_H_
00036 #define _TC_SCOPED_POINTER_H_
00037 
00038 #include "TCTypes.h"
00039 #include "TCDelete.h"
00040 
00041 #include <memory> // auto ptr
00042 
00043 namespace TC
00044 {
00064    template < class T, class DELETER=CheckedDelete >
00065    class ScopedPtr
00066    {
00067    private:
00068       typedef ScopedPtr<T> ThisType;
00069 
00070    public:
00072       ScopedPtr():m_ptr(0) {}
00074       explicit ScopedPtr(T* ptr):m_ptr(ptr) {}
00076       explicit ScopedPtr(std::auto_ptr<T> ptr):m_ptr(ptr.get()) {ptr.release();}
00077 
00079       ~ScopedPtr() { DELETER()(m_ptr);}
00080 
00082       inline const T* operator->() const { return m_ptr;}
00084       inline T* operator->() { return m_ptr; }
00085 
00086       typedef T* ThisType::*unspecified_bool_type;
00088       operator unspecified_bool_type() const
00089       {
00090          return m_ptr == 0? 0: &ThisType::m_ptr;
00091       }
00092 
00094       inline bool operator !() const { return m_ptr == 0; }
00095 
00096    private:
00097       T*    m_ptr;
00098    };
00099 
00104 } // namespace TC
00105 
00106 #endif // _TC_SCOPED_POINTER_H_

Copyright (c) Thomas Goessler 2003 - 2008