| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- //
- // match.hpp
- // case-matcher
- //
- // Created by Sam Jaffe on 9/10/16.
- //
- #pragma once
- #include <tuple>
- namespace matcher {
- struct {} any;
- using any_t = decltype(any);
-
- template <typename T>
- bool operator==(T const &, any_t) { return true; }
- template <typename T>
- bool operator==(any_t, T const &) { return true; }
-
- template <typename... Args>
- struct matcher {
- public:
- matcher(Args &&... args) : value(std::forward<Args>(args)...) {}
-
- operator bool( ) const { return true; }
- bool unmatched( ) const { return ! satisfied; }
-
- template <typename... NArgs>
- bool matches(NArgs &&... args) const {
- return value == std::tuple<NArgs...>(std::forward<NArgs>(args)...);
- }
- private:
- std::tuple<Args...> value;
- bool satisfied = false;
- };
-
- template <typename... Args>
- matcher<Args...> make_matcher(Args &&... args) {
- return matcher<Args...>(std::forward<Args>(args)...);
- }
- }
- #define match( ... ) \
- if ( auto const & _matcher_local = \
- ::matcher::make_matcher( __VA_ARGS__ ) )
- #define with( ... ) \
- if ( _matcher_local.unmatched( ) && \
- _matcher_local.matches( __VA_ARGS__ ) )
|