Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

StackSwapper.h

00001 #ifndef _STR_StackSwapper_H_  // -*-c++-*-
00002 
00003 /*
00004  * Copyright 2000 by Eric M. Hopper <hopper@omnifarious.mn.org>
00005  * 
00006  *     This program is free software; you can redistribute it and/or modify it
00007  *     under the terms of the GNU Lesser General Public License as published
00008  *     by the Free Software Foundation; either version 2 of the License, or
00009  *     (at your option) any later version.
00010  * 
00011  *     This program is distributed in the hope that it will be useful, but
00012  *     WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *     Lesser General Public License for more details.
00015  * 
00016  *     You should have received a copy of the GNU Lesser General Public
00017  *     License along with this program; if not, write to the Free Software
00018  *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #ifdef __GNUG__
00022 #  pragma interface
00023 #endif
00024 
00025 /* $Header: /home/hopper/src/cvs/C++/StrMod/StrMod/StackSwapper.h,v 1.5 2001/09/23 22:40:43 hopper Exp $ */
00026 
00027 // For a log, see ../ChangLog
00028 
00029 #define _STR_StackSwapper_H_
00030 
00031 namespace strmod {
00032 namespace strmod {
00033 
00034 /** \class StackSwapper StackSwapper.h StrMod/StackSwapper.h
00035  * A class to make a certain means of doing dynamic scoping in a lexically
00036  * scoped language exception safe.
00037  *
00038  * This could be thought of as maintaining a particular global or member
00039  * variable (<code>classvar</code>) as the top stack value in a recursive
00040  * function that maintained a stack of values on the execution stack.
00041  *
00042  * This establishes something akin to dynamic scope.
00043  *
00044  * Using the constructor and destructor to maintain these values causes this
00045  * manipulation to be exception safe.  It eliminates the need for a lot of
00046  * <code>try { } catch (...) { }</code> blocks to clean stuff up and make
00047  * things exception safe. */
00048 template <class T> class StackSwapper {
00049  public:
00050    /** Saves the value in classvar, then sets classvar to newval.
00051     * @param classvar A reference to the variable who's valued is to be saved,
00052     * then restored on scope exit.
00053     *
00054     * @param newval The new value to be stored in classvar.
00055     */
00056    inline StackSwapper(T &classvar, const T &newval);
00057    /** \brief Restores the value of the variable referenced by classvar in the
00058     * constructor.
00059     */
00060    inline ~StackSwapper();
00061 
00062  private:
00063    T &classvar_;
00064    T oldval_;
00065 
00066    // Left private and undefined on purpose.
00067    void operator =(const StackSwapper<T> &b);
00068    StackSwapper(const StackSwapper &b);
00069 };
00070 
00071 //-----------------------------inline functions--------------------------------
00072 
00073 template <class T>
00074 inline StackSwapper<T>::StackSwapper(T &classvar, const T &newval)
00075      : classvar_(classvar), oldval_(classvar)
00076 {
00077    classvar = newval;
00078 }
00079 
00080 template <class T>
00081 inline StackSwapper<T>::~StackSwapper()
00082 {
00083    classvar_ = oldval_;
00084 }
00085 
00086 };  // namespace strmod
00087 };  // namespace strmod
00088 
00089 #endif

Generated on Wed Jan 29 00:32:44 2003 for libNet by doxygen1.3-rc1