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

LocalCopy.h

00001 #ifndef _STR_LocalCopy_H_  // -*-c++-*-
00002 
00003 /*
00004  * Copyright 2001 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/LocalCopy.h,v 1.2 2001/09/23 22:40:43 hopper Exp $ */
00026 
00027 // For a log, see ../ChangeLog
00028 
00029 #define _STR_LocalCopy_H_
00030 
00031 namespace strmod {
00032 namespace strmod {
00033 
00034 /** \class LocalCopy LocalCopy.h StrMod/LocalCopy.h
00035  * \brief Makes a local copy of a variable the compiler doesn't have enough
00036  * information to do CSE on.
00037  *
00038  * This will make a local copy of a member variable and store the value of
00039  * that copy back into the member variable when the local copy is destroyed.
00040  * This is useful for a member variable you know will only be altered by the
00041  * function across an entire call, despite other functions being called.  When
00042  * other functions are called, the compiler's CSE throws out member variables,
00043  * as those variables may concievably be altered by the function call.
00044  */
00045 template <class T>
00046 class LocalCopy {
00047  public:
00048    /** Constructs a local copy of a member or global variable.
00049     * @param classvar A reference to the variable to make a copy of.
00050     */
00051    LocalCopy(T &classvar) : local(classvar), save_(classvar)  { }
00052    /** \brief Stores the local copy back to original member or global variable
00053     * referenced by classvar in the constructor.
00054     */
00055    ~LocalCopy()                                               { save_ = local; }
00056 
00057    T local;  //!< Yes, this is supposed to be public.
00058 
00059    //! Trying to make this stand in for the variable as much as possible.
00060    operator T &()                                             { return local; }
00061    //! Trying to make this stand in for the variable as much as possible.
00062    operator const T &() const                                 { return local; }
00063    //! Trying to make this stand in for the variable as much as possible.
00064    inline const T &operator =(const T &b);
00065 
00066  private:
00067    T &save_;
00068 };
00069 
00070 //-----------------------------inline functions--------------------------------
00071 
00072 template <class T>
00073 const T &LocalCopy<T>::operator =(const T &b)
00074 {
00075    return local = b;
00076 }
00077 
00078 };  // namespace strmod
00079 };  // namespace strmod
00080 
00081 #endif

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