Referenced.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2011, SRI International (R)
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include <iostream>
00019 
00020 #ifdef USE_TBB
00021 #include <tbb/mutex.h>
00022 #endif
00023 
00024 #include <OpenKarto/Referenced.h>
00025 
00026 namespace karto
00027 {
00028 
00032 
00033   struct ReferencedPrivate
00034   {
00035 #ifdef USE_TBB
00036     tbb::mutex m_Mutex;
00037 #endif
00038 
00039     kt_int32s m_Counter;
00040 
00041     ReferencedPrivate()
00042       : m_Counter(0)
00043     {
00044     }
00045   };
00046 
00050 
00051   Referenced::Referenced()
00052     : m_pReferencedPrivate(new ReferencedPrivate())
00053   {
00054   }
00055 
00056   Referenced::~Referenced()
00057   {
00058     if (m_pReferencedPrivate->m_Counter > 0)
00059     {
00060       std::cerr << "Deleting referenced object!!" << std::endl;
00061       assert(false);
00062     }
00063 
00064     delete m_pReferencedPrivate;
00065     m_pReferencedPrivate = NULL;
00066   }
00067 
00068   kt_int32s Referenced::Reference() const
00069   {
00070 #ifdef USE_TBB
00071     tbb::mutex::scoped_lock lock(m_pReferencedPrivate->m_Mutex);
00072 #endif
00073 
00074     return ++m_pReferencedPrivate->m_Counter;
00075   }
00076 
00077   kt_int32s Referenced::Unreference() const
00078   {
00079     kt_int32s count = 0;
00080     bool deleteMe = false;
00081 
00082     {
00083 #ifdef USE_TBB
00084       tbb::mutex::scoped_lock lock(m_pReferencedPrivate->m_Mutex);
00085 #endif
00086 
00087       count = --m_pReferencedPrivate->m_Counter;
00088       deleteMe = count <= 0;
00089     }
00090 
00091     if (deleteMe)
00092     {
00093       delete this;
00094     }
00095 
00096     return count;
00097   }
00098 
00099   kt_int32s Referenced::UnreferenceNoDelete() const
00100   {
00101     {
00102 #ifdef USE_TBB
00103       tbb::mutex::scoped_lock lock(m_pReferencedPrivate->m_Mutex);
00104 #endif
00105 
00106       return --m_pReferencedPrivate->m_Counter;
00107     }
00108   }
00109 
00110   kt_int32s Referenced::GetReferenceCount()
00111   {
00112     return m_pReferencedPrivate->m_Counter;
00113   }
00114 
00115 }


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Sun Apr 2 2017 04:05:24