home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / cache.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-19  |  3.0 KB  |  79 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. /* Making these classes inherit from CBaseObject does nothing for their
  23.    functionality but it allows us to check there are no memory leaks */
  24.  
  25. /* WARNING Be very careful when using this class, what it lets you do is
  26.    store and retrieve objects so that you can minimise object creation
  27.    which in turns improves efficiency. However the object you store is
  28.    exactly the same as the object you get back which means that it short
  29.    circuits the constructor initialisation phase. This means any class
  30.    variables the object has (eg pointers) are highly likely to be invalid.
  31.    Therefore ensure you reinitialise the object before using it again */
  32.  
  33.  
  34. #ifndef __CACHE__
  35. #define __CACHE__
  36.  
  37.  
  38. class CCache : CBaseObject {
  39.  
  40.     /* Make copy constructor and assignment operator inaccessible */
  41.  
  42.     CCache(const CCache &refCache);
  43.     CCache &operator=(const CCache &refCache);
  44.  
  45. private:
  46.  
  47.     /* These are initialised in the constructor. The first variable points to
  48.        an array of pointers, each of which points to a CBaseObject derived
  49.        object. The m_iCacheSize is the static fixed size for the cache and the
  50.        m_iUsed defines the number of places filled with objects at any time.
  51.        We fill the array of pointers from the start (ie m_ppObjects[0] first)
  52.        and then only add and remove objects from the end position, so in this
  53.        respect the array of object pointers should be treated as a stack */
  54.  
  55.     CBaseObject **m_ppObjects;
  56.     const INT m_iCacheSize;
  57.     INT m_iUsed;
  58.  
  59. public:
  60.  
  61.     CCache(TCHAR *pName,INT iItems);
  62.     virtual ~CCache();
  63.  
  64.     /* Add an item to the cache */
  65.     CBaseObject *AddToCache(CBaseObject *pObject);
  66.  
  67.     /* Remove an item from the cache */
  68.     CBaseObject *RemoveFromCache();
  69.  
  70.     /* Delete all the objects held in the cache */
  71.     void RemoveAll(void);
  72.  
  73.     /* Return the cache size which is set during construction */
  74.     INT GetCacheSize(void) const {return m_iCacheSize;};
  75. };
  76.  
  77. #endif /* __CACHE__ */
  78.  
  79.