diff options
author | Abseil Team <absl-team@google.com> | 2023-09-26 02:49:04 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-09-26 02:50:00 -0700 |
commit | f4c6246d2a67d1bf7d661a552a2f5be20211cca6 (patch) | |
tree | c0561947a7a21705306b6bd68cd876f1568151a4 | |
parent | d53ca3be7de2f6317c4d047d053c36a44cfc28ef (diff) |
`absl::Overload()` which returns a functor that provides overloads based on the functors passed to it.
PiperOrigin-RevId: 568476251
Change-Id: Ic625c9b5300d1db496979c178ca1e655581f9276
-rw-r--r-- | CMake/AbseilDll.cmake | 1 | ||||
-rw-r--r-- | absl/functional/BUILD.bazel | 26 | ||||
-rw-r--r-- | absl/functional/CMakeLists.txt | 24 | ||||
-rw-r--r-- | absl/functional/overload.h | 75 | ||||
-rw-r--r-- | absl/functional/overload_test.cc | 130 |
5 files changed, 256 insertions, 0 deletions
diff --git a/CMake/AbseilDll.cmake b/CMake/AbseilDll.cmake index f46aec48..c943814e 100644 --- a/CMake/AbseilDll.cmake +++ b/CMake/AbseilDll.cmake @@ -138,6 +138,7 @@ set(ABSL_INTERNAL_DLL_FILES "functional/function_ref.h" "functional/internal/any_invocable.h" "functional/internal/function_ref.h" + "functional/overload.h" "hash/hash.h" "hash/internal/city.h" "hash/internal/city.cc" diff --git a/absl/functional/BUILD.bazel b/absl/functional/BUILD.bazel index 4ceac539..28718257 100644 --- a/absl/functional/BUILD.bazel +++ b/absl/functional/BUILD.bazel @@ -113,6 +113,32 @@ cc_test( ], ) +cc_library( + name = "overload", + hdrs = ["overload.h"], + copts = ABSL_DEFAULT_COPTS, + linkopts = ABSL_DEFAULT_LINKOPTS, + deps = [ + "//absl/base:config", + "//absl/meta:type_traits", + ], +) + +cc_test( + name = "overload_test", + size = "small", + srcs = ["overload_test.cc"], + copts = ABSL_TEST_COPTS, + deps = [ + ":overload", + "//absl/base:config", + "//absl/strings", + "//absl/strings:string_view", + "//absl/types:variant", + "@com_google_googletest//:gtest_main", + ], +) + cc_test( name = "function_type_benchmark", srcs = [ diff --git a/absl/functional/CMakeLists.txt b/absl/functional/CMakeLists.txt index c704e049..602829cb 100644 --- a/absl/functional/CMakeLists.txt +++ b/absl/functional/CMakeLists.txt @@ -108,3 +108,27 @@ absl_cc_test( absl::test_instance_tracker GTest::gmock_main ) + +absl_cc_library( + NAME + overload + HDRS + "overload.h" + COPTS + ${ABSL_DEFAULT_COPTS} + DEPS + absl::meta + PUBLIC +) + +absl_cc_test( + NAME + overload_test + SRCS + "overload_test.cc" + COPTS + ${ABSL_TEST_COPTS} + DEPS + absl::strings + GTest::gmock_main +) diff --git a/absl/functional/overload.h b/absl/functional/overload.h new file mode 100644 index 00000000..4651f14b --- /dev/null +++ b/absl/functional/overload.h @@ -0,0 +1,75 @@ +// Copyright 2023 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ----------------------------------------------------------------------------- +// File: overload.h +// ----------------------------------------------------------------------------- +// +// `absl::Overload()` returns a functor that provides overloads based on the +// functors passed to it. +// Before using this function, consider whether named function overloads would +// be a better design. +// One use case for this is locally defining visitors for `std::visit` inside a +// function using lambdas. + +// Example: Using `absl::Overload` to define a visitor for `std::variant`. +// +// std::variant<int, std::string, double> v(int{1}); +// +// assert(std::visit(absl::Overload( +// [](int) -> absl::string_view { return "int"; }, +// [](const std::string&) -> absl::string_view { +// return "string"; +// }, +// [](double) -> absl::string_view { return "double"; }), +// v) == "int"); +// +// Note: This requires C++17. + +#ifndef ABSL_FUNCTIONAL_OVERLOAD_H_ +#define ABSL_FUNCTIONAL_OVERLOAD_H_ + +#include "absl/base/config.h" +#include "absl/meta/type_traits.h" + +namespace absl { +ABSL_NAMESPACE_BEGIN + +#if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \ + ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L + +template <int&... ExplicitArgumentBarrier, typename... T> +auto Overload(T&&... ts) { + struct OverloadImpl : absl::remove_cvref_t<T>... { + using absl::remove_cvref_t<T>::operator()...; + }; + return OverloadImpl{std::forward<T>(ts)...}; +} +#else +namespace functional_internal { +template <typename T> +constexpr bool kDependentFalse = false; +} + +template <typename Dependent = int, typename... T> +auto Overload(T&&...) { + static_assert(functional_internal::kDependentFalse<Dependent>, + "Overload is only usable with C++17 or above."); +} + +#endif +ABSL_NAMESPACE_END +} // namespace absl + +#endif // ABSL_FUNCTIONAL_OVERLOAD_H_ diff --git a/absl/functional/overload_test.cc b/absl/functional/overload_test.cc new file mode 100644 index 00000000..739c4c4c --- /dev/null +++ b/absl/functional/overload_test.cc @@ -0,0 +1,130 @@ +// Copyright 2023 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "absl/functional/overload.h" + +#include <cstdint> +#include <string> +#include <type_traits> + +#include "absl/base/config.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "absl/types/variant.h" + +#if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \ + ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L + +#include "gtest/gtest.h" + +namespace { + +TEST(OverloadTest, DispatchConsidersType) { + auto overloaded = absl::Overload( + [](int v) -> std::string { return absl::StrCat("int ", v); }, // + [](double v) -> std::string { return absl::StrCat("double ", v); }, // + [](const char* v) -> std::string { // + return absl::StrCat("const char* ", v); // + }, // + [](auto v) -> std::string { return absl::StrCat("auto ", v); } // + ); + EXPECT_EQ("int 1", overloaded(1)); + EXPECT_EQ("double 2.5", overloaded(2.5)); + EXPECT_EQ("const char* hello", overloaded("hello")); + EXPECT_EQ("auto 1.5", overloaded(1.5f)); +} + +TEST(OverloadTest, DispatchConsidersNumberOfArguments) { + auto overloaded = absl::Overload( // + [](int a) { return a + 1; }, // + [](int a, int b) { return a * b; }, // + []() -> absl::string_view { return "none"; } // + ); + EXPECT_EQ(3, overloaded(2)); + EXPECT_EQ(21, overloaded(3, 7)); + EXPECT_EQ("none", overloaded()); +} + +TEST(OverloadTest, SupportsConstantEvaluation) { + auto overloaded = absl::Overload( // + [](int a) { return a + 1; }, // + [](int a, int b) { return a * b; }, // + []() -> absl::string_view { return "none"; } // + ); + static_assert(overloaded() == "none"); + static_assert(overloaded(2) == 3); + static_assert(overloaded(3, 7) == 21); +} + +TEST(OverloadTest, PropogatesDefaults) { + auto overloaded = absl::Overload( // + [](int a, int b = 5) { return a * b; }, // + [](double c) { return c; } // + ); + + EXPECT_EQ(21, overloaded(3, 7)); + EXPECT_EQ(35, overloaded(7)); + EXPECT_EQ(2.5, overloaded(2.5)); +} + +TEST(OverloadTest, AmbiguousWithDefaultsNotInvocable) { + auto overloaded = absl::Overload( // + [](int a, int b = 5) { return a * b; }, // + [](int c) { return c; } // + ); + static_assert(!std::is_invocable_v<decltype(overloaded), int>); + static_assert(std::is_invocable_v<decltype(overloaded), int, int>); +} + +TEST(OverloadTest, AmbiguousDuplicatesNotInvocable) { + auto overloaded = absl::Overload( // + [](int a) { return a; }, // + [](int c) { return c; } // + ); + static_assert(!std::is_invocable_v<decltype(overloaded), int>); +} + +TEST(OverloadTest, AmbiguousConversionNotInvocable) { + auto overloaded = absl::Overload( // + [](uint16_t a) { return a; }, // + [](uint64_t c) { return c; } // + ); + static_assert(!std::is_invocable_v<decltype(overloaded), int>); +} + +TEST(OverloadTest, DispatchConsidersSfinae) { + auto overloaded = absl::Overload( // + [](auto a) -> decltype(a + 1) { return a + 1; } // + ); + static_assert(std::is_invocable_v<decltype(overloaded), int>); + static_assert(!std::is_invocable_v<decltype(overloaded), std::string>); +} + +TEST(OverloadTest, VariantVisitDispatchesCorrectly) { + absl::variant<int, double, std::string> v(1); + auto overloaded = absl::Overload( + [](int) -> absl::string_view { return "int"; }, // + [](double) -> absl::string_view { return "double"; }, // + [](const std::string&) -> absl::string_view { return "string"; } // + ); + EXPECT_EQ("int", absl::visit(overloaded, v)); + v = 1.1; + EXPECT_EQ("double", absl::visit(overloaded, v)); + v = "hello"; + EXPECT_EQ("string", absl::visit(overloaded, v)); +} + +} // namespace + +#endif |