Overload.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <type_traits>
  18. #include <utility>
  19. /**
  20. * folly implementation of `std::overload` like functionality
  21. *
  22. * Example:
  23. * struct One {};
  24. * struct Two {};
  25. * boost::variant<One, Two> value;
  26. *
  27. * variant_match(value,
  28. * [] (const One& one) { ... },
  29. * [] (const Two& two) { ... });
  30. */
  31. namespace folly {
  32. namespace detail {
  33. template <typename...>
  34. struct Overload;
  35. template <typename Case, typename... Cases>
  36. struct Overload<Case, Cases...> : Overload<Cases...>, Case {
  37. Overload(Case c, Cases... cs)
  38. : Overload<Cases...>(std::move(cs)...), Case(std::move(c)) {}
  39. using Case::operator();
  40. using Overload<Cases...>::operator();
  41. };
  42. template <typename Case>
  43. struct Overload<Case> : Case {
  44. explicit Overload(Case c) : Case(std::move(c)) {}
  45. using Case::operator();
  46. };
  47. } // namespace detail
  48. /*
  49. * Combine multiple `Cases` in one function object
  50. */
  51. template <typename... Cases>
  52. decltype(auto) overload(Cases&&... cases) {
  53. return detail::Overload<typename std::decay<Cases>::type...>{
  54. std::forward<Cases>(cases)...};
  55. }
  56. /*
  57. * Match `Variant` with one of the `Cases`
  58. *
  59. * Note: you can also use `[] (const auto&) {...}` as default case
  60. *
  61. */
  62. template <typename Variant, typename... Cases>
  63. decltype(auto) variant_match(Variant&& variant, Cases&&... cases) {
  64. return apply_visitor(
  65. overload(std::forward<Cases>(cases)...), std::forward<Variant>(variant));
  66. }
  67. } // namespace folly