From 12d273d287dfc60999c331868092c45718448e1b Mon Sep 17 00:00:00 2001 From: Sven Over Date: Fri, 1 Apr 2016 17:55:57 -0700 Subject: [PATCH] folly::Function: fix swap function and put in correct namespace Summary:The swap function belongs in the same namespace as Function: folly. Also, to avoid amibiguities with a generic swap function in , we need two variants: one for identical types of folly::Function, and one for folly::Functions with different configurations. Reviewed By: ericniebler Differential Revision: D3106429 fb-gh-sync-id: 11b04e9bc709d52016ac94c078278410f5ee43c6 fbshipit-source-id: 11b04e9bc709d52016ac94c078278410f5ee43c6 --- folly/Function.h | 22 ++++++++++----- folly/test/FunctionTest.cpp | 54 ++++++++++++++++++++++++++++--------- 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/folly/Function.h b/folly/Function.h index bf97752c..1f6238ed 100644 --- a/folly/Function.h +++ b/folly/Function.h @@ -532,15 +532,25 @@ constCastFunction(Function&& return std::move(from).castToConstFunction(); } -} // folly +template +void swap( + Function& lhs, + Function& rhs) noexcept(noexcept(lhs.swap(rhs))) { + lhs.swap(rhs); +} -namespace std { -template +template < + typename FunctionType, + FunctionMoveCtor NOM1, + FunctionMoveCtor NOM2, + size_t S1, + size_t S2> void swap( - ::folly::Function& lhs, - ::folly::Function& rhs) { + Function& lhs, + Function& rhs) noexcept(noexcept(lhs.swap(rhs))) { lhs.swap(rhs); } -} // std + +} // namespace folly #include "Function-inl.h" diff --git a/folly/test/FunctionTest.cpp b/folly/test/FunctionTest.cpp index 718c00fc..6e073f24 100644 --- a/folly/test/FunctionTest.cpp +++ b/folly/test/FunctionTest.cpp @@ -266,7 +266,7 @@ TEST(Function, Types) { // TEST ===================================================================== // Swap -template +template void swap_test() { Function mf1(func_int_int_add_25); Function mf2(func_int_int_add_111); @@ -274,7 +274,11 @@ void swap_test() { EXPECT_EQ(mf1(100), 125); EXPECT_EQ(mf2(100), 211); - mf1.swap(mf2); + if (UseSwapMethod) { + mf1.swap(mf2); + } else { + swap(mf1, mf2); + } EXPECT_EQ(mf2(100), 125); EXPECT_EQ(mf1(100), 211); @@ -282,7 +286,11 @@ void swap_test() { Function mf3(nullptr); EXPECT_EQ(mf3, nullptr); - mf1.swap(mf3); + if (UseSwapMethod) { + mf1.swap(mf3); + } else { + swap(mf1, mf3); + } EXPECT_EQ(mf3(100), 211); EXPECT_EQ(mf1, nullptr); @@ -290,25 +298,45 @@ void swap_test() { Function mf4([](int x) { return x + 222; }); EXPECT_EQ(mf4(100), 322); - mf4.swap(mf3); + if (UseSwapMethod) { + mf4.swap(mf3); + } else { + swap(mf4, mf3); + } EXPECT_EQ(mf4(100), 211); EXPECT_EQ(mf3(100), 322); - mf3.swap(mf1); + if (UseSwapMethod) { + mf3.swap(mf1); + } else { + swap(mf3, mf1); + } EXPECT_EQ(mf3, nullptr); EXPECT_EQ(mf1(100), 322); } -TEST(Function, Swap_TT) { - swap_test(); +TEST(Function, SwapMethod_TT) { + swap_test(); +} +TEST(Function, SwapMethod_TN) { + swap_test(); +} +TEST(Function, SwapMethod_NT) { + swap_test(); +} +TEST(Function, SwapMethod_NN) { + swap_test(); +} +TEST(Function, SwapFunction_TT) { + swap_test(); } -TEST(Function, Swap_TN) { - swap_test(); +TEST(Function, SwapFunction_TN) { + swap_test(); } -TEST(Function, Swap_NT) { - swap_test(); +TEST(Function, SwapFunction_NT) { + swap_test(); } -TEST(Function, Swap_NN) { - swap_test(); +TEST(Function, SwapFunction_NN) { + swap_test(); } // TEST ===================================================================== -- 2.34.1