casa
$Rev:20696$
|
00001 //# ObjectStack.h: A stack of re-usable objects 00002 //# Copyright (C) 2007 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id: ObjectStack.h,v 1.1 2007/11/16 04:08:17 wbrouw Exp $ 00027 00028 #ifndef CASA_OBJECTSTACK_H 00029 #define CASA_OBJECTSTACK_H 00030 00031 //# Includes 00032 #include <casa/aips.h> 00033 #include <casa/vector.h> 00034 #include <casa/OS/Mutex.h> 00035 00036 namespace casa { //# NAMESPACE CASA - BEGIN 00037 00038 //# Forward declarations 00039 00040 // <summary> 00041 // A stack of re-usable objects 00042 // </summary> 00043 // 00044 // <use visibility=export> 00045 // 00046 // <reviewed reviewer="Ger van Diepen" date="2001/07/03" tests="tObjectStack.cc" demos=""> 00047 // </reviewed> 00048 // 00049 // <prerequisite> 00050 // <li> 00051 // </prerequisite> 00052 // 00053 // <synopsis> 00054 // An ObjectStack contains a set of pre-allocated Objects of the type 00055 // <src>T</src>. 00056 // The stack is a very simple stack, without the 00057 // linking/unlinking of a normal Stack implementation. 00058 // This lightweight implementation was especially designed for use 00059 // with the <linkto class=AutoDiff>AutoDiff</linkto> 00060 // classes, but can be used independently. The stack works best with small 00061 // object sizes, or letter/envelope classes. 00062 // 00063 // The class is fully thread-safe, thus the same object can be used safely 00064 // in multiple threads. 00065 // 00066 // </synopsis> 00067 // 00068 // <example> 00069 // <srcblock> 00070 // { 00071 // // Get an element (and create stack!) 00072 // SparseDiff<Double> elem; 00073 // // Use it 00074 // elem.value() = 27; 00075 // // Release it (automatic by dtor on elem) 00076 // } 00077 // </srcblock> 00078 // </example> 00079 // 00080 // <motivation> 00081 // To improve the speed for the auto differentiating classes. 00082 // </motivation> 00083 // 00084 // <templating arg=T> 00085 // <li> the class T must have a <em>constructor(T::FULLREFRESH)</em> 00086 // for creating new entries and destructor; 00087 // and must possess a <em>clear()</em> method to enable element re-use. 00088 // </templating> 00089 // 00090 // <todo asof="2007/11/27"> 00091 // </todo> 00092 00093 template <class T> class ObjectStack { 00094 public: 00095 00096 //# Member functions 00097 // Destructor 00098 ~ObjectStack(); 00099 00100 // Create a singleton stack 00101 static ObjectStack<T> &stack(); 00102 00103 // Get a pointer to an object in the stack. The stack will be extended if 00104 // no objects left. 00105 T *get(); 00106 00107 // Return an object to the stack for re-use 00108 void put(T *obj) { obj->clear(); stack_p.push_back(obj); }; 00109 00110 // Decimate the stack by getting rid of all unused elements in it 00111 void clear(); 00112 00113 // Test if stack empty 00114 Bool empty() { return stack_p.empty(); }; 00115 00116 // return the stack extend (for debugging use and checking mainly) 00117 uInt nelements() const { return stack_p.size(); }; 00118 00119 private: 00120 //# Data 00121 // The Stack 00122 vector<T*> stack_p; 00123 Mutex mutex_p; 00124 00125 //# Constructors 00126 // All ctor and assignment constructors and assignment (not implemented) 00127 // <group> 00128 ObjectStack() : stack_p() {}; 00129 ObjectStack(const ObjectStack<T> &other); 00130 ObjectStack<T> &operator=(const ObjectStack<T> &other); 00131 // </group> 00132 00133 //# Member functions 00134 }; 00135 00136 00137 } //# NAMESPACE CASA - END 00138 00139 #ifndef CASACORE_NO_AUTO_TEMPLATES 00140 #include <casa/Containers/ObjectStack.tcc> 00141 #endif //# CASACORE_NO_AUTO_TEMPLATES 00142 #endif