Cache.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    ---------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __ccxx_Cache_hxx
00024 #define __ccxx_Cache_hxx
00025 
00026 #include <map>
00027 #include <utility>
00028 
00029 #include <commonc++/Common.h++>
00030 
00031 namespace ccxx {
00032 
00046 template<typename K, typename T> class Cache
00047 {
00048   public:
00049 
00056   Cache(uint_t maxSize);
00057 
00061   virtual ~Cache();
00062 
00069   void put(K key, T* item);
00070 
00078   T *get(K key) const;
00079 
00083   inline T *operator[](K key) const
00084   { return(this->get(key)); }
00085 
00094   T *take(K key);
00095 
00103   bool remove(K key);
00104 
00112   bool contains(K key) const;
00113 
00117   void clear();
00118 
00123   inline uint_t getMaxSize() const
00124   { return(_maxSize); }
00125 
00130   inline uint_t getSize() const
00131   { return(_size); }
00132 
00136   inline bool isEmpty() const
00137   { return(_size == 0); }
00138 
00139   protected:
00140 
00151   virtual void onRemove(K key, T *item);
00152 
00153   private:
00154 
00155   uint_t _maxSize;
00156   uint_t _size;
00157 
00158   struct Link
00159   {
00160     Link(K key)
00161       : key(key),
00162         prev(NULL),
00163         next(NULL)
00164     { }
00165 
00166     K key;
00167     Link *prev;
00168     Link *next;
00169   };
00170 
00171   struct KeyCompare
00172   {
00173     bool operator()(const Link *key1, const Link *key2) const
00174     { return(key1->key < key2->key); }
00175   };
00176 
00177   void _unlink(Link *link) const;
00178   void _link(Link *link) const;
00179 
00180   typedef std::map<Link *, T *, KeyCompare> MapType;
00181   mutable MapType _map;
00182   mutable Link *_head;
00183   mutable Link *_tail;
00184 };
00185 
00186 #include <commonc++/CacheImpl.h++>
00187 
00188 } // namespace ccxx
00189 
00190 #endif // __ccxx_Cache_hxx
00191 
00192 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3