Commit 12d273d2 authored by Sven Over's avatar Sven Over Committed by Facebook Github Bot 1

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
<algorithm>, 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
parent 4e995b4d
...@@ -532,15 +532,25 @@ constCastFunction(Function<FunctionType, NTM, EmbedFunctorSize>&& ...@@ -532,15 +532,25 @@ constCastFunction(Function<FunctionType, NTM, EmbedFunctorSize>&&
return std::move(from).castToConstFunction(); return std::move(from).castToConstFunction();
} }
} // folly template <typename FunctionType, FunctionMoveCtor NOM, size_t S>
void swap(
Function<FunctionType, NOM, S>& lhs,
Function<FunctionType, NOM, S>& rhs) noexcept(noexcept(lhs.swap(rhs))) {
lhs.swap(rhs);
}
namespace std { template <
template <typename FunctionType, bool NOM1, bool NOM2, size_t S1, size_t S2> typename FunctionType,
FunctionMoveCtor NOM1,
FunctionMoveCtor NOM2,
size_t S1,
size_t S2>
void swap( void swap(
::folly::Function<FunctionType, NOM1, S1>& lhs, Function<FunctionType, NOM1, S1>& lhs,
::folly::Function<FunctionType, NOM2, S2>& rhs) { Function<FunctionType, NOM2, S2>& rhs) noexcept(noexcept(lhs.swap(rhs))) {
lhs.swap(rhs); lhs.swap(rhs);
} }
} // std
} // namespace folly
#include "Function-inl.h" #include "Function-inl.h"
...@@ -266,7 +266,7 @@ TEST(Function, Types) { ...@@ -266,7 +266,7 @@ TEST(Function, Types) {
// TEST ===================================================================== // TEST =====================================================================
// Swap // Swap
template <FunctionMoveCtor NEM1, FunctionMoveCtor NEM2> template <FunctionMoveCtor NEM1, FunctionMoveCtor NEM2, bool UseSwapMethod>
void swap_test() { void swap_test() {
Function<int(int), NEM1> mf1(func_int_int_add_25); Function<int(int), NEM1> mf1(func_int_int_add_25);
Function<int(int), NEM2> mf2(func_int_int_add_111); Function<int(int), NEM2> mf2(func_int_int_add_111);
...@@ -274,7 +274,11 @@ void swap_test() { ...@@ -274,7 +274,11 @@ void swap_test() {
EXPECT_EQ(mf1(100), 125); EXPECT_EQ(mf1(100), 125);
EXPECT_EQ(mf2(100), 211); EXPECT_EQ(mf2(100), 211);
mf1.swap(mf2); if (UseSwapMethod) {
mf1.swap(mf2);
} else {
swap(mf1, mf2);
}
EXPECT_EQ(mf2(100), 125); EXPECT_EQ(mf2(100), 125);
EXPECT_EQ(mf1(100), 211); EXPECT_EQ(mf1(100), 211);
...@@ -282,7 +286,11 @@ void swap_test() { ...@@ -282,7 +286,11 @@ void swap_test() {
Function<int(int)> mf3(nullptr); Function<int(int)> mf3(nullptr);
EXPECT_EQ(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(mf3(100), 211);
EXPECT_EQ(mf1, nullptr); EXPECT_EQ(mf1, nullptr);
...@@ -290,25 +298,45 @@ void swap_test() { ...@@ -290,25 +298,45 @@ void swap_test() {
Function<int(int)> mf4([](int x) { return x + 222; }); Function<int(int)> mf4([](int x) { return x + 222; });
EXPECT_EQ(mf4(100), 322); EXPECT_EQ(mf4(100), 322);
mf4.swap(mf3); if (UseSwapMethod) {
mf4.swap(mf3);
} else {
swap(mf4, mf3);
}
EXPECT_EQ(mf4(100), 211); EXPECT_EQ(mf4(100), 211);
EXPECT_EQ(mf3(100), 322); EXPECT_EQ(mf3(100), 322);
mf3.swap(mf1); if (UseSwapMethod) {
mf3.swap(mf1);
} else {
swap(mf3, mf1);
}
EXPECT_EQ(mf3, nullptr); EXPECT_EQ(mf3, nullptr);
EXPECT_EQ(mf1(100), 322); EXPECT_EQ(mf1(100), 322);
} }
TEST(Function, Swap_TT) { TEST(Function, SwapMethod_TT) {
swap_test<FunctionMoveCtor::MAY_THROW, FunctionMoveCtor::MAY_THROW>(); swap_test<FunctionMoveCtor::MAY_THROW, FunctionMoveCtor::MAY_THROW, true>();
}
TEST(Function, SwapMethod_TN) {
swap_test<FunctionMoveCtor::MAY_THROW, FunctionMoveCtor::NO_THROW, true>();
}
TEST(Function, SwapMethod_NT) {
swap_test<FunctionMoveCtor::NO_THROW, FunctionMoveCtor::MAY_THROW, true>();
}
TEST(Function, SwapMethod_NN) {
swap_test<FunctionMoveCtor::NO_THROW, FunctionMoveCtor::NO_THROW, true>();
}
TEST(Function, SwapFunction_TT) {
swap_test<FunctionMoveCtor::MAY_THROW, FunctionMoveCtor::MAY_THROW, false>();
} }
TEST(Function, Swap_TN) { TEST(Function, SwapFunction_TN) {
swap_test<FunctionMoveCtor::MAY_THROW, FunctionMoveCtor::NO_THROW>(); swap_test<FunctionMoveCtor::MAY_THROW, FunctionMoveCtor::NO_THROW, false>();
} }
TEST(Function, Swap_NT) { TEST(Function, SwapFunction_NT) {
swap_test<FunctionMoveCtor::NO_THROW, FunctionMoveCtor::MAY_THROW>(); swap_test<FunctionMoveCtor::NO_THROW, FunctionMoveCtor::MAY_THROW, false>();
} }
TEST(Function, Swap_NN) { TEST(Function, SwapFunction_NN) {
swap_test<FunctionMoveCtor::NO_THROW, FunctionMoveCtor::NO_THROW>(); swap_test<FunctionMoveCtor::NO_THROW, FunctionMoveCtor::NO_THROW, false>();
} }
// TEST ===================================================================== // TEST =====================================================================
......
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