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