MCPcopy Create free account
hub / github.com/BowenFu/matchit.cpp / matchPatterns

Function matchPatterns

include/matchit.h:1865–1913  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1863
1864 template <typename Value, typename... PatternPairs>
1865 constexpr auto matchPatterns(Value &&value, PatternPairs const &...patterns)
1866 {
1867 using RetType = typename PatternPairsRetType<PatternPairs...>::RetType;
1868 using TypeTuple = decltype(std::tuple_cat(
1869 std::declval<typename PatternTraits<typename PatternPairs::PatternT>::
1870 template AppResultTuple<Value>>()...));
1871
1872 // expression, has return value.
1873 if constexpr (!std::is_same_v<RetType, void>)
1874 {
1875 constexpr auto const func =
1876 [](auto const &pattern, auto &&value, RetType &result) constexpr->bool
1877 {
1878 auto context = typename ContextTrait<TypeTuple>::ContextT{};
1879 if (pattern.matchValue(std::forward<Value>(value), context))
1880 {
1881 result = pattern.execute();
1882 processId(pattern, 0, IdProcess::kCANCEL);
1883 return true;
1884 }
1885 return false;
1886 };
1887 RetType result{};
1888 bool const matched = (func(patterns, value, result) || ...);
1889 if (!matched)
1890 {
1891 throw std::logic_error{"Error: no patterns got matched!"};
1892 }
1893 static_cast<void>(matched);
1894 return result;
1895 }
1896 else
1897 // statement, no return value, mismatching all patterns is not an error.
1898 {
1899 auto const func = [](auto const &pattern, auto &&value) -> bool
1900 {
1901 auto context = typename ContextTrait<TypeTuple>::ContextT{};
1902 if (pattern.matchValue(std::forward<Value>(value), context))
1903 {
1904 pattern.execute();
1905 processId(pattern, 0, IdProcess::kCANCEL);
1906 return true;
1907 }
1908 return false;
1909 };
1910 bool const matched = (func(patterns, value) || ...);
1911 static_cast<void>(matched);
1912 }
1913 }
1914
1915 } // namespace impl
1916

Callers 1

operator()Method · 0.70

Calls 3

processIdFunction · 0.85
executeMethod · 0.80
matchValueMethod · 0.45

Tested by

no test coverage detected