save.hh

Go to the documentation of this file.
00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2005, 2006, 2007, 2008 Ciaran McCreesh
00005  *
00006  * This file is part of the Paludis package manager. Paludis is free software;
00007  * you can redistribute it and/or modify it under the terms of the GNU General
00008  * Public License version 2, as published by the Free Software Foundation.
00009  *
00010  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013  * details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef PALUDIS_GUARD_PALUDIS_SAVE_HH
00021 #define PALUDIS_GUARD_PALUDIS_SAVE_HH 1
00022 
00023 #include <paludis/util/attributes.hh>
00024 #include <tr1/functional>
00025 
00026 /** \file
00027  * Declarations for the Save and RunOnDestruction classes.
00028  *
00029  * \ingroup g_utils
00030  *
00031  * \section Examples
00032  *
00033  * - None at this time.
00034  */
00035 
00036 namespace paludis
00037 {
00038     /**
00039      * Save the value of a particular variable and assign it a new value for the
00040      * duration of the Save instance's lifetime (RAII, see \ref EffCpp item 13 or
00041      * \ref TCppPL section 14.4).
00042      *
00043      * \ingroup g_utils
00044      * \nosubgrouping
00045      */
00046     template <typename T_>
00047     class Save
00048     {
00049         private:
00050             T_ * const _ptr;
00051             const T_ _value;
00052 
00053             Save(const Save &);
00054             void operator= (const Save &);
00055 
00056         public:
00057             ///\name Basic operations
00058             ///\{
00059 
00060             /**
00061              * Constructor.
00062              */
00063             Save(T_ * const p) :
00064                 _ptr(p),
00065                 _value(*p)
00066             {
00067             }
00068 
00069             /**
00070              * Constructor, with convenience assignment to new_value.
00071              */
00072             Save(T_ * const p, const T_ & new_value) :
00073                 _ptr(p),
00074                 _value(*p)
00075             {
00076                 *p = new_value;
00077             }
00078 
00079             /**
00080              * Destructor.
00081              */
00082             ~Save()
00083             {
00084                 *_ptr = _value;
00085             }
00086 
00087             ///\}
00088     };
00089 
00090     /**
00091      * Run the supplied function when the class is destructed.
00092      *
00093      * \ingroup g_utils
00094      * \since 0.26
00095      */
00096     class PALUDIS_VISIBLE RunOnDestruction
00097     {
00098         private:
00099             RunOnDestruction(const RunOnDestruction &);
00100             void operator= (const RunOnDestruction &);
00101 
00102             const std::tr1::function<void ()> _f;
00103 
00104         public:
00105             RunOnDestruction(const std::tr1::function<void ()> & f) :
00106                 _f(f)
00107             {
00108             }
00109 
00110             ~RunOnDestruction()
00111             {
00112                 _f();
00113             }
00114     };
00115 
00116 }
00117 
00118 #endif
00119 

Generated on Mon Sep 21 10:36:08 2009 for paludis by  doxygen 1.5.4