QGpCoreTools/CacheItem.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2006-07-06
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef CACHEITEM_H
00029 #define CACHEITEM_H
00030 
00031 #include <time.h>
00032 
00033 #include "Global.h"
00034 #include "ApplicationClock.h"
00035 #include "QGpCoreToolsDLLExport.h"
00036 #include "Trace.h"
00037 
00038 namespace QGpCoreTools {
00039 
00040   class CacheProcess;
00041 
00042   class QGPCORETOOLS_EXPORT CacheItem
00043   {
00044   public:
00045     inline CacheItem( );
00046     inline CacheItem(const CacheItem& o);
00047     virtual ~CacheItem();
00048 
00049     inline void addProcess(CacheProcess * p) const;
00050     inline void removeProcess(CacheProcess * p) const;
00051     quint64 nextPredictedTime() const;
00052   protected:
00053     friend class Cache;
00054     friend class AllocatedCacheItem;
00055 
00056     inline void lockData() const;
00057     inline void unlockData() const;
00058 
00059     // Memory management for cached data
00060     bool isSaved() const {return _saved;}
00061     virtual bool isAllocated() const=0;
00062     virtual int dataSize() const=0;
00063     virtual bool allocate() const=0;
00064     virtual void free() const=0;
00065     virtual void save(QDir& d) const=0;
00066     virtual void load(QDir& d) const=0;
00067 
00068     inline QString swapFileName() const {return QString("cache_data_%1").arg((ulong) this, 0, 16);}
00069     virtual QString debugName() const {return swapFileName();}
00070   private:
00071     mutable QMutex _dataLock, _processLock;
00072     mutable int _lockCount;
00073     mutable QSet<CacheProcess *> _processes;
00074     mutable quint64 _lastAccess;
00075     mutable bool _saved:1;
00076   };
00077 
00078   inline CacheItem::CacheItem( ) : _dataLock(QMutex::Recursive)
00079   {
00080     TRACE;
00081     _lastAccess=ApplicationClock::elapsed();
00082     _saved=false;
00083     _lockCount=0;
00084   }
00085 
00086   inline CacheItem::CacheItem(const CacheItem& /*o*/ ) : _dataLock(QMutex::Recursive)
00087   {
00088     TRACE;
00089     _lastAccess=ApplicationClock::elapsed();
00090     _saved=false;
00091     _lockCount=0;
00092   }
00093 
00094   inline void CacheItem::lockData() const
00095   {
00096     _dataLock.lock();
00097     _lockCount++;
00098   }
00099 
00100   inline void CacheItem::unlockData() const
00101   {
00102     _lockCount--;
00103     _lastAccess=ApplicationClock::elapsed();
00104     _dataLock.unlock();
00105   }
00106 
00107   inline void CacheItem::addProcess(CacheProcess * p) const
00108   {
00109     _processLock.lock();
00110     _processes.insert(p);
00111     _processLock.unlock();
00112   }
00113 
00114   inline void CacheItem::removeProcess(CacheProcess * p) const
00115   {
00116     _processLock.lock();
00117     _processes.remove(p);
00118     _processLock.unlock();
00119   }
00120 
00121 } // namespace QGpCoreTools
00122 
00123 #endif // CACHEITEM_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines