Created
February 25, 2014 13:40
-
-
Save loosechainsaw/9208889 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
#ifndef DRAWING | |
#define DRAWING | |
#include <memory> | |
#include <iostream> | |
namespace concepts{ | |
namespace drawing{ | |
struct concept_t{ | |
concept_t() = default; | |
concept_t(concept_t const&) = delete; | |
concept_t(concept_t&& ) = delete; | |
concept_t& operator = (concept_t const&) = delete; | |
concept_t& operator = (concept_t&&) = delete; | |
virtual void draw() = 0; | |
}; | |
template<class T> | |
class model_t: public concept_t{ | |
public: | |
model_t(T const& concept) : concept_(concept){ | |
} | |
model_t(T&& concept) : concept_(std::move(concept)){ | |
} | |
model_t(model_t const&) = delete; | |
model_t(model_t&& ) = delete; | |
model_t& operator = (model_t const&) = delete; | |
model_t& operator = (model_t&&) = delete; | |
void draw(){ | |
concept_.draw(); | |
} | |
private: | |
T concept_; | |
}; | |
class drawable{ | |
public: | |
template<class T> | |
drawable(T const& concept) { | |
auto ptr = new model_t<T>(concept); | |
impl_ = std::shared_ptr<concept_t>(ptr); | |
} | |
drawable(drawable const&) = default; | |
drawable(drawable&& ) = default; | |
drawable& operator = (drawable const&) = default; | |
drawable& operator = (drawable&&) = default; | |
void draw(){ | |
impl_->draw(); | |
} | |
private: | |
std::shared_ptr<concept_t> impl_; | |
}; | |
} | |
} | |
namespace graphics { | |
class graphics_display{ | |
public: | |
template<class T> | |
graphics_display(T const& concept) : control_(concept){ | |
} | |
void draw(){ | |
control_.draw(); | |
} | |
private: | |
concepts::drawing::drawable control_; | |
}; | |
class rectangle{ | |
public: | |
void draw(){ | |
std::cout << "Rectangle\n"; | |
} | |
}; | |
class square{ | |
public: | |
void draw(){ | |
std::cout << "Square\n"; | |
} | |
}; | |
class circle{ | |
public: | |
void draw(){ | |
std::cout << "Circle\n"; | |
} | |
}; | |
} | |
#endif | |
// Main file starts here | |
#include "drawing.hpp" | |
int main(int argc, const char * argv[]) | |
{ | |
graphics::circle c; | |
graphics::graphics_display g(c); | |
g.draw(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment