home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / cache.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-19  |  3.9 KB  |  113 lines

  1. //==========================================================================;
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (c) 1992 - 1996  Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11.  
  12. // Non MFC based generic cache class, January 1995
  13.  
  14. /* This class implements a simple cache. A cache object is instantiated
  15.    with the number of items it is to hold. An item is a pointer to an
  16.    object derived from CBaseObject (helps reduce memory leaks). The cache
  17.    can then have objects added to it and removed from it. The cache size
  18.    is fixed at construction time and may therefore run out or be flooded.
  19.    If it runs out it returns a NULL pointer, if it fills up it also returns
  20.    a NULL pointer instead of a pointer to the object just inserted */
  21.  
  22. #include <streams.h>
  23.  
  24. /* Constructor creates an array of pointers (to CBaseObject derived objects)
  25.    and then zero fills each position. The cache cannot be resized dynamically
  26.    so if the number of objects that you use varies widely during the lifetime
  27.    of the cache then there will be some inefficiency. Making the cache resize
  28.    would be an expensive operation especially with so little to be gained */
  29.  
  30. CCache::CCache(TCHAR *pName, INT iItems) :
  31.     CBaseObject(pName),
  32.     m_iCacheSize(iItems),
  33.     m_iUsed(0)
  34. {
  35.     ASSERT(iItems > 0);
  36.  
  37.     /* Create the array of pointers and set the cache size, if this does not
  38.        succeed then it should probably throw an exception otherwise there is
  39.        no way to propogate the error condition back to creating object */
  40.  
  41.     m_ppObjects = (CBaseObject **) new BYTE[iItems * sizeof(CBaseObject *)];
  42.     ASSERT(m_ppObjects);
  43.  
  44.     ZeroMemory((PVOID) m_ppObjects,iItems * sizeof(CBaseObject *));
  45. }
  46.  
  47.  
  48. /* Destructor causes the cached objects to be deleted (if not already done)
  49.    and then deletes the (variable length) array that was used to hold them */
  50.  
  51. CCache::~CCache()
  52. {
  53.     RemoveAll();
  54.     delete m_ppObjects;
  55. }
  56.  
  57.  
  58. /* Add an item to the cache, if no space is found for it then we return NULL
  59.    otherwise we put the object in the free space. The object is meant to be
  60.    as small and lightweight as possible so it does not do any multithread
  61.    protection. This is assumed to be handled by the owning (list) object */
  62.  
  63. CBaseObject *CCache::AddToCache(CBaseObject *pObject)
  64. {
  65.     /* Have we any room for this object */
  66.  
  67.     if (m_iUsed == m_iCacheSize) {
  68.         return NULL;
  69.     }
  70.  
  71.     /* Add the object to the end of the cache */
  72.  
  73.     m_ppObjects[m_iUsed] = pObject;
  74.     m_iUsed++;
  75.     return pObject;
  76. }
  77.  
  78.  
  79. /* This is called to retrieve one of the objects we were given to look after
  80.    if there are none available then we return NULL otherwise the return the
  81.    object. NOTE we do not do any processing on the object so it will be in
  82.    exactly the same state when it is handed back as when it is installed */
  83.  
  84. CBaseObject *CCache::RemoveFromCache()
  85. {
  86.     /* Are all the cache positions empty */
  87.  
  88.     if (m_iUsed == 0) {
  89.         return NULL;
  90.     }
  91.  
  92.     /* Return the last object put into the cache */
  93.  
  94.     m_iUsed--;
  95.     ASSERT(m_ppObjects[m_iUsed]);
  96.     return m_ppObjects[m_iUsed];
  97. }
  98.  
  99.  
  100. /* Delete all the objects held in the cache, simply scans the list of objects
  101.    in the cache and deletes each of them in turn. The list class uses a cache
  102.    to manage node objects that are frequently created and destroyed and when
  103.    the list is finally deleted we will also go at which point we also delete
  104.    any remaining node objects. The objects must be derived from CBaseObject */
  105.  
  106. void CCache::RemoveAll()
  107. {
  108.     while (m_iUsed--) {
  109.         ASSERT(m_ppObjects[m_iUsed]);
  110.         delete m_ppObjects[m_iUsed];
  111.     }
  112. }
  113.