casacore
PoolStack.h
Go to the documentation of this file.
1 //# PoolStack.h: A parameterized stack of re-usable objects
2 //# Copyright (C) 2001,2002,2004
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASA_POOLSTACK_H
29 #define CASA_POOLSTACK_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
33 #include <casacore/casa/Containers/Block.h>
34 
35 namespace casacore { //# NAMESPACE CASACORE - BEGIN
36 
37 //# Forward declarations
38 
39 // <summary>
40 // A parameterized stack of re-usable objects
41 // </summary>
42 //
43 // <use visibility=export>
44 //
45 // <reviewed reviewer="Ger van Diepen" date="2001/07/03" tests="tPoolStack.cc" demos="">
46 // </reviewed>
47 //
48 // <prerequisite>
49 // <li>
50 // </prerequisite>
51 //
52 // <synopsis>
53 // A PoolStack contains a set of pre-allocated Objects of the type
54 // <src>T</src>, with a parameter <src>Key</src> (e.g. an object could be
55 // a <src>Vector</src> of <src>T Double</src> with an <src>uInt Key</src>).
56 // The stack is a very simple stack, without the
57 // linking/unlinking of a normal Stack implementation.
58 // This lightweight implementation was especially designed for use
59 // with the <linkto class=ObjectPool>ObjectPool</linkto>
60 // class, but can be used independently.
61 //
62 // Objects can be obtained with the <src>get()</src> method, and
63 // returned for re-use with <src>release()</src>.
64 //
65 // Objects are not initialised when popped. The user should never delete the
66 // object returned by get; but return it to the pool.
67 //
68 // PoolStack is not thread-safe, but ObjectPool is.
69 // </synopsis>
70 //
71 // <example>
72 // <srcblock>
73 // // Create a pool of length 5 vectors
74 // PoolStack<Vector<Double>, uInt> pool5(5);
75 // // Get an element
76 // Vector<Double> *elem(pool5.get());
77 // // Use it
78 // (*elem)(2) = 27;
79 // // Release it
80 // pool5.release(elem);
81 // </srcblock>
82 // </example>
83 //
84 // <motivation>
85 // To improve the speed for the auto differentiating class.
86 // </motivation>
87 //
88 // <templating arg=T>
89 // <li> the class T must have a constructor with a Key argument
90 // </templating>
91 //
92 // <templating arg=Key>
93 // <li> the class Key must be usable as a constructor argument for T
94 // </templating>
95 //
96 // <todo asof="2001/06/07">
97 // <li> Nothing I know of
98 // </todo>
99 
100 template <class T, class Key> class PoolStack {
101  public:
102  //# Constants
103  // Number of default stack entries.
104  static const uInt NDEF=8;
105  //# Constructors
106  // Create the stack with the default Key
107  PoolStack();
108  // Create the stack for the specified key
109  explicit PoolStack(const Key &key);
110  // Delete the stack
111  ~PoolStack();
112 
113  //# Member functions
114  // Get a pointer to an object in the stack. The stack will be extended if
115  // no objects left. Extension is done with the NDEF number of elements.
116  // Different extension can be done manually with the addElements() method.
117  T *get() { if (!top_p) addElements(NDEF); T *tmp = stack_p[--top_p];
118  stack_p[top_p] = 0; return tmp; };
119 
120  // Return an object to the stack for re-use
121  void release(T *obj) {if (obj) stack_p[top_p++] = obj; };
122 
123  // Add n elements
124  void addElements(const uInt n);
125 
126  // Decimate the stack by getting rid of all unused elements in it
127  void clear();
128 
129  // Test if stack empty
130  Bool empty() { return top_p == 0; };
131 
132  // Return the key belonging to the stack
133  const Key &key() const { return key_p; }
134  // return the stack extend (for debugging use and checking mainly)
135  uInt nelements() const { return stack_p.nelements(); };
136 
137 private:
138  //# Data
139  // Current pointer to top-of-stack
140  uInt top_p;
141  // The stack
143  // The key belonging to this stack
144  Key key_p;
145 
146  //# Constructors
147  // Copy and assignment constructors and assignment (not implemented)
148  // <group>
149  PoolStack(const PoolStack<T, Key> &other);
151  // </group>
152 
153  //# Member functions
154 };
155 
156 
157 } //# NAMESPACE CASACORE - END
158 
159 #ifndef CASACORE_NO_AUTO_TEMPLATES
160 #include <casacore/casa/Containers/PoolStack.tcc>
161 #endif //# CASACORE_NO_AUTO_TEMPLATES
162 #endif
~PoolStack()
Delete the stack.
void release(T *obj)
Return an object to the stack for re-use.
Definition: PoolStack.h:121
void clear()
Decimate the stack by getting rid of all unused elements in it.
PoolStack< T, Key > & operator=(const PoolStack< T, Key > &other)
size_t nelements() const
Definition: Block.h:888
void addElements(const uInt n)
Add n elements.
Bool empty()
Test if stack empty.
Definition: PoolStack.h:130
uInt nelements() const
return the stack extend (for debugging use and checking mainly)
Definition: PoolStack.h:135
const Key & key() const
Return the key belonging to the stack.
Definition: PoolStack.h:133
static const uInt NDEF
Number of default stack entries.
Definition: PoolStack.h:104
PoolStack()
Create the stack with the default Key.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
A drop-in replacement for Block<T*>.
Definition: Block.h:861
Key key_p
The key belonging to this stack.
Definition: PoolStack.h:144
A parameterized stack of re-usable objects.
Definition: PoolStack.h:100
PtrBlock< T * > stack_p
The stack.
Definition: PoolStack.h:142
this file contains all the compiler specific defines
Definition: mainpage.dox:28
unsigned int uInt
Definition: aipstype.h:51
uInt top_p
Current pointer to top-of-stack.
Definition: PoolStack.h:135