paludis Version 3.0.2
The one true package mangler
save.hh
Go to the documentation of this file.
1/* vim: set sw=4 sts=4 et foldmethod=syntax : */
2
3/*
4 * Copyright (c) 2005, 2006, 2007, 2008 Ciaran McCreesh
5 *
6 * This file is part of the Paludis package manager. Paludis is free software;
7 * you can redistribute it and/or modify it under the terms of the GNU General
8 * Public License version 2, as published by the Free Software Foundation.
9 *
10 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#ifndef PALUDIS_GUARD_PALUDIS_SAVE_HH
21#define PALUDIS_GUARD_PALUDIS_SAVE_HH 1
22
24#include <functional>
25
26/** \file
27 * Declarations for the Save and RunOnDestruction classes.
28 *
29 * \ingroup g_utils
30 *
31 * \section Examples
32 *
33 * - None at this time.
34 */
35
36namespace paludis
37{
38 /**
39 * Save the value of a particular variable and assign it a new value for the
40 * duration of the Save instance's lifetime (RAII, see \ref EffCpp item 13 or
41 * \ref TCppPL section 14.4).
42 *
43 * \ingroup g_utils
44 * \nosubgrouping
45 */
46 template <typename T_>
47 class Save
48 {
49 private:
50 T_ * const _ptr;
51 const T_ _value;
52
53 Save(const Save &);
54 void operator= (const Save &);
55
56 public:
57 ///\name Basic operations
58 ///\{
59
60 /**
61 * Constructor.
62 */
63 Save(T_ * const p) :
64 _ptr(p),
65 _value(*p)
66 {
67 }
68
69 /**
70 * Constructor, with convenience assignment to new_value.
71 */
72 Save(T_ * const p, const T_ & new_value) :
73 _ptr(p),
74 _value(*p)
75 {
76 *p = new_value;
77 }
78
79 /**
80 * Destructor.
81 */
83 {
84 *_ptr = _value;
85 }
86
87 ///\}
88 };
89
90 /**
91 * Run the supplied function when the class is destructed.
92 *
93 * \ingroup g_utils
94 * \since 0.26
95 */
96 class PALUDIS_VISIBLE RunOnDestruction
97 {
98 private:
99 RunOnDestruction(const RunOnDestruction &);
100 void operator= (const RunOnDestruction &);
101
102 const std::function<void ()> _f;
103
104 public:
105 RunOnDestruction(const std::function<void ()> & f) :
106 _f(f)
107 {
108 }
109
110 ~RunOnDestruction()
111 {
112 _f();
113 }
114 };
115
116}
117
118#endif
119
Save(T_ *const p)
Definition save.hh:63
Save(T_ *const p, const T_ &new_value)
Definition save.hh:72
~Save()
Definition save.hh:82
#define PALUDIS_VISIBLE
Definition attributes.hh:59
Definition about_metadata-fwd.hh:24