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 <typename FunctionType, bool NOM1, bool NOM2, size_t S1, size_t S2>
+template <
+ typename FunctionType,
+ FunctionMoveCtor NOM1,
+ FunctionMoveCtor NOM2,
+ size_t S1,
+ size_t S2>
void swap(
- ::folly::Function<FunctionType, NOM1, S1>& lhs,
- ::folly::Function<FunctionType, NOM2, S2>& rhs) {
+ Function<FunctionType, NOM1, S1>& lhs,
+ Function<FunctionType, NOM2, S2>& rhs) noexcept(noexcept(lhs.swap(rhs))) {
lhs.swap(rhs);
}
-} // std
+
+} // namespace folly
#include "Function-inl.h"
// TEST =====================================================================
// Swap
-template <FunctionMoveCtor NEM1, FunctionMoveCtor NEM2>
+template <FunctionMoveCtor NEM1, FunctionMoveCtor NEM2, bool UseSwapMethod>
void swap_test() {
Function<int(int), NEM1> mf1(func_int_int_add_25);
Function<int(int), NEM2> mf2(func_int_int_add_111);
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);
Function<int(int)> 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);
Function<int(int)> 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<FunctionMoveCtor::MAY_THROW, FunctionMoveCtor::MAY_THROW>();
+TEST(Function, SwapMethod_TT) {
+ 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) {
- swap_test<FunctionMoveCtor::MAY_THROW, FunctionMoveCtor::NO_THROW>();
+TEST(Function, SwapFunction_TN) {
+ swap_test<FunctionMoveCtor::MAY_THROW, FunctionMoveCtor::NO_THROW, false>();
}
-TEST(Function, Swap_NT) {
- swap_test<FunctionMoveCtor::NO_THROW, FunctionMoveCtor::MAY_THROW>();
+TEST(Function, SwapFunction_NT) {
+ swap_test<FunctionMoveCtor::NO_THROW, FunctionMoveCtor::MAY_THROW, false>();
}
-TEST(Function, Swap_NN) {
- swap_test<FunctionMoveCtor::NO_THROW, FunctionMoveCtor::NO_THROW>();
+TEST(Function, SwapFunction_NN) {
+ swap_test<FunctionMoveCtor::NO_THROW, FunctionMoveCtor::NO_THROW, false>();
}
// TEST =====================================================================