match_method.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include <rpl/details/callable.h>
  9. namespace base {
  10. template <typename Data, typename Method, typename ...Methods>
  11. inline decltype(auto) match_method(
  12. Data &&data,
  13. Method &&method,
  14. Methods &&...methods) {
  15. using namespace rpl::details;
  16. if constexpr (is_callable_plain_v<Method, Data&&>) {
  17. return std::forward<Method>(method)(std::forward<Data>(data));
  18. } else {
  19. return match_method(
  20. std::forward<Data>(data),
  21. std::forward<Methods>(methods)...);
  22. }
  23. }
  24. template <
  25. typename Data1,
  26. typename Data2,
  27. typename Method,
  28. typename ...Methods>
  29. inline decltype(auto) match_method2(
  30. Data1 &&data1,
  31. Data2 &&data2,
  32. Method &&method,
  33. Methods &&...methods) {
  34. using namespace rpl::details;
  35. if constexpr (is_callable_plain_v<Method, Data1&&, Data2&&>) {
  36. return std::forward<Method>(method)(
  37. std::forward<Data1>(data1),
  38. std::forward<Data2>(data2));
  39. } else {
  40. return match_method2(
  41. std::forward<Data1>(data1),
  42. std::forward<Data2>(data2),
  43. std::forward<Methods>(methods)...);
  44. }
  45. }
  46. } // namespace base