QGpCoreTools/AbstractNumericalCache.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 : 2009-04-11
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef ABSTRACTNUMERICALCACHE_H
00029 #define ABSTRACTNUMERICALCACHE_H
00030 
00031 #include "QGpCoreToolsDLLExport.h"
00032 #include "Global.h"
00033 #include "CacheItem.h"
00034 #include "AbstractNumericalKey.h"
00035 
00036 namespace QGpCoreTools {
00037 
00038 class HashNumericalKey;
00039 
00040 class QGPCORETOOLS_EXPORT AbstractNumericalCache
00041 {
00042 public:
00043   inline AbstractNumericalCache(AbstractNumericalKey * key);
00044   virtual ~AbstractNumericalCache() {delete _key;}
00045 
00046   virtual void init()=0;
00047   const AbstractNumericalKey& key() const {return *_key;}
00048 
00049   static const AbstractNumericalCache * begin(AbstractNumericalKey * key);
00050   static void end(const AbstractNumericalCache * cache);
00051   static void clear();
00052 private:
00053   static AbstractNumericalCache * add(AbstractNumericalKey * key);
00054 
00055   clock_t lastUse() const {return _lastUse;}
00056   void use() const {_lastUse=clock();}
00057 
00058   inline void setLocked(bool l) const;
00059   bool locked() const {return !_locked.testAndSetOrdered(0, 0);}
00060 
00061   AbstractNumericalKey * _key;
00062   mutable clock_t _lastUse;
00063   mutable QAtomicInt _locked;
00064 
00065   static QMutex _mutex;
00066   static QHash<HashNumericalKey, AbstractNumericalCache *> _caches;
00067   static int _currentByteCount;
00068   static int _maximumByteCount;
00069 };
00070 
00071 inline AbstractNumericalCache::AbstractNumericalCache(AbstractNumericalKey * key)
00072 {
00073   _key=key;
00074   _lastUse=clock();
00075   _locked=0;
00076 }
00077 
00078 inline void AbstractNumericalCache::setLocked(bool l) const
00079 {
00080   if(l) {
00081     _locked.fetchAndAddOrdered (1);
00082   } else {
00083     ASSERT(_locked.fetchAndAddOrdered ( -1)>=0);
00084   }
00085 }
00086 
00087 } // namespace QGpCoreTools
00088 
00089 #endif // ABSTRACTNUMERICALCACHE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines