KSquare Utilities
KKB::RasterBuffer Class Reference

Will manage a buffer that will allow multiple threads to add and remove instances of 'Raster' objects. More...

#include <RasterBuffer.h>

Public Types

typedef RasterBufferRasterBufferPtr
 

Public Member Functions

 RasterBuffer (const KKStr &_name, kkint32 _maxNumOfBuffers)
 Constructor. More...
 
 ~RasterBuffer ()
 
void AddRaster (RasterPtr raster)
 Adds 'raster' to the end of the queue giving the queue ownership of the instance. More...
 
RasterPtr GetCopyOfLastImage ()
 Returns a copy of the last Raster instance added to the queue; if buffer is empty will return NULL. More...
 
RasterPtr GetNextRaster ()
 Removes from the buffer the oldest instance of 'Raster' and returns it to caller; if buffer is empty will return NULL. More...
 
kkint32 MaxNumOfBuffers () const
 
void MaxNumOfBuffers (kkint32 _maxNumOfBuffers)
 
kkint32 MemoryConsumedEstimated ()
 Returns an estimate of the amount of memory consumed in bytes. More...
 
kkint32 NumAvailable () const
 The number of entries that are left in the buffer before 'maxNumOfBuffers' is reached. More...
 
kkint32 NumPopulated () const
 
kkint32 RastersDropped () const
 Returns the number of 'Raster' instances that had to be deleted because the size of the queue had reached 'maxNumOfBuffers'. More...
 

Detailed Description

Will manage a buffer that will allow multiple threads to add and remove instances of 'Raster' objects.

A 'GoalKeepeer' object 'gateKeeper' will be used to enforce integrity in the Multi-Threaded environment. It will guarantee that only one thread at a time can access the Raster Queue. This queue will take ownership of 'Raster' instances added to it via 'AddRaster'. It will pass ownership of the 'Raster' instances when it returns them in GetNextRaster. When a 'RasterBuffer' instance is deleted it will delete all the 'Raster' instances it still contains.

Definition at line 34 of file RasterBuffer.h.

Member Typedef Documentation

Definition at line 37 of file RasterBuffer.h.

Constructor & Destructor Documentation

RasterBuffer::RasterBuffer ( const KKStr _name,
kkint32  _maxNumOfBuffers 
)

Constructor.

Parameters
[in]_nameName of the buffer; this will be used by the associated 'GateKeeper' instance; should be unique.
[in]_maxNumOfBuffersThe maximum number of raster instances that can be added to this buffer. When this limit has been reached the oldest entries in the list will be deleted when a new one is added (AddRaster).

Definition at line 39 of file RasterBuffer.cpp.

References KKB::GoalKeeper::Create(), KKB::KKStr::KKStr(), KKB::GoalKeeper::MemoryConsumedEstimated(), KKB::operator+(), and RasterBuffer().

Referenced by RasterBuffer().

41  :
42 
43  buffer (),
44  gateKeeper (NULL),
45  maxNumOfBuffers (_maxNumOfBuffers),
46  memoryConsumed (0),
47  name (_name),
48  rastersDropped (0)
49 {
50  GoalKeeper::Create ("RasterBuffer_" + name, gateKeeper);
51  memoryConsumed = sizeof (RasterBuffer) + gateKeeper->MemoryConsumedEstimated ();
52 }
kkint32 MemoryConsumedEstimated() const
Definition: GoalKeeper.cpp:166
static void Create(const KKStr &_name, volatile GoalKeeperPtr &_newGoalKeeper)
Create a GoalKeeper object and avoid a race condition doing it.
Definition: GoalKeeper.cpp:346
RasterBuffer(const KKStr &_name, kkint32 _maxNumOfBuffers)
Constructor.
RasterBuffer::~RasterBuffer ( )

Definition at line 56 of file RasterBuffer.cpp.

References KKB::GoalKeeper::Destroy().

57 {
58  while (buffer.size () > 0)
59  {
60  RasterPtr r = buffer.front ();
61  buffer.pop ();
62  delete r;
63  r = NULL;
64  }
65 
66  GoalKeeper::Destroy (gateKeeper);
67  gateKeeper = NULL;
68 }
static void Destroy(volatile GoalKeeperPtr &_goalKeeperInstance)
Destroys an existing instance of GoalKeeper.
Definition: GoalKeeper.cpp:491
A class that is used by to represent a single image in memory.
Definition: Raster.h:108

Member Function Documentation

void RasterBuffer::AddRaster ( RasterPtr  raster)

Adds 'raster' to the end of the queue giving the queue ownership of the instance.

If the number of entries in the queue are already equal or greater than 'MaxNumOfBuffers' specified then the oldest instances in the queue(back of the queue) will be removed and deleted until the size of the queue is less than 'MaxNumOfBuffers' before adding this new instance.

Definition at line 105 of file RasterBuffer.cpp.

References KKB::KKStr::Concat(), KKB::GoalKeeper::EndBlock(), KKB::KKException::KKException(), KKB::Raster::MemoryConsumedEstimated(), and KKB::GoalKeeper::StartBlock().

106 {
107  if (raster == NULL)
108  {
109  KKStr errMsg;
110  errMsg << "RasterBuffer::AddRaster raster == NULL";
111  cerr << std::endl << std::endl << errMsg << std::endl << std::endl;
112  throw KKException (errMsg);
113  }
114 
115  gateKeeper->StartBlock ();
116 
117  while (buffer.size () >= (kkuint32)maxNumOfBuffers)
118  ThrowOutOldestOccupiedBuffer ();
119 
120  buffer.push (raster);
121  memoryConsumed = memoryConsumed + raster->MemoryConsumedEstimated ();
122 
123  gateKeeper->EndBlock ();
124 } /* AddFrame */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
void StartBlock()
Initiates a Block as long as another thread has not already locked this object.
Definition: GoalKeeper.cpp:214
void EndBlock()
Ends the block and allows other threads to pass through StatBlock.
Definition: GoalKeeper.cpp:295
kkint32 MemoryConsumedEstimated() const
Definition: Raster.cpp:864
RasterPtr RasterBuffer::GetCopyOfLastImage ( )

Returns a copy of the last Raster instance added to the queue; if buffer is empty will return NULL.

Caller will get ownership and be responsible for deleting it.

Definition at line 158 of file RasterBuffer.cpp.

References KKB::GoalKeeper::EndBlock(), and KKB::GoalKeeper::StartBlock().

159 {
160  RasterPtr result = NULL;
161  gateKeeper->StartBlock ();
162  if (buffer.size () > 0)
163  {
164  result = new Raster (*(buffer.back ()));
165  }
166  gateKeeper->EndBlock ();
167  return result;
168 }
A class that is used by to represent a single image in memory.
Definition: Raster.h:108
void StartBlock()
Initiates a Block as long as another thread has not already locked this object.
Definition: GoalKeeper.cpp:214
void EndBlock()
Ends the block and allows other threads to pass through StatBlock.
Definition: GoalKeeper.cpp:295
RasterPtr RasterBuffer::GetNextRaster ( )

Removes from the buffer the oldest instance of 'Raster' and returns it to caller; if buffer is empty will return NULL.

Definition at line 128 of file RasterBuffer.cpp.

References KKB::GoalKeeper::EndBlock(), and KKB::GoalKeeper::StartBlock().

129 {
130  RasterPtr result = NULL;
131 
132  gateKeeper->StartBlock ();
133 
134  if (buffer.size () > 0)
135  {
136  result = buffer.front ();
137  buffer.pop ();
138  memoryConsumed = memoryConsumed - result->MemoryConsumedEstimated ();
139  }
140 
141  gateKeeper->EndBlock ();
142  return result;
143 } /* GetNextRaster */
A class that is used by to represent a single image in memory.
Definition: Raster.h:108
void StartBlock()
Initiates a Block as long as another thread has not already locked this object.
Definition: GoalKeeper.cpp:214
void EndBlock()
Ends the block and allows other threads to pass through StatBlock.
Definition: GoalKeeper.cpp:295
kkint32 MemoryConsumedEstimated() const
Definition: Raster.cpp:864
kkint32 KKB::RasterBuffer::MaxNumOfBuffers ( ) const
inline

Definition at line 55 of file RasterBuffer.h.

55 {return maxNumOfBuffers;}
void KKB::RasterBuffer::MaxNumOfBuffers ( kkint32  _maxNumOfBuffers)
inline

Definition at line 69 of file RasterBuffer.h.

69 {maxNumOfBuffers = _maxNumOfBuffers;}
kkint32 RasterBuffer::MemoryConsumedEstimated ( )

Returns an estimate of the amount of memory consumed in bytes.

This will help managed objects keep track of how much memory they are using in the unmanaged world.

Definition at line 147 of file RasterBuffer.cpp.

References KKB::GoalKeeper::EndBlock(), and KKB::GoalKeeper::StartBlock().

148 {
149  kkint32 result = 0;
150  gateKeeper->StartBlock ();
151  result = memoryConsumed;
152  gateKeeper->EndBlock ();
153  return result;
154 } /* MemoryConsumedEstimated */
__int32 kkint32
Definition: KKBaseTypes.h:88
void StartBlock()
Initiates a Block as long as another thread has not already locked this object.
Definition: GoalKeeper.cpp:214
void EndBlock()
Ends the block and allows other threads to pass through StatBlock.
Definition: GoalKeeper.cpp:295
kkint32 RasterBuffer::NumAvailable ( ) const

The number of entries that are left in the buffer before 'maxNumOfBuffers' is reached.

Definition at line 93 of file RasterBuffer.cpp.

94 {
95  return maxNumOfBuffers - (kkint32)buffer.size ();
96 }
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 RasterBuffer::NumPopulated ( ) const

Definition at line 99 of file RasterBuffer.cpp.

100 {
101  return (kkint32)buffer.size ();
102 }
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 KKB::RasterBuffer::RastersDropped ( ) const
inline

Returns the number of 'Raster' instances that had to be deleted because the size of the queue had reached 'maxNumOfBuffers'.

Definition at line 53 of file RasterBuffer.h.

53 {return rastersDropped;}

The documentation for this class was generated from the following files: