summaryrefslogtreecommitdiff
path: root/louloulibs/utils/scopeguard.hpp
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2017-06-24 09:21:37 +0200
committerJonas Smedegaard <dr@jones.dk>2017-06-24 09:21:37 +0200
commit3d39f109ba8ea7ae9778c58bd1665b9e8e0f45cb (patch)
tree9a5684babcd16d302fbe59c56b6045660ad62488 /louloulibs/utils/scopeguard.hpp
parentf9cee98aacd6aea8ccb7f5677b4ff1e1e234e4d1 (diff)
parentc21cbbf9667991d2b928562a9c199e625d3f9bba (diff)
downloadbiboumi-3d39f109ba8ea7ae9778c58bd1665b9e8e0f45cb.tar.gz
biboumi-3d39f109ba8ea7ae9778c58bd1665b9e8e0f45cb.tar.bz2
biboumi-3d39f109ba8ea7ae9778c58bd1665b9e8e0f45cb.tar.xz
biboumi-3d39f109ba8ea7ae9778c58bd1665b9e8e0f45cb.zip
Updated version 5.0 from 'upstream/5.0'
with Debian dir 2ae31d03ffb1d79153a692af23c7b2b097cc4b2b
Diffstat (limited to 'louloulibs/utils/scopeguard.hpp')
-rw-r--r--louloulibs/utils/scopeguard.hpp96
1 files changed, 0 insertions, 96 deletions
diff --git a/louloulibs/utils/scopeguard.hpp b/louloulibs/utils/scopeguard.hpp
deleted file mode 100644
index cd0e89e..0000000
--- a/louloulibs/utils/scopeguard.hpp
+++ /dev/null
@@ -1,96 +0,0 @@
-#pragma once
-
-#include <functional>
-#include <memory>
-#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));
- }
-
- ScopeGuard(const ScopeGuard&) = delete;
- ScopeGuard& operator=(ScopeGuard&&) = delete;
- ScopeGuard(ScopeGuard&&) = delete;
- ScopeGuard& operator=(const ScopeGuard&) = delete;
-
- /**
- * 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;
-
-};
-
-template<typename F>
-auto make_scope_guard(F&& f)
-{
- return std::unique_ptr<void, std::decay_t<F>>{(void*)1, std::forward<F>(f)};
-}
-
-}
-