Skip to content

Instantly share code, notes, and snippets.

@cdacamar
Created July 7, 2016 18:24
Show Gist options
  • Save cdacamar/fbdca1ce6c6b38e1fcc4c8b280d21757 to your computer and use it in GitHub Desktop.
Save cdacamar/fbdca1ce6c6b38e1fcc4c8b280d21757 to your computer and use it in GitHub Desktop.
struct circle_t;
struct triangle_t;
struct square_t;
struct shape_t {
using visitor_t = vdsp::visitor_t<circle_t, triangle_t, square_t>;
virtual void accept(const visitor_t&) const = 0;
virtual void do_thing() const = 0;
};
struct circle_t : shape_t {
void accept(const visitor_t& visitor) const override { visitor(*this); }
void do_thing() const override { }
};
struct triangle_t : shape_t {
void accept(const visitor_t& visitor) const override { visitor(*this); }
void do_thing() const override { }
};
struct square_t : shape_t {
void accept(const visitor_t& visitor) const override { visitor(*this); }
void do_thing() const override { }
};
struct bar;
struct foobar;
struct foo {
using visitor_t = vdsp::visitor_t<bar, foobar>;
virtual void accept(const visitor_t&) const = 0;
};
struct bar : foo {
void accept(const visitor_t& visitor) const override { visitor(*this); }
};
struct foobar : foo {
void accept(const visitor_t& visitor) const override { visitor(*this); }
};
struct thingy {
bool operator()(const circle_t&, const bar&) const { return true; }
bool operator()(const circle_t&, const foobar&) const { return false; }
bool operator()(const triangle_t&, const bar&) const { return true; }
bool operator()(const triangle_t&, const foobar&) const { return true; }
bool operator()(const square_t&, const bar&) const { return true; }
bool operator()(const square_t&, const foobar&) const { return true; }
};
bool do_thingy(const shape_t& shape, const foo& f) {
return match<bool>(shape, f, thingy{});
}
int main() {
circle_t circle;
foobar f;
std::cout<<std::boolalpha<<do_thingy(circle, f)<<'\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment