src/util/ref.hpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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 3 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, see <http://www.gnu.org/licenses/>.
00016 
00017 #ifndef HEADER_SUPERTUX_UTIL_REF_HPP
00018 #define HEADER_SUPERTUX_UTIL_REF_HPP
00019 
00024 template<typename T>
00025 class Ref
00026 {
00027 public:
00028   Ref(T* object = 0)
00029     : object(object)
00030   {
00031     if(object)
00032       object->ref();
00033   }
00034   Ref(const Ref<T>& other)
00035     : object(other.object)
00036   {
00037     if(object)
00038       object->ref();
00039   }
00040   ~Ref()
00041   {
00042     if(object)
00043       object->unref();
00044   }
00045   
00046   Ref<T>& operator= (const Ref<T>& other)
00047   {
00048     *this = other.get();
00049     return *this;
00050   }
00051 
00052   Ref<T>& operator= (T* object)
00053   {
00054     if(object)
00055       object->ref();
00056     if(this->object)
00057       this->object->unref();
00058     this->object = object;
00059 
00060     return *this;
00061   }
00062 
00063   T* operator ->() const
00064   {
00065     return object;
00066   }
00067 
00068   T& operator* () const
00069   {
00070     return *object;
00071   }
00072 
00073   operator const T* () const
00074   {
00075     return object;
00076   }
00077 
00078   T* get() const
00079   {
00080     return object;
00081   }
00082 
00083 private:
00084   T* object;
00085 };
00086 
00087 #endif
00088 
00089 /* EOF */

Generated on Mon Jun 9 03:38:23 2014 for SuperTux by  doxygen 1.5.1