enigma-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Enigma-cvs] enigma/src/px cache.hh,1.2,1.3


From: Daniel Heck <address@hidden>
Subject: [Enigma-cvs] enigma/src/px cache.hh,1.2,1.3
Date: Sun, 16 Nov 2003 17:48:29 +0000

Update of /cvsroot/enigma/enigma/src/px
In directory subversions:/tmp/cvs-serv13590/src/px

Modified Files:
        cache.hh 
Log Message:
Redesigned  to use inheritance instead of template argument; bad idea,
re-redesign later...


Index: cache.hh
===================================================================
RCS file: /cvsroot/enigma/enigma/src/px/cache.hh,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** cache.hh    12 Jan 2003 19:33:16 -0000      1.2
--- cache.hh    16 Nov 2003 17:48:25 -0000      1.3
***************
*** 21,24 ****
--- 21,32 ----
  #define PX_CACHE_HH
  
+ /* -------------------- Cache -------------------- */
+ 
+ /*
+   A generic class for caching external data.  Stored data is owned by
+   the cache and is automatically `release'd on destruction.  Missing
+   values are automatically retrieved using the `acquire' method.
+ */
+ 
  #include "dict.hh"
  #include <string>
***************
*** 26,60 ****
  namespace px
  {
!     template <class T, class Alloc>
      class Cache {
      public:
!         Cache() {}
!         Cache(const Alloc &a) :alloc(a), cache(1223) {}
!         ~Cache() { clear(); }
!         void clear() {
!             for (iterator i=cache.begin(); i!=cache.end(); ++i) 
!                 alloc.release(i->second);
!             cache.clear();
!         }
  
-         T get(const std::string &key) {
-             iterator i=cache.find(key);
-             if (i!=cache.end())
-                 return i->second;
-             else {
-                 T p = alloc.acquire(key);
-                 cache.insert(key, p); //[key] = p;
-                 return p;
-             }
-         }
-         unsigned size() const { return cache.size(); }
      private:
          typedef px::Dict<T> Map;
          typedef typename Map::iterator iterator;
- //         typedef typename Map::const_iterator const_iterator;
  
-         Alloc alloc;
          Map cache;
      };
  }
  
--- 34,112 ----
  namespace px
  {
!     template <class T>
      class Cache {
      public:
!         Cache();
!         virtual ~Cache() {}
! 
!         // ---------- Methods ----------
!         void     clear();
!         T        get (const std::string &key);
!         unsigned size() const;
!         bool     has_key(const std::string &key) const;
!     protected:
!         T        store (const std::string &key, T value);
  
      private:
+         Cache (const Cache &other);
+         Cache &operator= (const Cache &other);
+ 
+         // ---------- Interface ----------
+         virtual T    acquire (const std::string &name) = 0;
+         virtual void release (T value)                 = 0;
+ 
+         // ---------- Variables ----------
          typedef px::Dict<T> Map;
          typedef typename Map::iterator iterator;
  
          Map cache;
      };
+ 
+     template <class T>
+     Cache<T>::Cache() : cache(1223) {
+     }
+ 
+     template <class T>
+     void Cache<T>::clear() {
+         for (iterator i=cache.begin(); i!=cache.end(); ++i) 
+             release(i->second);
+         cache.clear();
+     }
+ 
+     template <class T>
+     T Cache<T>::store (const std::string &key, T value) {
+         cache.insert(key, value);
+         return value;
+     }
+ 
+     template <class T>
+     T Cache<T>::get(const std::string &key) {
+         iterator i=cache.find(key);
+         if (i!=cache.end())
+             return i->second;
+         else
+             return store (key, acquire(key));
+     }
+ 
+     template <class T>
+     unsigned Cache<T>::size() const { 
+         return cache.size(); 
+     }
+     
+     template <class T>
+     bool Cache<T>::has_key(const std::string &key) const {
+         return cache.has_key(key);
+     }
+ 
+ /* -------------------- PtrCache -------------------- */
+ 
+     template <class T>
+     class PtrCache : public Cache<T*> {
+     public:
+         ~PtrCache() { clear(); }
+ 
+         void release (T *value) { delete value; }
+     };
+ 
  }
  





reply via email to

[Prev in Thread] Current Thread [Next in Thread]