Merge "Allow android::base::ScopeGuard in STL containers" into pi-dev

This commit is contained in:
TreeHugger Robot 2018-03-27 02:29:20 +00:00 committed by Android (Google) Code Review
commit 64250b5abe
2 changed files with 28 additions and 5 deletions

View file

@ -17,20 +17,27 @@
#ifndef ANDROID_BASE_SCOPEGUARD_H
#define ANDROID_BASE_SCOPEGUARD_H
#include <utility> // for std::move
#include <utility> // for std::move, std::forward
namespace android {
namespace base {
// ScopeGuard ensures that the specified functor is executed no matter how the
// current scope exits.
template <typename F>
class ScopeGuard {
public:
ScopeGuard(F f) : f_(f), active_(true) {}
ScopeGuard(F&& f) : f_(std::forward<F>(f)), active_(true) {}
ScopeGuard(ScopeGuard&& that) : f_(std::move(that.f_)), active_(that.active_) {
that.active_ = false;
}
template <typename Functor>
ScopeGuard(ScopeGuard<Functor>&& that) : f_(std::move(that.f_)), active_(that.active_) {
that.active_ = false;
}
~ScopeGuard() {
if (active_) f_();
}
@ -45,13 +52,16 @@ class ScopeGuard {
bool active() const { return active_; }
private:
template <typename Functor>
friend class ScopeGuard;
F f_;
bool active_;
};
template <typename T>
ScopeGuard<T> make_scope_guard(T f) {
return ScopeGuard<T>(f);
template <typename F>
ScopeGuard<F> make_scope_guard(F&& f) {
return ScopeGuard<F>(std::forward<F>(f));
}
} // namespace base

View file

@ -17,6 +17,7 @@
#include "android-base/scopeguard.h"
#include <utility>
#include <vector>
#include <gtest/gtest.h>
@ -44,3 +45,15 @@ TEST(scopeguard, moved) {
EXPECT_FALSE(scopeguard.active());
ASSERT_FALSE(guarded_var);
}
TEST(scopeguard, vector) {
int guarded_var = 0;
{
std::vector<android::base::ScopeGuard<std::function<void()>>> scopeguards;
scopeguards.emplace_back(android::base::make_scope_guard(
std::bind([](int& guarded_var) { guarded_var++; }, std::ref(guarded_var))));
scopeguards.emplace_back(android::base::make_scope_guard(
std::bind([](int& guarded_var) { guarded_var++; }, std::ref(guarded_var))));
}
ASSERT_EQ(guarded_var, 2);
}