Create a gist now

Instantly share code, notes, and snippets.

@ivme /type_smudging.cpp Secret
Created Aug 9, 2017

What would you like to do?
Implementation of type smudging for nodes and renderers
#include <string>
#include <vector>
#include <memory>
using namespace std;
struct image{};
struct renderable_concept {
virtual image render() const = 0;
};
template <class WRAPPED, class RENDERER>
struct renderable_model : public renderable_concept {
WRAPPED *w;
RENDERER r;
virtual image render() const final override {
return r.render(*w);
}
renderable_model(WRAPPED *w_, RENDERER r_) : w(w_), r(r_) {}
};
struct node {
template <class WRAPPED, class RENDERER>
node(WRAPPED *w_, RENDERER r_) :
p_renderable(new renderable_model<WRAPPED,RENDERER>(w_,r_)) {}
template <class RENDERER>
node(RENDERER r_) : node(this,r_) {}
image render() {return p_renderable->render();}
vector<shared_ptr<node>> children;
unique_ptr<renderable_concept> p_renderable;
};
struct text_node : public node {
template<class RENDERER>
text_node(RENDERER r) : node(this,r) {}
string val;
};
struct shape_node : public node {
template<class RENDERER>
shape_node(RENDERER r) : node(this,r) {}
};
struct color_renderer {
image render(node &) const {/*implementation*/};
image render(text_node &) const {/*implementation*/};
image render(shape_node &) const {/*implementation*/};
};
struct grayscale_renderer {
image render(node &) const {/*implementation*/};
image render(text_node &) const {/*implementation*/};
image render(shape_node &) const {/*implementation*/};
};
struct custom_node : public node {
template<class RENDERER>
custom_node(RENDERER r) : node(this,r) {}
};
struct custom_renderer : public color_renderer {
image render(custom_node &) const {/* implementation */}
};
int main() {
color_renderer cr{};
shared_ptr<node> my_text_node = std::make_shared<text_node>(cr);
my_text_node->render();
shared_ptr<text_node> n = make_shared<text_node>(color_renderer{});
n->val = "New string value!";
node root{color_renderer{}};
shared_ptr<node> tn = make_shared<text_node>(grayscale_renderer{});
shared_ptr<node> sn = make_shared<shape_node>(color_renderer{});
shared_ptr<node> cn = make_shared<custom_node>(custom_renderer{});
root.children = {tn,sn};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment