Advertisement
tinyevil

Untitled

Nov 29th, 2018
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #pragma once
  2. #include <utility>
  3.  
  4. #define match(e) \
  5.     { \
  6.     using __match_type = std::remove_pointer_t<decltype(e)>; \
  7.     auto __match_value = (e); \
  8.     if ( false ){
  9.  
  10. #define caseof(type, var) \
  11.     } else if ( type::Discriminate(__match_value->tag) ) { \
  12.         auto var = (type*)__match_value; \
  13.  
  14. #define casedef(var) } else { auto var = __match_value;
  15.  
  16. #define endmatch }}
  17.  
  18. #define match2(e1, e2) \
  19.     { \
  20.     using __match_type = std::remove_pointer_t<decltype(e1)>; \
  21.     auto __match_value_1 = (e1); \
  22.     auto __match_value_2 = (e2); \
  23.     if ( false ){ \
  24.  
  25. #define caseof2(type1, var1, type2, var2) \
  26.     } else if ( type1::Discriminate(__match_value_1->tag) && type2::Discriminate(__match_value_2->tag) ){ \
  27.         auto var1 = (type1*)__match_value_1; \
  28.         auto var2 = (type2*)__match_value_2;
  29.  
  30. #define casedef2(var1, var2) } else { \
  31.     auto var1 = __match_value_1; \
  32.     auto var2 = __match_value_2;
  33.  
  34. #define endmatch2 }}
  35.  
  36. #define SYNTHESIZE_PM_BASE(name, type) \
  37.     template<type::Tag _tag> \
  38.     struct name : type { \
  39.         template<class... Args> \
  40.         name(Args&&... args) : type(_tag, std::forward<Args>(args)...) {} \
  41.         static bool Discriminate(type::Tag tag){ return tag == _tag; } \
  42.     }
  43.  
  44. #define SYNTHESIZE_PM_BASE2(name, inherit, base_type) \
  45.     template<base_type::Tag _tag> \
  46.     struct name : inherit { \
  47.         template<class... Args> \
  48.         name(Args&&... args) : inherit(_tag, std::forward<Args>(args)...) {} \
  49.         static bool Discriminate(base_type::Tag tag){ return tag == _tag; } \
  50.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement