cache.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //------------------------------------------------------------------------------
  2. // File: Cache.h
  3. //
  4. // Desc: DirectShow base classes - efines a non-MFC generic cache class.
  5. //
  6. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  7. //------------------------------------------------------------------------------
  8. /* This class implements a simple cache. A cache object is instantiated
  9. with the number of items it is to hold. An item is a pointer to an
  10. object derived from CBaseObject (helps reduce memory leaks). The cache
  11. can then have objects added to it and removed from it. The cache size
  12. is fixed at construction time and may therefore run out or be flooded.
  13. If it runs out it returns a NULL pointer, if it fills up it also returns
  14. a NULL pointer instead of a pointer to the object just inserted */
  15. /* Making these classes inherit from CBaseObject does nothing for their
  16. functionality but it allows us to check there are no memory leaks */
  17. /* WARNING Be very careful when using this class, what it lets you do is
  18. store and retrieve objects so that you can minimise object creation
  19. which in turns improves efficiency. However the object you store is
  20. exactly the same as the object you get back which means that it short
  21. circuits the constructor initialisation phase. This means any class
  22. variables the object has (eg pointers) are highly likely to be invalid.
  23. Therefore ensure you reinitialise the object before using it again */
  24. #ifndef __CACHE__
  25. #define __CACHE__
  26. class CCache : CBaseObject {
  27. /* Make copy constructor and assignment operator inaccessible */
  28. CCache(const CCache &refCache);
  29. CCache &operator=(const CCache &refCache);
  30. private:
  31. /* These are initialised in the constructor. The first variable points to
  32. an array of pointers, each of which points to a CBaseObject derived
  33. object. The m_iCacheSize is the static fixed size for the cache and the
  34. m_iUsed defines the number of places filled with objects at any time.
  35. We fill the array of pointers from the start (ie m_ppObjects[0] first)
  36. and then only add and remove objects from the end position, so in this
  37. respect the array of object pointers should be treated as a stack */
  38. CBaseObject **m_ppObjects;
  39. const INT m_iCacheSize;
  40. INT m_iUsed;
  41. public:
  42. CCache(__in_opt LPCTSTR pName,INT iItems);
  43. virtual ~CCache();
  44. /* Add an item to the cache */
  45. CBaseObject *AddToCache(__in CBaseObject *pObject);
  46. /* Remove an item from the cache */
  47. CBaseObject *RemoveFromCache();
  48. /* Delete all the objects held in the cache */
  49. void RemoveAll(void);
  50. /* Return the cache size which is set during construction */
  51. INT GetCacheSize(void) const {return m_iCacheSize;};
  52. };
  53. #endif /* __CACHE__ */