~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/components/ogre/SceneManagers/EmberPagingSceneManager/include/OgrePagingLandScapePoolSet.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-07-23 07:46:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090723074640-wh0ukzis0kda36qv
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
-----------------------------------------------------------------------------
 
3
This source file is part of OGRE
 
4
(Object-oriented Graphics Rendering Engine)
 
5
For the latest info, see http://www.ogre3d.org/
 
6
 
 
7
Copyright (c) 2000-2006 The OGRE Team
 
8
Also see acknowledgements in Readme.html
 
9
 
 
10
This program is free software; you can redistribute it and/or modify it under
 
11
the terms of the GNU Lesser General Public License as published by the Free Software
 
12
Foundation; either version 2 of the License, or (at your option) any later
 
13
version.
 
14
 
 
15
This program is distributed in the hope that it will be useful, but WITHOUT
 
16
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
17
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 
18
 
 
19
You should have received a copy of the GNU Lesser General Public License along with
 
20
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
21
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
 
22
http://www.gnu.org/copyleft/lesser.txt.
 
23
-----------------------------------------------------------------------------
 
24
*/
 
25
 
 
26
#ifndef __OgrePagingLandScapePoolSet_H__
 
27
#define __OgrePagingLandScapePoolSet_H__
 
28
 
 
29
#include <list>
 
30
#include <vector>
 
31
 
 
32
 
 
33
namespace Ogre 
 
34
{
 
35
        /** A collection of pre-allocated object instance of a same base class
 
36
        */
 
37
        template <class T> class PoolSet 
 
38
        {
 
39
        protected:
 
40
 
 
41
                typedef std::list<T*> ActivePoolableList;
 
42
                typedef std::list<T*> FreePoolableList;
 
43
                typedef std::vector<T*> PoolablePool;
 
44
 
 
45
        public:
 
46
 
 
47
                /** Usual constructor 
 
48
                @param
 
49
                poolSize The initial size of the object pool. Estimate of the number of objects
 
50
                which will be required, and pass it using this parameter. The set will
 
51
                preallocate this number to avoid memory fragmentation. The default behaviour
 
52
                once this pool has run out is to double it.            
 
53
                @see
 
54
                PoolSet::setAutoextend
 
55
                */
 
56
                PoolSet(unsigned int poolSize = 0,  bool autoExtendPool = true):
 
57
                                mAutoExtendPool(autoExtendPool),
 
58
                                mAutoExtendFactor(2.0f),
 
59
                                mPoolSize(poolSize)
 
60
                  {
 
61
                          assert (mFreePoolables.empty());
 
62
                          assert (mActivePoolables.empty());
 
63
                          assert (mPoolablePool.empty());
 
64
 
 
65
                          setPoolSize(poolSize);
 
66
                  }
 
67
                  /** 
 
68
                        dtor
 
69
                  */
 
70
                  virtual ~PoolSet() 
 
71
                  {
 
72
                          assert (mPoolSize == 0 && mPoolablePool.empty());
 
73
                  }
 
74
                  /** 
 
75
                        empty totally the pool.
 
76
                  */
 
77
                  void deletePool() 
 
78
                  {
 
79
                          // Free pool items
 
80
                          for (typename PoolablePool::iterator i = mPoolablePool.begin(); 
 
81
                                  i != mPoolablePool.end(); ++i)
 
82
                          {   
 
83
                                  deallocate (*i);
 
84
                          }
 
85
                          mPoolablePool.clear();
 
86
                          mFreePoolables.clear();
 
87
                          mActivePoolables.clear();
 
88
                          mPoolSize = 0;
 
89
                  }
 
90
                  /** Creates a new pooled object and adds it to this set.
 
91
                  @remarks
 
92
                  Behavior once the query pool has been exhausted depends on the
 
93
                  PoolSet::setAutoextendPool option.         
 
94
                  @returns
 
95
                  On success, a pointer to a newly created Poolable is
 
96
                  returned.
 
97
                  @par
 
98
                  On failure (i.e. no more space and can't autoextend),
 
99
                  <b>NULL</b> is returned.
 
100
                  @see
 
101
                  PoolSet::setAutoextend
 
102
                  */
 
103
                  T* getPoolable() 
 
104
                  {
 
105
                          if (mFreePoolables.empty())
 
106
                          {
 
107
                                  if (mAutoExtendPool)
 
108
                                  {
 
109
                                          autoExtend ();   
 
110
                                  }
 
111
                                  else
 
112
                                  {
 
113
                                          return 0;
 
114
                                  }
 
115
                          }
 
116
                          // Get a new Object
 
117
                          T* newPoolable = mFreePoolables.front();
 
118
                          mFreePoolables.pop_front();
 
119
                          mActivePoolables.push_back(newPoolable); 
 
120
 
 
121
                          return newPoolable;
 
122
                  }
 
123
                  /*
 
124
                  Extend the pool Size by a mAutoExtendFactor factor (default 2.0f)
 
125
                  */
 
126
                  void autoExtend()
 
127
                  {
 
128
                          unsigned int newSize = (unsigned int)(mPoolSize * mAutoExtendFactor);
 
129
                          if (newSize <= mPoolSize)
 
130
                                  newSize = mPoolSize + 1;
 
131
                          setPoolSize (newSize);
 
132
                  }
 
133
                  /*
 
134
                  Extend the pool Size by this factor (default is 2.0f)
 
135
                  */
 
136
                  void setExtendFactor(const float factor)
 
137
                  {
 
138
                          assert(factor >= 1.0f);// otherwise you'll make it smaller
 
139
                          mAutoExtendFactor = factor;
 
140
                  }
 
141
                  /** Removes a pooled object from the set.
 
142
                  @note
 
143
                  This version is more efficient than removing by index.
 
144
                  */
 
145
                  void removePoolable(T * const pPoolable)
 
146
                  {
 
147
                          mActivePoolables.remove (pPoolable);
 
148
                          mFreePoolables.push_back (pPoolable);
 
149
                  }
 
150
                  /** Returns the number of active object which currently make up this set.
 
151
                  */
 
152
                  size_t getActivePoolablesSize(void) const
 
153
                  {
 
154
                          return static_cast< size_t >(mActivePoolables.size());
 
155
                  }
 
156
                  ActivePoolableList &getActivePoolables(void) 
 
157
                  {
 
158
                          return mActivePoolables;
 
159
                  }
 
160
                  /** Tells the set whether to allow automatic extension of the pool of objects.
 
161
                  @remarks
 
162
                  A PoolSet stores a pool of pre-constructed queries which are used as needed when
 
163
                  a new object is requested. This allows applications to create / remove objects efficiently
 
164
                  without incurring construction / destruction costs. This method allows you to configure
 
165
                  the behavior when a new object is requested  but the object pool has been exhausted.
 
166
                  @par
 
167
                  The default behavior is to allow the pool to extend (typically this allocates double the current
 
168
                  pool of queries when the pool is expended), equivalent to calling this method with
 
169
                  autoExtend = true. If you set the parameter to false however, any attempt to create a new pooled object
 
170
                  when the pool has expired will simply fail silently, returning a null pointer.
 
171
                  @param autoextend true to double the pool every time it runs out, false to fail silently.
 
172
                  */
 
173
                  void setAutoextend(bool autoextend)
 
174
                  {
 
175
                          mAutoExtendPool = autoextend;
 
176
                  }
 
177
                  /** Returns true if the object pool automatically extends.
 
178
                  @see
 
179
                  PoolSet::setAutoextend
 
180
                  */
 
181
                  bool getAutoextend(void) const
 
182
                  {
 
183
                          return mAutoExtendPool;
 
184
                  }
 
185
                  /** Adjusts the size of the pool of object available in this set.
 
186
                  @remarks
 
187
                  See the PoolSet::setAutoextend method for full details of the query pool. This method adjusts
 
188
                  the preallocated size of the pool. If you try to reduce the size of the pool, the set has the option
 
189
                  of ignoring you if too many objects are already in use. Bear in mind that calling this method will
 
190
                  incur significant construction / destruction calls so should be avoided in time-critical code. The same
 
191
                  goes for auto-extension, try to avoid it by estimating the pool size correctly up-front.
 
192
                  @param
 
193
                  size The new size for the pool.
 
194
                  */
 
195
                  void setPoolSize(const unsigned int size)
 
196
                  {
 
197
                          // Never shrink below size()
 
198
                          const size_t currSize = mPoolablePool.size();
 
199
 
 
200
                          if(currSize < size)
 
201
                          {
 
202
                                  increasePool(size);
 
203
                                  for (size_t i = currSize; i < size; ++i)
 
204
                                  {
 
205
                                          // Add new items to the queue
 
206
                                          mFreePoolables.push_back (mPoolablePool[i]);
 
207
                                  }
 
208
                                  mPoolSize = size;
 
209
                          }
 
210
                  }
 
211
                  /** Returns the current size of the object pool.
 
212
                  @returns
 
213
                  The current size of the object pool.
 
214
                  @see
 
215
                  PoolSet::setAutoextend
 
216
                  */
 
217
                  unsigned int getPoolSize(void) const
 
218
                  {
 
219
                          return static_cast< unsigned int >(mPoolablePool.size());
 
220
                  }
 
221
                  /** Empties this set of all Actives object (but do not delete them).
 
222
                  */
 
223
                  void clear()
 
224
                  {
 
225
                          // Insert actives into free list
 
226
                          mFreePoolables.insert(mFreePoolables.end(), mActivePoolables.begin(), mActivePoolables.end());
 
227
 
 
228
                          // Remove all active instances
 
229
                          mActivePoolables.clear(); 
 
230
                  }
 
231
 
 
232
        protected:
 
233
                /** Active object list.
 
234
                @remarks
 
235
                This is a linked list of pointers to objects in the object pool.
 
236
                @par
 
237
                This allows very fast insertions and deletions from anywhere in the list to activate / deactivate objects
 
238
                (required for particle systems etc) as well as reuse of Poolable instances in the pool
 
239
                without construction & destruction which avoids memory thrashing.
 
240
                */
 
241
                ActivePoolableList mActivePoolables;
 
242
                /** Free object queue.
 
243
                @remarks
 
244
                This contains a list of the objects free for use as new instances
 
245
                as required by the set. Poolable instances are pre-constructed up to the estimated size in the
 
246
                mPoolablePool vector and are referenced on this deque at startup. As they get used this deque
 
247
                reduces, as they get released back to to the set they get added back to the deque.
 
248
                */
 
249
                FreePoolableList mFreePoolables;
 
250
                /** Pool of objects instances for use and reuse in the active query list.
 
251
                @remarks
 
252
                This vector will be preallocated with the estimated size of the set,and will extend as required.
 
253
                */
 
254
                PoolablePool mPoolablePool;
 
255
                /// The number of query in the pool.
 
256
                unsigned int mPoolSize;
 
257
                /// Flag indicating whether to auto extend pool
 
258
                bool mAutoExtendPool;
 
259
                // by how much we will auto extend current pool
 
260
                Real mAutoExtendFactor;
 
261
 
 
262
                /// method for increasing pool size
 
263
                void increasePool(unsigned int size)
 
264
                {
 
265
                        assert (size > 0);
 
266
                        const size_t oldSize = mPoolablePool.size();
 
267
 
 
268
                        // Increase size
 
269
                        mPoolablePool.reserve(size);
 
270
                        mPoolablePool.resize(size);
 
271
 
 
272
                        // Create new queries
 
273
                        for (size_t i = oldSize; i < size; ++i)
 
274
                                mPoolablePool[i] = allocate();
 
275
                }
 
276
        protected :
 
277
                virtual T* allocate() = 0;
 
278
                virtual void deallocate(T* p) = 0;
 
279
 
 
280
        };
 
281
 
 
282
}
 
283
 
 
284
#endif//__OgrePagingLandScapePoolSet_H__