00001 /*************************************************************************** 00002 * Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider, * 00003 * Matthias Hardt, Guido Madaus * 00004 * * 00005 * Copyright (C) 2007-2008 BerLinux Solutions GbR * 00006 * Stefan Schwarzer & Guido Madaus * 00007 * * 00008 * Copyright (C) 2009-2013 BerLinux Solutions GmbH * 00009 * * 00010 * Authors: * 00011 * Stefan Schwarzer <stefan.schwarzer@diskohq.org>, * 00012 * Matthias Hardt <matthias.hardt@diskohq.org>, * 00013 * Jens Schneider <jens.schneider@diskohq.org>, * 00014 * Guido Madaus <guido.madaus@diskohq.org>, * 00015 * Patrick Helterhoff <patrick.helterhoff@diskohq.org>, * 00016 * René Bählkow <rene.baehlkow@diskohq.org> * 00017 * * 00018 * This library is free software; you can redistribute it and/or * 00019 * modify it under the terms of the GNU Lesser General Public * 00020 * License version 2.1 as published by the Free Software Foundation. * 00021 * * 00022 * This library is distributed in the hope that it will be useful, * 00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00025 * Lesser General Public License for more details. * 00026 * * 00027 * You should have received a copy of the GNU Lesser General Public * 00028 * License along with this library; if not, write to the * 00029 * Free Software Foundation, Inc., * 00030 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00031 **************************************************************************/ 00032 00033 #ifndef MMSHANDLER_H_ 00034 #define MMSHANDLER_H_ 00035 00036 #include "mmstools/mmsmutex.h" 00037 #include <iostream> 00038 00039 /** 00040 * Handler class that implements reference counting. 00041 */ 00042 template<class X> class MMSHandle { 00043 private: 00044 X *data; /**< pointer to object that shall be handled */ 00045 int *refCount; /**< reference counter */ 00046 MMSMutex *lock; /**< needed to make increase and decrease of refCount atomic */ 00047 00048 public: 00049 /** 00050 * Constructor 00051 */ 00052 explicit MMSHandle(X* orig) : data(orig), refCount(new int(1)), lock(new MMSMutex()) { 00053 } 00054 00055 /** 00056 * Copy constructor 00057 */ 00058 MMSHandle(const MMSHandle& orig) : data(orig.data), refCount(orig.refCount), lock(orig.lock) { 00059 lock->lock(); 00060 (*refCount)++; 00061 lock->unlock(); 00062 } 00063 00064 /** 00065 * Destructor 00066 */ 00067 virtual ~MMSHandle() throw() { 00068 lock->lock(); 00069 --(*refCount); 00070 lock->unlock(); 00071 if(*refCount == 0) { 00072 if(data) delete data; 00073 delete refCount; 00074 delete lock; 00075 } 00076 } 00077 00078 /** 00079 * Overloaded -> operator. 00080 * Just returns the data object. 00081 */ 00082 X* operator->() throw() { 00083 return data; 00084 } 00085 00086 /** 00087 * Overloaded = operator. 00088 */ 00089 void operator=(const MMSHandle& orig) throw() { 00090 /* do not copy if data is already the same */ 00091 if(data != orig.data) { 00092 /* free memory if reference counter is 0 */ 00093 lock->lock(); 00094 --(*refCount); 00095 lock->unlock(); 00096 if(*refCount == 0) { 00097 if(data) delete data; 00098 delete refCount; 00099 delete lock; 00100 } 00101 00102 /* create a copy */ 00103 data = orig.data; 00104 refCount = orig.refCount; 00105 lock = orig.lock; 00106 00107 /* increase reference counter */ 00108 lock->lock(); 00109 refCount++; 00110 lock->unlock(); 00111 } 00112 } 00113 00114 /** 00115 * Get data object. 00116 */ 00117 X& operator*() const throw() { 00118 return *data; 00119 } 00120 00121 /** 00122 * Get data object. 00123 */ 00124 X* getObject() const throw() { 00125 return data; 00126 } 00127 }; 00128 00129 #endif /*MMSHANDLER_H_*/