Created
February 17, 2014 12:21
-
-
Save loosechainsaw/9049615 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <memory> | |
#include <algorithm> | |
class drawable_concept{ | |
public: | |
drawable_concept() = default; | |
virtual ~drawable_concept() = default; | |
drawable_concept(drawable_concept const&) = delete; | |
drawable_concept& operator = (drawable_concept const& ) = delete; | |
virtual void draw() = 0; | |
}; | |
template<class T> | |
class drawable_model : public drawable_concept{ | |
public: | |
typedef T model_type; | |
drawable_model(T const& model) : model_(model){} | |
void draw(){ | |
model_.draw(); | |
} | |
~drawable_model() = default; | |
private: | |
T model_; | |
}; | |
template<class T> | |
class drawable_forwarder{ | |
public: | |
drawable_forwarder(T const& item) : item_(item){} | |
inline void draw(){ | |
item_->draw(); | |
} | |
private: | |
T item_; | |
}; | |
class graphics_surface{ | |
public: | |
void render(){ | |
std::for_each(std::begin(controls_), std::end(controls_), [] (std::unique_ptr<drawable_concept> const& control){ | |
control->draw(); | |
}); | |
} | |
template<class T> | |
void push_back(T control){ | |
auto t = new drawable_model<T>(std::move(control)); | |
controls_.push_back(std::unique_ptr<drawable_concept>(t)); | |
} | |
private: | |
std::vector<std::unique_ptr<drawable_concept>> controls_; | |
}; | |
struct triangle{ | |
void draw(){ | |
std::cout << "Triangle" << std::endl; | |
} | |
}; | |
struct square{ | |
void draw(){ | |
std::cout << "Square" << std::endl; | |
} | |
}; | |
int main(int argc, const char * argv[]) | |
{ | |
graphics_surface surface; | |
surface.push_back(triangle()); | |
surface.push_back(square()); | |
std::shared_ptr<triangle> ptr(new triangle); | |
drawable_forwarder<std::shared_ptr<triangle>> fwd(ptr); | |
surface.push_back(fwd); | |
surface.render(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment