aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2008-01-01 16:58:45 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2008-01-01 16:58:45 +0000
commitb59d7c6e1bd11ec83acfb43e24c02b125312a38f (patch)
treee7c3f7bd47d87662c5e0c69905992993042c4793
parentc22bb23c8a0c37cc817251489a83744eef91117c (diff)
downloadpaludis-b59d7c6e1bd11ec83acfb43e24c02b125312a38f.tar.gz
paludis-b59d7c6e1bd11ec83acfb43e24c02b125312a38f.tar.xz
Add RunOnDestruction to Save
-rw-r--r--paludis/util/save.hh34
-rw-r--r--paludis/util/save_TEST.cc27
2 files changed, 49 insertions, 12 deletions
diff --git a/paludis/util/save.hh b/paludis/util/save.hh
index e83eeee..90f3086 100644
--- a/paludis/util/save.hh
+++ b/paludis/util/save.hh
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2005, 2006, 2007 Ciaran McCreesh
+ * Copyright (c) 2005, 2006, 2007, 2008 Ciaran McCreesh
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -20,8 +20,11 @@
#ifndef PALUDIS_GUARD_PALUDIS_SAVE_HH
#define PALUDIS_GUARD_PALUDIS_SAVE_HH 1
+#include <paludis/util/tr1_functional.hh>
+#include <paludis/util/attributes.hh>
+
/** \file
- * Declarations for the Save class.
+ * Declarations for the Save and RunOnDestruction classes.
*
* \ingroup g_utils
*
@@ -83,6 +86,33 @@ namespace paludis
///\}
};
+
+ /**
+ * Run the supplied function when the class is destructed.
+ *
+ * \ingroup g_utils
+ * \since 0.26
+ */
+ class PALUDIS_VISIBLE RunOnDestruction
+ {
+ private:
+ RunOnDestruction(const RunOnDestruction &);
+ void operator= (const RunOnDestruction &);
+
+ const tr1::function<void ()> _f;
+
+ public:
+ RunOnDestruction(const tr1::function<void ()> & f) :
+ _f(f)
+ {
+ }
+
+ ~RunOnDestruction()
+ {
+ _f();
+ }
+ };
+
}
#endif
diff --git a/paludis/util/save_TEST.cc b/paludis/util/save_TEST.cc
index 9da5b8b..2031e1d 100644
--- a/paludis/util/save_TEST.cc
+++ b/paludis/util/save_TEST.cc
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2005, 2006 Ciaran McCreesh
+ * Copyright (c) 2005, 2006, 2007, 2008 Ciaran McCreesh
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -24,17 +24,8 @@
using namespace test;
using namespace paludis;
-/** \file
- * Test cases for save.hh .
- *
- */
-
namespace test_cases
{
- /**
- * \test Test Save.
- *
- */
struct SaveTest : TestCase
{
SaveTest() : TestCase("save") { }
@@ -62,5 +53,21 @@ namespace test_cases
TEST_CHECK_EQUAL(s, "one");
}
} test_save;
+
+ struct RunOnDestructionTest : TestCase
+ {
+ RunOnDestructionTest() : TestCase("run on destruction") { }
+
+ void run()
+ {
+ std::string s("one");
+ TEST_CHECK_EQUAL(s, "one");
+ {
+ RunOnDestruction save_s(tr1::bind(&std::string::clear, &s));
+ TEST_CHECK_EQUAL(s, "one");
+ }
+ TEST_CHECK_EQUAL(s, "");
+ }
+ } test_run_on_destruction;
}