summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authoravi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-30 02:04:04 +0000
committeravi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-30 02:04:04 +0000
commit1c232c2bbc955bd58282cd815bc0fcc2bcf66aa1 (patch)
tree0ab63673a38ea13eb0659e48d542e49867062f50 /base
parent85267b1f29294369994453d2b459b252db1fe556 (diff)
downloadchromium_src-1c232c2bbc955bd58282cd815bc0fcc2bcf66aa1.zip
chromium_src-1c232c2bbc955bd58282cd815bc0fcc2bcf66aa1.tar.gz
chromium_src-1c232c2bbc955bd58282cd815bc0fcc2bcf66aa1.tar.bz2
Move ScopedClosureRunner to callback_helpers, add Reset.
BUG=none TEST=none TBR=ben@chromium.org Review URL: https://chromiumcodereview.appspot.com/23514018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@220473 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/base.gyp2
-rw-r--r--base/base.gypi1
-rw-r--r--base/bind_helpers.cc15
-rw-r--r--base/bind_helpers.h19
-rw-r--r--base/callback_helpers.cc42
-rw-r--r--base/callback_helpers.h20
-rw-r--r--base/callback_helpers_unittest.cc (renamed from base/bind_helpers_unittest.cc)28
-rw-r--r--base/mac/bind_objc_block_unittest.mm2
-rw-r--r--base/prefs/pref_member.cc2
-rw-r--r--base/test/unit_test_launcher.cc1
10 files changed, 92 insertions, 40 deletions
diff --git a/base/base.gyp b/base/base.gyp
index fabc868..0cdcaa4 100644
--- a/base/base.gyp
+++ b/base/base.gyp
@@ -455,11 +455,11 @@
'atomicops_unittest.cc',
'barrier_closure_unittest.cc',
'base64_unittest.cc',
- 'bind_helpers_unittest.cc',
'bind_unittest.cc',
'bind_unittest.nc',
'bits_unittest.cc',
'build_time_unittest.cc',
+ 'callback_helpers_unittest.cc',
'callback_unittest.cc',
'callback_unittest.nc',
'cancelable_callback_unittest.cc',
diff --git a/base/base.gypi b/base/base.gypi
index 02697b2..9bac4fd 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -104,6 +104,7 @@
'build_time.cc',
'build_time.h',
'callback.h',
+ 'callback_helpers.cc',
'callback_helpers.h',
'callback_internal.cc',
'callback_internal.h',
diff --git a/base/bind_helpers.cc b/base/bind_helpers.cc
index f2fc3bb..f1fe46d 100644
--- a/base/bind_helpers.cc
+++ b/base/bind_helpers.cc
@@ -11,19 +11,4 @@ namespace base {
void DoNothing() {
}
-ScopedClosureRunner::ScopedClosureRunner(const Closure& closure)
- : closure_(closure) {
-}
-
-ScopedClosureRunner::~ScopedClosureRunner() {
- if (!closure_.is_null())
- closure_.Run();
-}
-
-Closure ScopedClosureRunner::Release() {
- Closure result = closure_;
- closure_.Reset();
- return result;
-}
-
} // namespace base
diff --git a/base/bind_helpers.h b/base/bind_helpers.h
index 0cfaab7..d717892 100644
--- a/base/bind_helpers.h
+++ b/base/bind_helpers.h
@@ -139,10 +139,6 @@
// pointer when invoked. Only use this when necessary.
// In most cases MessageLoop::DeleteSoon() is a better
// fit.
-// ScopedClosureRunner - Scoper object that runs the wrapped closure when it
-// goes out of scope. It's conceptually similar to
-// scoped_ptr<> but calls Run() instead of deleting
-// the pointer.
#ifndef BASE_BIND_HELPERS_H_
#define BASE_BIND_HELPERS_H_
@@ -543,21 +539,6 @@ void DeletePointer(T* obj) {
delete obj;
}
-// ScopedClosureRunner is akin to scoped_ptr for Closures. It ensures that the
-// Closure is executed and deleted no matter how the current scope exits.
-class BASE_EXPORT ScopedClosureRunner {
- public:
- explicit ScopedClosureRunner(const Closure& closure);
- ~ScopedClosureRunner();
-
- Closure Release();
-
- private:
- Closure closure_;
-
- DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedClosureRunner);
-};
-
} // namespace base
#endif // BASE_BIND_HELPERS_H_
diff --git a/base/callback_helpers.cc b/base/callback_helpers.cc
new file mode 100644
index 0000000..ef02b2b
--- /dev/null
+++ b/base/callback_helpers.cc
@@ -0,0 +1,42 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/callback_helpers.h"
+
+#include "base/callback.h"
+
+namespace base {
+
+ScopedClosureRunner::ScopedClosureRunner() {
+}
+
+ScopedClosureRunner::ScopedClosureRunner(const Closure& closure)
+ : closure_(closure) {
+}
+
+ScopedClosureRunner::~ScopedClosureRunner() {
+ if (!closure_.is_null())
+ closure_.Run();
+}
+
+void ScopedClosureRunner::Reset() {
+ Closure old_closure = Release();
+ if (!old_closure.is_null())
+ old_closure.Run();
+}
+
+void ScopedClosureRunner::Reset(const Closure& closure) {
+ Closure old_closure = Release();
+ closure_ = closure;
+ if (!old_closure.is_null())
+ old_closure.Run();
+}
+
+Closure ScopedClosureRunner::Release() {
+ Closure result = closure_;
+ closure_.Reset();
+ return result;
+}
+
+} // namespace base
diff --git a/base/callback_helpers.h b/base/callback_helpers.h
index 52cb71b..8481e3e 100644
--- a/base/callback_helpers.h
+++ b/base/callback_helpers.h
@@ -14,7 +14,9 @@
#ifndef BASE_CALLBACK_HELPERS_H_
#define BASE_CALLBACK_HELPERS_H_
+#include "base/basictypes.h"
#include "base/callback.h"
+#include "base/compiler_specific.h"
namespace base {
@@ -25,6 +27,24 @@ base::Callback<Sig> ResetAndReturn(base::Callback<Sig>* cb) {
return ret;
}
+// ScopedClosureRunner is akin to scoped_ptr for Closures. It ensures that the
+// Closure is executed and deleted no matter how the current scope exits.
+class BASE_EXPORT ScopedClosureRunner {
+ public:
+ ScopedClosureRunner();
+ explicit ScopedClosureRunner(const Closure& closure);
+ ~ScopedClosureRunner();
+
+ void Reset();
+ void Reset(const Closure& closure);
+ Closure Release() WARN_UNUSED_RESULT;
+
+ private:
+ Closure closure_;
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedClosureRunner);
+};
+
} // namespace base
#endif // BASE_CALLBACK_HELPERS_H_
diff --git a/base/bind_helpers_unittest.cc b/base/callback_helpers_unittest.cc
index 3ef2d75..3b17a6b 100644
--- a/base/bind_helpers_unittest.cc
+++ b/base/callback_helpers_unittest.cc
@@ -1,11 +1,11 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/bind_helpers.h"
+#include "base/callback_helpers.h"
-#include "base/callback.h"
#include "base/bind.h"
+#include "base/callback.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
@@ -36,4 +36,26 @@ TEST(BindHelpersTest, TestScopedClosureRunnerRelease) {
EXPECT_EQ(1, run_count);
}
+TEST(BindHelpersTest, TestScopedClosureRunnerReset) {
+ int run_count_1 = 0;
+ int run_count_2 = 0;
+ {
+ base::ScopedClosureRunner runner;
+ runner.Reset(base::Bind(&Increment, &run_count_1));
+ runner.Reset(base::Bind(&Increment, &run_count_2));
+ EXPECT_EQ(1, run_count_1);
+ EXPECT_EQ(0, run_count_2);
+ }
+ EXPECT_EQ(1, run_count_2);
+
+ int run_count_3 = 0;
+ {
+ base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count_3));
+ EXPECT_EQ(0, run_count_3);
+ runner.Reset();
+ EXPECT_EQ(1, run_count_3);
+ }
+ EXPECT_EQ(1, run_count_3);
+}
+
} // namespace
diff --git a/base/mac/bind_objc_block_unittest.mm b/base/mac/bind_objc_block_unittest.mm
index 888b3dc..a4bcd76 100644
--- a/base/mac/bind_objc_block_unittest.mm
+++ b/base/mac/bind_objc_block_unittest.mm
@@ -6,7 +6,7 @@
#include "base/callback.h"
#include "base/bind.h"
-#include "base/bind_helpers.h"
+#include "base/callback_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
diff --git a/base/prefs/pref_member.cc b/base/prefs/pref_member.cc
index e3f6ac0..eb70839 100644
--- a/base/prefs/pref_member.cc
+++ b/base/prefs/pref_member.cc
@@ -4,8 +4,8 @@
#include "base/prefs/pref_member.h"
-#include "base/bind_helpers.h"
#include "base/callback.h"
+#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/prefs/pref_service.h"
#include "base/value_conversions.h"
diff --git a/base/test/unit_test_launcher.cc b/base/test/unit_test_launcher.cc
index 49ad727..0116bba 100644
--- a/base/test/unit_test_launcher.cc
+++ b/base/test/unit_test_launcher.cc
@@ -5,6 +5,7 @@
#include "base/test/unit_test_launcher.h"
#include "base/bind.h"
+#include "base/callback_helpers.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/files/scoped_temp_dir.h"