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