summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorTreehugger Robot2018-03-26 18:05:25 -0500
committerGerrit Code Review2018-03-26 18:05:25 -0500
commit3b279aa5c7b570e9ac9db23198806186c1fe95bd (patch)
tree22710ffd286d5eae454f027d04395ce7bc42436f /base
parent80dce0127c48a86d74fde017c207890c5cf33d3b (diff)
parentb77035b89a3e95358a1aac9e28cd61c8b256e7ae (diff)
downloadplatform-system-core-3b279aa5c7b570e9ac9db23198806186c1fe95bd.tar.gz
platform-system-core-3b279aa5c7b570e9ac9db23198806186c1fe95bd.tar.xz
platform-system-core-3b279aa5c7b570e9ac9db23198806186c1fe95bd.zip
Merge "Allow android::base::ScopeGuard in STL containers"
Diffstat (limited to 'base')
-rw-r--r--base/include/android-base/scopeguard.h20
-rw-r--r--base/scopeguard_test.cpp13
2 files changed, 28 insertions, 5 deletions
diff --git a/base/include/android-base/scopeguard.h b/base/include/android-base/scopeguard.h
index abcf4bca4..c314e0263 100644
--- a/base/include/android-base/scopeguard.h
+++ b/base/include/android-base/scopeguard.h
@@ -17,20 +17,27 @@
17#ifndef ANDROID_BASE_SCOPEGUARD_H 17#ifndef ANDROID_BASE_SCOPEGUARD_H
18#define ANDROID_BASE_SCOPEGUARD_H 18#define ANDROID_BASE_SCOPEGUARD_H
19 19
20#include <utility> // for std::move 20#include <utility> // for std::move, std::forward
21 21
22namespace android { 22namespace android {
23namespace base { 23namespace base {
24 24
25// ScopeGuard ensures that the specified functor is executed no matter how the
26// current scope exits.
25template <typename F> 27template <typename F>
26class ScopeGuard { 28class ScopeGuard {
27 public: 29 public:
28 ScopeGuard(F f) : f_(f), active_(true) {} 30 ScopeGuard(F&& f) : f_(std::forward<F>(f)), active_(true) {}
29 31
30 ScopeGuard(ScopeGuard&& that) : f_(std::move(that.f_)), active_(that.active_) { 32 ScopeGuard(ScopeGuard&& that) : f_(std::move(that.f_)), active_(that.active_) {
31 that.active_ = false; 33 that.active_ = false;
32 } 34 }
33 35
36 template <typename Functor>
37 ScopeGuard(ScopeGuard<Functor>&& that) : f_(std::move(that.f_)), active_(that.active_) {
38 that.active_ = false;
39 }
40
34 ~ScopeGuard() { 41 ~ScopeGuard() {
35 if (active_) f_(); 42 if (active_) f_();
36 } 43 }
@@ -45,13 +52,16 @@ class ScopeGuard {
45 bool active() const { return active_; } 52 bool active() const { return active_; }
46 53
47 private: 54 private:
55 template <typename Functor>
56 friend class ScopeGuard;
57
48 F f_; 58 F f_;
49 bool active_; 59 bool active_;
50}; 60};
51 61
52template <typename T> 62template <typename F>
53ScopeGuard<T> make_scope_guard(T f) { 63ScopeGuard<F> make_scope_guard(F&& f) {
54 return ScopeGuard<T>(f); 64 return ScopeGuard<F>(std::forward<F>(f));
55} 65}
56 66
57} // namespace base 67} // namespace base
diff --git a/base/scopeguard_test.cpp b/base/scopeguard_test.cpp
index e11154a57..9236d7b78 100644
--- a/base/scopeguard_test.cpp
+++ b/base/scopeguard_test.cpp
@@ -17,6 +17,7 @@
17#include "android-base/scopeguard.h" 17#include "android-base/scopeguard.h"
18 18
19#include <utility> 19#include <utility>
20#include <vector>
20 21
21#include <gtest/gtest.h> 22#include <gtest/gtest.h>
22 23
@@ -44,3 +45,15 @@ TEST(scopeguard, moved) {
44 EXPECT_FALSE(scopeguard.active()); 45 EXPECT_FALSE(scopeguard.active());
45 ASSERT_FALSE(guarded_var); 46 ASSERT_FALSE(guarded_var);
46} 47}
48
49TEST(scopeguard, vector) {
50 int guarded_var = 0;
51 {
52 std::vector<android::base::ScopeGuard<std::function<void()>>> scopeguards;
53 scopeguards.emplace_back(android::base::make_scope_guard(
54 std::bind([](int& guarded_var) { guarded_var++; }, std::ref(guarded_var))));
55 scopeguards.emplace_back(android::base::make_scope_guard(
56 std::bind([](int& guarded_var) { guarded_var++; }, std::ref(guarded_var))));
57 }
58 ASSERT_EQ(guarded_var, 2);
59}