Commit 46563373 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Use auto for scope-guard locals v.s. folly::ScopeGuard

Summary: Use `auto` for scope-guard locals v.s. `folly::ScopeGuard`.

Reviewed By: igorsugak, meyering

Differential Revision: D6664915

fbshipit-source-id: ea239b712f3f9dc7ef81105aaf82f4b36bc07db5
parent cacdd1d5
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2011-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -47,8 +47,7 @@ namespace folly { ...@@ -47,8 +47,7 @@ namespace folly {
* *
* // If the db insertion that follows fails, we should * // If the db insertion that follows fails, we should
* // remove it from memory. * // remove it from memory.
* // (You could also declare this as "auto guard = makeGuard(...)") * auto guard = makeGuard([&] { friends_.pop_back(); });
* ScopeGuard guard = makeGuard([&] { friends_.pop_back(); });
* *
* // this will throw an exception upon error, which * // this will throw an exception upon error, which
* // makes the ScopeGuard execute UserCont::pop_back() * // makes the ScopeGuard execute UserCont::pop_back()
......
...@@ -76,7 +76,7 @@ int ElfFile::openNoThrow( ...@@ -76,7 +76,7 @@ int ElfFile::openNoThrow(
// Always close fd and unmap in case of failure along the way to avoid // Always close fd and unmap in case of failure along the way to avoid
// check failure above if we leave fd != -1 and the object is recycled // check failure above if we leave fd != -1 and the object is recycled
// like it is inside SignalSafeElfCache // like it is inside SignalSafeElfCache
ScopeGuard guard = makeGuard([&] { reset(); }); auto guard = makeGuard([&] { reset(); });
struct stat st; struct stat st;
int r = fstat(fd_, &st); int r = fstat(fd_, &st);
if (r == -1) { if (r == -1) {
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2011-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
using folly::ScopeGuard;
using folly::makeGuard; using folly::makeGuard;
using std::vector; using std::vector;
...@@ -47,7 +46,7 @@ TEST(ScopeGuard, DifferentWaysToBind) { ...@@ -47,7 +46,7 @@ TEST(ScopeGuard, DifferentWaysToBind) {
{ {
// There is implicit conversion from func pointer // There is implicit conversion from func pointer
// double (*)() to function<void()>. // double (*)() to function<void()>.
ScopeGuard g = makeGuard(returnsDouble); auto g = makeGuard(returnsDouble);
(void)g; (void)g;
} }
...@@ -57,42 +56,42 @@ TEST(ScopeGuard, DifferentWaysToBind) { ...@@ -57,42 +56,42 @@ TEST(ScopeGuard, DifferentWaysToBind) {
v.push_back(1); v.push_back(1);
{ {
// binding to member function. // binding to member function.
ScopeGuard g = makeGuard(std::bind(&vector<int>::pop_back, &v)); auto g = makeGuard(std::bind(&vector<int>::pop_back, &v));
(void)g; (void)g;
} }
EXPECT_EQ(0, v.size()); EXPECT_EQ(0, v.size());
{ {
// bind member function with args. v is passed-by-value! // bind member function with args. v is passed-by-value!
ScopeGuard g = makeGuard(std::bind(push_back, v, 2)); auto g = makeGuard(std::bind(push_back, v, 2));
(void)g; (void)g;
} }
EXPECT_EQ(0, v.size()); // push_back happened on a copy of v... fail! EXPECT_EQ(0, v.size()); // push_back happened on a copy of v... fail!
// pass in an argument by pointer so to avoid copy. // pass in an argument by pointer so to avoid copy.
{ {
ScopeGuard g = makeGuard(std::bind(push_back, &v, 4)); auto g = makeGuard(std::bind(push_back, &v, 4));
(void)g; (void)g;
} }
EXPECT_EQ(1, v.size()); EXPECT_EQ(1, v.size());
{ {
// pass in an argument by reference so to avoid copy. // pass in an argument by reference so to avoid copy.
ScopeGuard g = makeGuard(std::bind(push_back, std::ref(v), 4)); auto g = makeGuard(std::bind(push_back, std::ref(v), 4));
(void)g; (void)g;
} }
EXPECT_EQ(2, v.size()); EXPECT_EQ(2, v.size());
// lambda with a reference to v // lambda with a reference to v
{ {
ScopeGuard g = makeGuard([&] { v.push_back(5); }); auto g = makeGuard([&] { v.push_back(5); });
(void)g; (void)g;
} }
EXPECT_EQ(3, v.size()); EXPECT_EQ(3, v.size());
// lambda with a copy of v // lambda with a copy of v
{ {
ScopeGuard g = makeGuard([v] () mutable { v.push_back(6); }); auto g = makeGuard([v]() mutable { v.push_back(6); });
(void)g; (void)g;
} }
EXPECT_EQ(3, v.size()); EXPECT_EQ(3, v.size());
...@@ -101,7 +100,7 @@ TEST(ScopeGuard, DifferentWaysToBind) { ...@@ -101,7 +100,7 @@ TEST(ScopeGuard, DifferentWaysToBind) {
int n = 0; int n = 0;
{ {
MyFunctor f(&n); MyFunctor f(&n);
ScopeGuard g = makeGuard(f); auto g = makeGuard(f);
(void)g; (void)g;
} }
EXPECT_EQ(1, n); EXPECT_EQ(1, n);
...@@ -109,7 +108,7 @@ TEST(ScopeGuard, DifferentWaysToBind) { ...@@ -109,7 +108,7 @@ TEST(ScopeGuard, DifferentWaysToBind) {
// temporary functor object // temporary functor object
n = 0; n = 0;
{ {
ScopeGuard g = makeGuard(MyFunctor(&n)); auto g = makeGuard(MyFunctor(&n));
(void)g; (void)g;
} }
EXPECT_EQ(1, n); EXPECT_EQ(1, n);
...@@ -132,14 +131,9 @@ TEST(ScopeGuard, DifferentWaysToBind) { ...@@ -132,14 +131,9 @@ TEST(ScopeGuard, DifferentWaysToBind) {
} }
TEST(ScopeGuard, GuardException) { TEST(ScopeGuard, GuardException) {
EXPECT_DEATH({ EXPECT_DEATH(
ScopeGuard g = makeGuard([&] { makeGuard([] { throw std::runtime_error("dtors should never throw!"); }),
throw std::runtime_error("destructors should never throw!"); "dtors should never throw!");
});
(void)g;
},
"destructors should never throw!"
);
} }
/** /**
...@@ -155,7 +149,7 @@ void testUndoAction(bool failure) { ...@@ -155,7 +149,7 @@ void testUndoAction(bool failure) {
v.push_back(1); v.push_back(1);
// The guard is triggered to undo the insertion unless dismiss() is called. // The guard is triggered to undo the insertion unless dismiss() is called.
ScopeGuard guard = makeGuard([&] { v.pop_back(); }); auto guard = makeGuard([&] { v.pop_back(); });
// Do some action; Use the failure argument to pretend // Do some action; Use the failure argument to pretend
// if it failed or succeeded. // if it failed or succeeded.
...@@ -207,7 +201,7 @@ void testFinally(ErrorBehavior error) { ...@@ -207,7 +201,7 @@ void testFinally(ErrorBehavior error) {
bool cleanupOccurred = false; bool cleanupOccurred = false;
try { try {
ScopeGuard guard = makeGuard([&] { cleanupOccurred = true; }); auto guard = makeGuard([&] { cleanupOccurred = true; });
(void)guard; (void)guard;
try { try {
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2011-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
#include <folly/ScopeGuard.h> #include <folly/ScopeGuard.h>
#include <folly/portability/GFlags.h> #include <folly/portability/GFlags.h>
using folly::ScopeGuard;
using folly::makeGuard; using folly::makeGuard;
// Declare the bm_max_iters flag from folly/Benchmark.cpp // Declare the bm_max_iters flag from folly/Benchmark.cpp
...@@ -158,7 +157,7 @@ BENCHMARK(std_bind_direct_invoke, iters) { ...@@ -158,7 +157,7 @@ BENCHMARK(std_bind_direct_invoke, iters) {
BENCHMARK(scope_guard_std_function, iters) { BENCHMARK(scope_guard_std_function, iters) {
std::function<void()> fn(doNothing); std::function<void()> fn(doNothing);
for (size_t n = 0; n < iters; ++n) { for (size_t n = 0; n < iters; ++n) {
ScopeGuard g = makeGuard(fn); auto g = makeGuard(fn);
(void)g; (void)g;
} }
} }
...@@ -167,7 +166,7 @@ BENCHMARK(scope_guard_std_function, iters) { ...@@ -167,7 +166,7 @@ BENCHMARK(scope_guard_std_function, iters) {
// but create the ScopeGuard with an rvalue to a std::function // but create the ScopeGuard with an rvalue to a std::function
BENCHMARK(scope_guard_std_function_rvalue, iters) { BENCHMARK(scope_guard_std_function_rvalue, iters) {
for (size_t n = 0; n < iters; ++n) { for (size_t n = 0; n < iters; ++n) {
ScopeGuard g = makeGuard(std::function<void()>(doNothing)); auto g = makeGuard(std::function<void()>(doNothing));
(void)g; (void)g;
} }
} }
...@@ -176,7 +175,7 @@ BENCHMARK(scope_guard_std_function_rvalue, iters) { ...@@ -176,7 +175,7 @@ BENCHMARK(scope_guard_std_function_rvalue, iters) {
// but create the ScopeGuard with an rvalue to a folly::Function // but create the ScopeGuard with an rvalue to a folly::Function
BENCHMARK(scope_guard_Function_rvalue, iters) { BENCHMARK(scope_guard_Function_rvalue, iters) {
for (size_t n = 0; n < iters; ++n) { for (size_t n = 0; n < iters; ++n) {
ScopeGuard g = makeGuard(folly::Function<void()>(doNothing)); auto g = makeGuard(folly::Function<void()>(doNothing));
(void)g; (void)g;
} }
} }
...@@ -184,7 +183,7 @@ BENCHMARK(scope_guard_Function_rvalue, iters) { ...@@ -184,7 +183,7 @@ BENCHMARK(scope_guard_Function_rvalue, iters) {
// Using ScopeGuard to invoke a function pointer // Using ScopeGuard to invoke a function pointer
BENCHMARK(scope_guard_fn_ptr, iters) { BENCHMARK(scope_guard_fn_ptr, iters) {
for (size_t n = 0; n < iters; ++n) { for (size_t n = 0; n < iters; ++n) {
ScopeGuard g = makeGuard(doNothing); auto g = makeGuard(doNothing);
(void)g; (void)g;
} }
} }
...@@ -192,7 +191,7 @@ BENCHMARK(scope_guard_fn_ptr, iters) { ...@@ -192,7 +191,7 @@ BENCHMARK(scope_guard_fn_ptr, iters) {
// Using ScopeGuard to invoke a lambda that does nothing // Using ScopeGuard to invoke a lambda that does nothing
BENCHMARK(scope_guard_lambda_noop, iters) { BENCHMARK(scope_guard_lambda_noop, iters) {
for (size_t n = 0; n < iters; ++n) { for (size_t n = 0; n < iters; ++n) {
ScopeGuard g = makeGuard([] {}); auto g = makeGuard([] {});
(void)g; (void)g;
} }
} }
...@@ -200,7 +199,7 @@ BENCHMARK(scope_guard_lambda_noop, iters) { ...@@ -200,7 +199,7 @@ BENCHMARK(scope_guard_lambda_noop, iters) {
// Using ScopeGuard to invoke a lambda that invokes a function // Using ScopeGuard to invoke a lambda that invokes a function
BENCHMARK(scope_guard_lambda_function, iters) { BENCHMARK(scope_guard_lambda_function, iters) {
for (size_t n = 0; n < iters; ++n) { for (size_t n = 0; n < iters; ++n) {
ScopeGuard g = makeGuard([] { doNothing(); }); auto g = makeGuard([] { doNothing(); });
(void)g; (void)g;
} }
} }
...@@ -209,7 +208,7 @@ BENCHMARK(scope_guard_lambda_function, iters) { ...@@ -209,7 +208,7 @@ BENCHMARK(scope_guard_lambda_function, iters) {
BENCHMARK(scope_guard_lambda_local_var, iters) { BENCHMARK(scope_guard_lambda_local_var, iters) {
uint32_t count = 0; uint32_t count = 0;
for (size_t n = 0; n < iters; ++n) { for (size_t n = 0; n < iters; ++n) {
ScopeGuard g = makeGuard([&] { auto g = makeGuard([&] {
// Increment count if n is odd. Without this conditional check // Increment count if n is odd. Without this conditional check
// (i.e., if we just increment count each time through the loop), // (i.e., if we just increment count each time through the loop),
// gcc is smart enough to optimize the entire loop away, and just set // gcc is smart enough to optimize the entire loop away, and just set
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment