00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 namespace paludis
00037 {
00038
00039
00040
00041
00042
00043
00044
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
00058
00059
00060
00061
00062
00063 Save(T_ * const p) :
00064 _ptr(p),
00065 _value(*p)
00066 {
00067 }
00068
00069
00070
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
00081
00082 ~Save()
00083 {
00084 *_ptr = _value;
00085 }
00086
00087
00088 };
00089
00090
00091
00092
00093
00094
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