summaryrefslogtreecommitdiff
path: root/src/utils/scopeguard.hpp
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2015-02-27 12:16:09 +0100
committerFlorent Le Coz <louiz@louiz.org>2015-02-27 12:26:48 +0100
commitd600a2843f1dbe3b1ba2dead9a020cc73d7d10ae (patch)
treea34da2e3c617a39a73f716b9a5dddbdfbc510df6 /src/utils/scopeguard.hpp
parent1028d4d549b517c5b42bb0c30a410d1ab43c4cf3 (diff)
downloadbiboumi-d600a2843f1dbe3b1ba2dead9a020cc73d7d10ae.tar.gz
biboumi-d600a2843f1dbe3b1ba2dead9a020cc73d7d10ae.tar.bz2
biboumi-d600a2843f1dbe3b1ba2dead9a020cc73d7d10ae.tar.xz
biboumi-d600a2843f1dbe3b1ba2dead9a020cc73d7d10ae.zip
Remove all the libs that are now in louloulibs
Diffstat (limited to 'src/utils/scopeguard.hpp')
-rw-r--r--src/utils/scopeguard.hpp89
1 files changed, 0 insertions, 89 deletions
diff --git a/src/utils/scopeguard.hpp b/src/utils/scopeguard.hpp
deleted file mode 100644
index df78831..0000000
--- a/src/utils/scopeguard.hpp
+++ /dev/null
@@ -1,89 +0,0 @@
-#ifndef SCOPEGUARD_HPP
-#define SCOPEGUARD_HPP
-
-#include <functional>
-#include <vector>
-
-/**
- * A class to be used to make sure some functions are called when the scope
- * is left, because they will be called in the ScopeGuard's destructor. It
- * can for example be used to delete some pointer whenever any exception is
- * called. Example:
-
- * {
- * ScopeGuard scope;
- * int* number = new int(2);
- * scope.add_callback([number]() { delete number; });
- * // Do some other stuff with the number. But these stuff might throw an exception:
- * throw std::runtime_error("Some error not caught here, but in our caller");
- * return true;
- * }
-
- * In this example, our pointer will always be deleted, even when the
- * exception is thrown. If we want the functions to be called only when the
- * scope is left because of an unexpected exception, we can use
- * ScopeGuard::disable();
- */
-
-namespace utils
-{
-
-class ScopeGuard
-{
-public:
- /**
- * The constructor can take a callback. But additional callbacks can be
- * added later with add_callback()
- */
- explicit ScopeGuard(std::function<void()>&& func):
- enabled(true)
- {
- this->add_callback(std::move(func));
- }
- /**
- * default constructor, the scope guard is enabled but empty, use
- * add_callback()
- */
- explicit ScopeGuard():
- enabled(true)
- {
- }
- /**
- * Call all callbacks in the desctructor, unless it has been disabled.
- */
- ~ScopeGuard()
- {
- if (this->enabled)
- for (auto& func: this->callbacks)
- func();
- }
- /**
- * Add a callback to be called in our destructor, one scope guard can be
- * used for more than one task, if needed.
- */
- void add_callback(std::function<void()>&& func)
- {
- this->callbacks.emplace_back(std::move(func));
- }
- /**
- * Disable that scope guard, nothing will be done when the scope is
- * exited.
- */
- void disable()
- {
- this->enabled = false;
- }
-
-private:
- bool enabled;
- std::vector<std::function<void()>> callbacks;
-
- ScopeGuard(const ScopeGuard&) = delete;
- ScopeGuard& operator=(ScopeGuard&&) = delete;
- ScopeGuard(ScopeGuard&&) = delete;
- ScopeGuard& operator=(const ScopeGuard&) = delete;
-};
-
-}
-
-#endif