KSquare Utilities
RasterBuffer.h
Go to the documentation of this file.
1 /* RasterBuffer.h -- Implements buffering of raster instances allowing for multiple threads to access the queue safely.
2  * Copyright (C) 2011-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #if !defined(_RASTERBUFFER_X_)
6 #define _RASTERBUFFER_X_
7 
8 #include <queue>
9 
10 
11 #include "KKStr.h"
12 #include "GoalKeeper.h"
13 
14 namespace KKB
15 {
16  #if !defined(_RASTER_)
17  class Raster;
18  typedef Raster* RasterPtr;
19 
20  class RasterList;
21  typedef RasterList* RasterListPtr;
22  #endif
23 
24  /**
25  *@class RasterBuffer
26  *@brief Will manage a buffer that will allow multiple threads to add and remove instances of 'Raster' objects.
27  *@details
28  * A 'GoalKeepeer' object 'gateKeeper' will be used to enforce integrity in the Multi-Threaded environment. It
29  * will guarantee that only one thread at a time can access the Raster Queue. This queue will take ownership of
30  * 'Raster' instances added to it via 'AddRaster'. It will pass ownership of the 'Raster' instances when it
31  * returns them in GetNextRaster. When a 'RasterBuffer' instance is deleted it will delete all the 'Raster'
32  * instances it still contains.
33  */
35  {
36  public:
38 
39  /**
40  *@brief Constructor.
41  *@param[in] _name Name of the buffer; this will be used by the associated 'GateKeeper' instance; should be unique.
42  *@param[in] _maxNumOfBuffers The maximum number of raster instances that can be added to this buffer. When
43  * this limit has been reached the oldest entries in the list will be deleted when a new one is
44  * added (AddRaster).
45  */
46  RasterBuffer (const KKStr& _name,
47  kkint32 _maxNumOfBuffers
48  );
49 
50  ~RasterBuffer ();
51 
52  /** @brief Returns the number of 'Raster' instances that had to be deleted because the size of the queue had reached 'maxNumOfBuffers'. */
53  kkint32 RastersDropped () const {return rastersDropped;}
54 
55  kkint32 MaxNumOfBuffers () const {return maxNumOfBuffers;}
56 
57  /** @brief The number of entries that are left in the buffer before 'maxNumOfBuffers' is reached. */
58  kkint32 NumAvailable () const;
59 
60  kkint32 NumPopulated () const;
61 
62 
63  /** @brief Returns an estimate of the amount of memory consumed in bytes.
64  * @details This will help managed objects keep track of how much memory they are using in the unmanaged world.
65  */
67 
68 
69  void MaxNumOfBuffers (kkint32 _maxNumOfBuffers) {maxNumOfBuffers = _maxNumOfBuffers;}
70 
71 
72  /** @brief Adds 'raster' to the end of the queue giving the queue ownership of the instance.
73  * @details
74  * If the number of entries in the queue are already equal or greater than 'MaxNumOfBuffers' specified
75  * then the oldest instances in the queue(back of the queue) will be removed and deleted until the
76  * size of the queue is less than 'MaxNumOfBuffers' before adding this new instance.
77  */
78  void AddRaster (RasterPtr raster);
79 
80  /** @brief Removes from the buffer the oldest instance of 'Raster' and returns it to caller; if buffer is
81  * empty will return NULL.
82  */
83  RasterPtr GetNextRaster ();
84 
85  /**@brief Returns a copy of the last Raster instance added to the queue; if buffer is empty will return NULL.
86  * @details Caller will get ownership and be responsible for deleting it.
87  */
88  RasterPtr GetCopyOfLastImage ();
89 
90 
91  private:
92  /** @brief Remove the oldest 'Raster' instance from the buffer. */
93  void ThrowOutOldestOccupiedBuffer ();
94 
95  std::queue<RasterPtr> buffer;
96  GoalKeeperPtr gateKeeper;
97  kkint32 maxNumOfBuffers;
98  kkint32 memoryConsumed;
99  KKStr name; /**< Name of buffer. */
100  kkint32 rastersDropped; /**< The number of raster instances that had to be deleted because 'maxNumOfBuffers' was reached. */
101  }; /* RasterBuffer */
102 
103 
104  typedef RasterBuffer::RasterBufferPtr RasterBufferPtr;
105 } /* KKB */
106 
107 #endif
__int32 kkint32
Definition: KKBaseTypes.h:88
RasterPtr GetCopyOfLastImage()
Returns a copy of the last Raster instance added to the queue; if buffer is empty will return NULL...
kkint32 MemoryConsumedEstimated()
Returns an estimate of the amount of memory consumed in bytes.
RasterBuffer * RasterBufferPtr
Definition: RasterBuffer.h:37
kkint32 RastersDropped() const
Returns the number of &#39;Raster&#39; instances that had to be deleted because the size of the queue had rea...
Definition: RasterBuffer.h:53
RasterBuffer(const KKStr &_name, kkint32 _maxNumOfBuffers)
Constructor.
KKTHread * KKTHreadPtr
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
kkint32 NumAvailable() const
The number of entries that are left in the buffer before &#39;maxNumOfBuffers&#39; is reached.
RasterPtr GetNextRaster()
Removes from the buffer the oldest instance of &#39;Raster&#39; and returns it to caller; if buffer is empty ...
Will manage a buffer that will allow multiple threads to add and remove instances of &#39;Raster&#39; objects...
Definition: RasterBuffer.h:34
kkint32 NumPopulated() const
kkint32 MaxNumOfBuffers() const
Definition: RasterBuffer.h:55
void MaxNumOfBuffers(kkint32 _maxNumOfBuffers)
Definition: RasterBuffer.h:69
void AddRaster(RasterPtr raster)
Adds &#39;raster&#39; to the end of the queue giving the queue ownership of the instance. ...