cache.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. {
  28. /* Make copy constructor and assignment operator inaccessible */
  29. CCache(const CCache &refCache);
  30. CCache &operator=(const CCache &refCache);
  31. private:
  32. /* These are initialised in the constructor. The first variable points to
  33. an array of pointers, each of which points to a CBaseObject derived
  34. object. The m_iCacheSize is the static fixed size for the cache and the
  35. m_iUsed defines the number of places filled with objects at any time.
  36. We fill the array of pointers from the start (ie m_ppObjects[0] first)
  37. and then only add and remove objects from the end position, so in this
  38. respect the array of object pointers should be treated as a stack */
  39. CBaseObject **m_ppObjects;
  40. const INT m_iCacheSize;
  41. INT m_iUsed;
  42. public:
  43. CCache(__in_opt LPCTSTR pName,INT iItems);
  44. virtual ~CCache();
  45. /* Add an item to the cache */
  46. CBaseObject *AddToCache(__in CBaseObject *pObject);
  47. /* Remove an item from the cache */
  48. CBaseObject *RemoveFromCache();
  49. /* Delete all the objects held in the cache */
  50. void RemoveAll(void);
  51. /* Return the cache size which is set during construction */
  52. INT GetCacheSize(void) const {
  53. return m_iCacheSize;
  54. };
  55. };
  56. #endif /* __CACHE__ */