match.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // match.hpp
  3. // case-matcher
  4. //
  5. // Created by Sam Jaffe on 9/10/16.
  6. //
  7. #pragma once
  8. #include <array>
  9. #include <tuple>
  10. namespace matcher {
  11. struct {} any;
  12. using any_t = decltype(any);
  13. template <typename T>
  14. bool operator==(T const &, any_t) { return true; }
  15. }
  16. namespace matcher {
  17. template <typename T, size_t I>
  18. struct any_of_t {
  19. std::array<T, I> options;
  20. };
  21. template <typename T, size_t I>
  22. bool operator==(T const & t, any_of_t<T, I> const & of) {
  23. return std::find(of.options.begin(), of.options.end(), t) != of.options.end();
  24. }
  25. template <typename... Args>
  26. any_of_t<typename std::common_type<Args...>::type, sizeof...(Args)> any_of(Args &&... args) {
  27. return {{args...}};
  28. }
  29. }
  30. namespace matcher {
  31. template <typename Container>
  32. struct any_in_t {
  33. Container options;
  34. };
  35. template <typename T, typename Container>
  36. bool operator==(T const & t, any_in_t<Container> const & of) {
  37. return std::find(of.options.begin(), of.options.end(), t) != of.options.end();
  38. }
  39. template <typename Container>
  40. any_in_t<Container> any_in(Container && args) {
  41. return {args};
  42. }
  43. }
  44. namespace matcher {
  45. template <typename Pred>
  46. struct predicate_t {
  47. Pred predicate;
  48. };
  49. template <typename T, typename Pred>
  50. bool operator==(T const & t, predicate_t<Pred> const & of) {
  51. return of.predicate(t);
  52. }
  53. template <typename Pred>
  54. predicate_t<Pred> matches(Pred && args) {
  55. return {args};
  56. }
  57. }
  58. namespace matcher {
  59. template <typename... Args>
  60. struct matcher {
  61. public:
  62. matcher(Args &&... args) : value(std::forward<Args>(args)...) {}
  63. operator bool( ) const { return true; }
  64. bool unmatched( ) const { return ! satisfied; }
  65. template <typename... NArgs>
  66. bool matches(NArgs &&... args) const {
  67. bool const matched = value == std::make_tuple(std::forward<NArgs>(args)...);
  68. satisfied |= matched;
  69. return matched;
  70. }
  71. private:
  72. std::tuple<Args...> value;
  73. mutable bool satisfied = false;
  74. };
  75. template <typename... Args>
  76. matcher<Args...> make_matcher(Args &&... args) {
  77. return matcher<Args...>(std::forward<Args>(args)...);
  78. }
  79. }
  80. #define match( ... ) \
  81. if ( auto const & _matcher_local = \
  82. ::matcher::make_matcher( __VA_ARGS__ ) )
  83. #define with( ... ) \
  84. if ( _matcher_local.unmatched( ) && \
  85. _matcher_local.matches( __VA_ARGS__ ) )
  86. #define else_with( ... ) \
  87. else if ( _matcher_local.matches( __VA_ARGS__ ) )
  88. #define and_with( ... ) \
  89. if ( _matcher_local.matches( __VA_ARGS__ ) )
  90. #define nomatch( ) \
  91. if ( _matcher_local.unmatched( ) )