casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ObjectStack.h
Go to the documentation of this file.
1 //# ObjectStack.h: A stack of re-usable objects
2 //# Copyright (C) 2007
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: ObjectStack.h,v 1.1 2007/11/16 04:08:17 wbrouw Exp $
27 
28 #ifndef CASA_OBJECTSTACK_H
29 #define CASA_OBJECTSTACK_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
33 #include <casacore/casa/vector.h>
34 #include <casacore/casa/OS/Mutex.h>
35 
36 namespace casacore { //# NAMESPACE CASACORE - BEGIN
37 
38  //# Forward declarations
39 
40  // <summary>
41  // A stack of re-usable objects
42  // </summary>
43  //
44  // <use visibility=export>
45  //
46  // <reviewed reviewer="Ger van Diepen" date="2001/07/03" tests="tObjectStack.cc" demos="">
47  // </reviewed>
48  //
49  // <prerequisite>
50  // <li>
51  // </prerequisite>
52  //
53  // <synopsis>
54  // An ObjectStack contains a set of pre-allocated Objects of the type
55  // <src>T</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=AutoDiff>AutoDiff</linkto>
60  // classes, but can be used independently. The stack works best with small
61  // object sizes, or letter/envelope classes.
62  //
63  // The class is fully thread-safe, thus the same object can be used safely
64  // in multiple threads.
65  //
66  // </synopsis>
67  //
68  // <example>
69  // <srcblock>
70  // {
71  // // Get an element (and create stack!)
72  // SparseDiff<Double> elem;
73  // // Use it
74  // elem.value() = 27;
75  // // Release it (automatic by dtor on elem)
76  // }
77  // </srcblock>
78  // </example>
79  //
80  // <motivation>
81  // To improve the speed for the auto differentiating classes.
82  // </motivation>
83  //
84  // <templating arg=T>
85  // <li> the class T must have a <em>constructor(T::FULLREFRESH)</em>
86  // for creating new entries and destructor;
87  // and must possess a <em>clear()</em> method to enable element re-use.
88  // </templating>
89  //
90  // <todo asof="2007/11/27">
91  // </todo>
92 
93  template <class T> class ObjectStack {
94  public:
95 
96  //# Member functions
97  // Destructor
98  ~ObjectStack();
99 
100  // Create a singleton stack
101  static ObjectStack<T> &stack();
102 
103  // Get a pointer to an object in the stack. The stack will be extended if
104  // no objects left.
105  T *get();
106 
107  // Return an object to the stack for re-use
108  void put(T *obj) { obj->clear(); stack_p.push_back(obj); };
109 
110  // Decimate the stack by getting rid of all unused elements in it
111  void clear();
112 
113  // Test if stack empty
114  Bool empty() { return stack_p.empty(); };
115 
116  // return the stack extend (for debugging use and checking mainly)
117  uInt nelements() const { return stack_p.size(); };
118 
119  private:
120  //# Data
121  // The Stack
122  vector<T*> stack_p;
124 
125  //# Constructors
126  // All ctor and assignment constructors and assignment (not implemented)
127  // <group>
129  ObjectStack(const ObjectStack<T> &other);
130  ObjectStack<T> &operator=(const ObjectStack<T> &other);
131  // </group>
132 
133  //# Member functions
134  };
135 
136 
137 } //# NAMESPACE CASACORE - END
138 
139 #ifndef CASACORE_NO_AUTO_TEMPLATES
140 #include <casacore/casa/Containers/ObjectStack.tcc>
141 #endif //# CASACORE_NO_AUTO_TEMPLATES
142 #endif
Bool empty()
Test if stack empty.
Definition: ObjectStack.h:114
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
ObjectStack< T > & operator=(const ObjectStack< T > &other)
void clear()
Decimate the stack by getting rid of all unused elements in it.
void put(T *obj)
Return an object to the stack for re-use.
Definition: ObjectStack.h:108
Wrapper around a pthreads mutex.
Definition: Mutex.h:58
uInt nelements() const
return the stack extend (for debugging use and checking mainly)
Definition: ObjectStack.h:117
static ObjectStack< T > & stack()
Create a singleton stack.
ObjectStack()
All ctor and assignment constructors and assignment (not implemented)
Definition: ObjectStack.h:128
vector< T * > stack_p
The Stack.
Definition: ObjectStack.h:117
A stack of re-usable objects.
Definition: ObjectStack.h:93
unsigned int uInt
Definition: aipstype.h:51
~ObjectStack()
Destructor.
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42