OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Public Member Functions | Protected Attributes | Friends | List of all members
ossimPool< T > Class Template Reference

Container pool to be used for things like threads. More...

#include <ossimPool.h>

Public Member Functions

 ossimPool (ossim_uint32 n=OSSIM_DEFAULT_POOL_SIZE) throw (std::bad_alloc)
 Contructor. More...
 
 ~ossimPool ()
 Destructor. More...
 
T * checkout ()
 
void checkin (T *obj) throw (std::out_of_range)
 Resets and eturns object to the pool. More...
 
std::ostream & print (std::ostream &out) const
 Outputs object addresses and theAvailableFlag. More...
 

Protected Attributes

std::vector< ossimPoolObject< T > * > thePool
 

Friends

template<class C >
std::ostream & operator<< (std::ostream &out, const ossimPool< C > &obj)
 

Detailed Description

template<class T>
class ossimPool< T >

Container pool to be used for things like threads.

Definition at line 26 of file ossimPool.h.

Constructor & Destructor Documentation

◆ ossimPool()

template<class T>
ossimPool< T >::ossimPool ( ossim_uint32  n = OSSIM_DEFAULT_POOL_SIZE)
throw (std::bad_alloc
)
inline

Contructor.

Parameters
nNumber of pooled objects to create.

Definition at line 33 of file ossimPool.h.

References n, ossimNotify(), ossimNotifyLevel_FATAL, ossimPoolObject< T >::theAvailableFlag, ossimPoolObject< T >::theObjectPtr, and ossimPool< T >::thePool.

34  : thePool(n)
35  {
36  for(ossim_uint32 idx = 0; idx < n; ++idx)
37  {
38  try
39  {
40  ossimPoolObject<T>* poolObject = new ossimPoolObject<T>();
41  poolObject->theAvailableFlag = true;
42  poolObject->theObjectPtr = new T();
43  thePool[idx] = poolObject;
44  }
45  catch(std::bad_alloc)
46  {
48  << "FATAL ossimPool::ossimPool(): Bad allocation" << endl;
49  }
50  }
51  }
std::vector< ossimPoolObject< T > * > thePool
Definition: ossimPool.h:133
os2<< "> n<< " > nendobj n
unsigned int ossim_uint32
Templated container for an object pointer and a flag.
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)

◆ ~ossimPool()

template<class T>
ossimPool< T >::~ossimPool ( )
inline

Destructor.

Definition at line 56 of file ossimPool.h.

References ossimPool< T >::thePool.

57  {
58  thePool.clear();
59  }
std::vector< ossimPoolObject< T > * > thePool
Definition: ossimPool.h:133

Member Function Documentation

◆ checkin()

template<class T>
void ossimPool< T >::checkin ( T *  obj)
throw (std::out_of_range
)
inline

Resets and eturns object to the pool.

Parameters
objObject to be returned to the pool.
Note
The object is required to have a "reset()" method.
Returns
Returns void unless object was not from pool then throws a std::out_of_range error.

Definition at line 92 of file ossimPool.h.

References ossimPool< T >::thePool.

93  {
94  theCriticalSectionMutex.lock();
95  for (ossim_uint32 idx = 0; idx < thePool.size(); ++idx)
96  {
97  if (obj == thePool[idx]->theObjectPtr)
98  {
99  obj->reset();
100  thePool[idx]->theAvailableFlag = true;
101  theCriticalSectionMutex.unlock();
102  return;
103  }
104  }
105 
106  theCriticalSectionMutex.unlock();
107  throw std::out_of_range(string("ossimPool<T>::checkin Error Object not managed by this pool!"));
108  }
std::vector< ossimPoolObject< T > * > thePool
Definition: ossimPool.h:133
unsigned int ossim_uint32

◆ checkout()

template<class T>
T* ossimPool< T >::checkout ( )
inline
Returns
A pooled object of type T*. Will return null if the pool is used up or pool size is zero.

Definition at line 65 of file ossimPool.h.

References ossimPool< T >::thePool.

66  {
67  T* t = NULL;
68 
69  theCriticalSectionMutex.lock();
70  for (ossim_uint32 idx = 0; idx < thePool.size(); ++idx)
71  {
72  if (thePool[idx]->theAvailableFlag == true)
73  {
74  thePool[idx]->theAvailableFlag = false;
75  t = thePool[idx]->theObjectPtr;
76  break;
77  }
78  }
79  theCriticalSectionMutex.unlock();
80  return t;
81  }
std::vector< ossimPoolObject< T > * > thePool
Definition: ossimPool.h:133
unsigned int ossim_uint32

◆ print()

template<class T>
std::ostream& ossimPool< T >::print ( std::ostream &  out) const
inline

Outputs object addresses and theAvailableFlag.

Definition at line 113 of file ossimPool.h.

References ossimPool< T >::thePool.

114  {
115  for (ossim_uint32 idx = 0; idx < thePool.size(); ++idx)
116  {
117  out << "obj address: " << (hex) << thePool[idx]->theObjectPtr
118  << " is "
119  << (thePool[idx]->theAvailableFlag ? "available":"unavailable")
120  << endl;
121  }
122  return out;
123  }
std::vector< ossimPoolObject< T > * > thePool
Definition: ossimPool.h:133
unsigned int ossim_uint32

Friends And Related Function Documentation

◆ operator<<

template<class T>
template<class C >
std::ostream& operator<< ( std::ostream &  out,
const ossimPool< C > &  obj 
)
friend

Definition at line 125 of file ossimPool.h.

127  {
128  return obj.print(out);
129  }
std::ostream & print(std::ostream &out) const
Outputs object addresses and theAvailableFlag.
Definition: ossimPool.h:113

Member Data Documentation

◆ thePool

template<class T>
std::vector<ossimPoolObject<T>*> ossimPool< T >::thePool
protected

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