Blender  V2.59
AUD_Reference.h
Go to the documentation of this file.
00001 /*
00002  * $Id: AUD_Reference.h 35141 2011-02-25 10:21:56Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * Copyright 2009-2011 Jörg Hermann Müller
00007  *
00008  * This file is part of AudaSpace.
00009  *
00010  * Audaspace is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * AudaSpace is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with Audaspace; if not, write to the Free Software Foundation,
00022  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00023  *
00024  * ***** END GPL LICENSE BLOCK *****
00025  */
00026 
00032 #ifndef AUD_REFERENCE
00033 #define AUD_REFERENCE
00034 
00035 template <class T>
00039 class AUD_Reference
00040 {
00041 private:
00043         T* m_reference;
00045         int* m_refcount;
00046 public:
00051         AUD_Reference(T* reference = 0)
00052         {
00053                 m_reference = reference;
00054                 m_refcount = new int;
00055                 *m_refcount = 1;
00056         }
00057 
00062         AUD_Reference(const AUD_Reference& ref)
00063         {
00064                 m_reference = ref.m_reference;
00065                 m_refcount = ref.m_refcount;
00066                 (*m_refcount)++;
00067         }
00068 
00073         ~AUD_Reference()
00074         {
00075                 (*m_refcount)--;
00076                 if(*m_refcount == 0)
00077                 {
00078                         if(m_reference)
00079                         {
00080                                 delete m_reference;
00081                         }
00082                         delete m_refcount;
00083                 }
00084         }
00085 
00090         AUD_Reference& operator=(const AUD_Reference& ref)
00091         {
00092                 if(&ref == this)
00093                         return *this;
00094 
00095                 (*m_refcount)--;
00096                 if(*m_refcount == 0)
00097                 {
00098                         if(m_reference)
00099                         {
00100                                 delete m_reference;
00101                         }
00102                         delete m_refcount;
00103                 }
00104 
00105                 m_reference = ref.m_reference;
00106                 m_refcount = ref.m_refcount;
00107                 (*m_refcount)++;
00108 
00109                 return *this;
00110         }
00111 
00115         T* get() const
00116         {
00117                 return m_reference;
00118         }
00119 };
00120 
00121 #endif // AUD_REFERENCE