Think that this macro does exactly what you want:
#define DYN_IF(dest_type, dest_ptr, src_ptr) \ if((src_ptr).isType<dest_type>()) \ if(int dest_type##dest_ptr = 1) \ for(Ptr<dest_type> dest_ptr = (src_ptr).convertAs<dest_type>(); \ dest_type##dest_ptr; \ dest_type##dest_ptr=0)
Using:
ObjectPtr ptr; DYN_IF(Foo, foo_ptr, ptr) { // foo_ptr is Ptr<Foo> } DYN_IF(Bar, bar_ptr, ptr) // Works without braces too for single statement // bar_ptr is Ptr<Bar>
I would not recommend this material in code that should be read by someone else, but since you mentioned the word "macro" ...
In addition, I would not pretend that this has to do with pattern matching in the Haskell / OCaml style. Check out Scala if you want a language that has semantics similar to C ++ (well, sort) and true pattern matching.
Manuel
source share