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