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

Function matchPatterns

develop/matchit/patterns.h:1558–1606  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1556
1557 template <typename Value, typename... PatternPairs>
1558 constexpr auto matchPatterns(Value &&value, PatternPairs const &...patterns)
1559 {
1560 using RetType = typename PatternPairsRetType<PatternPairs...>::RetType;
1561 using TypeTuple = decltype(std::tuple_cat(
1562 std::declval<typename PatternTraits<typename PatternPairs::PatternT>::
1563 template AppResultTuple<Value>>()...));
1564
1565 // expression, has return value.
1566 if constexpr (!std::is_same_v<RetType, void>)
1567 {
1568 constexpr auto const func =
1569 [](auto const &pattern, auto &&value, RetType &result) constexpr->bool
1570 {
1571 auto context = typename ContextTrait<TypeTuple>::ContextT{};
1572 if (pattern.matchValue(std::forward<Value>(value), context))
1573 {
1574 result = pattern.execute();
1575 processId(pattern, 0, IdProcess::kCANCEL);
1576 return true;
1577 }
1578 return false;
1579 };
1580 RetType result{};
1581 bool const matched = (func(patterns, value, result) || ...);
1582 if (!matched)
1583 {
1584 throw std::logic_error{"Error: no patterns got matched!"};
1585 }
1586 static_cast<void>(matched);
1587 return result;
1588 }
1589 else
1590 // statement, no return value, mismatching all patterns is not an error.
1591 {
1592 auto const func = [](auto const &pattern, auto &&value) -> bool
1593 {
1594 auto context = typename ContextTrait<TypeTuple>::ContextT{};
1595 if (pattern.matchValue(std::forward<Value>(value), context))
1596 {
1597 pattern.execute();
1598 processId(pattern, 0, IdProcess::kCANCEL);
1599 return true;
1600 }
1601 return false;
1602 };
1603 bool const matched = (func(patterns, value) || ...);
1604 static_cast<void>(matched);
1605 }
1606 }
1607
1608 } // namespace impl
1609

Callers 1

operator()Method · 0.70

Calls 3

processIdFunction · 0.85
executeMethod · 0.80
matchValueMethod · 0.45

Tested by

no test coverage detected