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