OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimPool.h
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: See top level LICENSE.txt file.
4 //
5 // Author: Garrett Potts
6 //
7 //**********************************************************************
8 // $Id: ossimPool.h 9968 2006-11-29 14:01:53Z gpotts $
9 #ifndef ossimPool_HEADER
10 #define ossimPool_HEADER
11 #include <iostream>
12 #include <vector>
13 #include <exception>
14 #include <stdexcept>
15 #include <ossim/base/ossimCommon.h>
19 
20 #define OSSIM_DEFAULT_POOL_SIZE (ossim_uint32)10
21 
25 template <class T>
26 class ossimPool
27 {
28  public:
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  }
52 
57  {
58  thePool.clear();
59  }
60 
65  T* checkout()
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  }
82 
92  void checkin(T* obj) throw(std::out_of_range)
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  }
109 
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  }
124 
125  template <class C> friend std::ostream& operator<<(std::ostream& out,
126  const ossimPool<C>& obj)
127  {
128  return obj.print(out);
129  }
130 
131 protected:
132 
133  std::vector<ossimPoolObject<T>*> thePool;
134 };
135 
136 #endif /* #ifndef ossimPool_HEADER */
137 
T * checkout()
Definition: ossimPool.h:65
Container pool to be used for things like threads.
Definition: ossimPool.h:26
ossimPool(ossim_uint32 n=OSSIM_DEFAULT_POOL_SIZE)
Contructor.
Definition: ossimPool.h:33
#define OSSIM_DEFAULT_POOL_SIZE
Definition: ossimPool.h:20
std::vector< ossimPoolObject< T > * > thePool
Definition: ossimPool.h:133
os2<< "> n<< " > nendobj n
unsigned int ossim_uint32
~ossimPool()
Destructor.
Definition: ossimPool.h:56
Templated container for an object pointer and a flag.
std::ostream & print(std::ostream &out) const
Outputs object addresses and theAvailableFlag.
Definition: ossimPool.h:113
void checkin(T *obj)
Resets and eturns object to the pool.
Definition: ossimPool.h:92
friend std::ostream & operator<<(std::ostream &out, const ossimPool< C > &obj)
Definition: ossimPool.h:125
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23