| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- //
- // match.hpp
- // case-matcher
- //
- // Created by Sam Jaffe on 9/10/16.
- //
- #pragma once
- #include <tuple>
- namespace matcher {
- 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 {
- bool const matched = value == std::make_tuple(std::forward<NArgs>(args)...);
- satisfied |= matched;
- return matched;
- }
- private:
- std::tuple<Args...> value;
- mutable 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__ ) )
- #define else_with( ... ) \
- else if ( _matcher_local.matches( __VA_ARGS__ ) )
- #define and_with( ... ) \
- if ( _matcher_local.matches( __VA_ARGS__ ) )
- #define nomatch( ) \
- if ( _matcher_local.unmatched( ) )
- #include "matchers/any_in.hpp"
- #include "matchers/any_of.hpp"
- #include "matchers/any.hpp"
- #include "matchers/matches.hpp"
|