Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <memory>
- #include <vector>
- #include <iostream>
- #include <common.h>
- class Shape;
- typedef std::unique_ptr<Shape> shape_ptr;
- class Shape{
- public:
- Shape(){};
- virtual ~Shape(){std::cout << "Shape destructor: " << this << std::endl;};
- virtual void draw() const = 0;
- virtual float area() const = 0;
- virtual float doSomething() = 0;
- virtual float doThat() const = 0;
- virtual shape_ptr clone() const = 0;
- virtual shape_ptr create() const = 0;
- };
- class Rectangle:public Shape{
- public:
- Rectangle(int height=0, int width=0):m_Height(height),m_Width(width){};
- ~Rectangle(){};
- virtual void draw() const;
- virtual float area() const;
- virtual float doSomething();
- virtual float doThat() const;
- virtual shape_ptr clone() const{ return shape_ptr(new Rectangle(*this)); };
- virtual shape_ptr create() const{ return shape_ptr(new Rectangle()); };
- private:
- int m_Height;
- int m_Width;
- };
- class Circle:public Shape{
- public:
- Circle(float radius=0):m_Radius(radius){};
- Circle(const Circle & other):m_Radius(other.m_Radius){};
- ~Circle(){std::cout << "Circle destructor: " << this << std::endl; };
- virtual void draw() const;
- virtual float area() const;
- virtual float doSomething();
- virtual float doThat() const;
- virtual shape_ptr clone() const{ return shape_ptr(new Circle(*this)); };
- virtual shape_ptr create() const{ return shape_ptr(new Circle()); };
- private:
- float m_Radius;
- };
- class ShapeInfo{
- public:
- typedef std::shared_ptr<Shape> ShapePtr;
- ShapeInfo(ShapePtr ptr, int id = 0):m_ShapePtr(ptr),m_nID(id){};
- ShapeInfo( const ShapeInfo & refDesc):m_ShapePtr(refDesc.m_ShapePtr),m_nID(refDesc.m_nID){};
- ~ShapeInfo(){std::cout << "ShapeInfo destructor!" << std::endl; }
- void swap( ShapeInfo & );
- ShapeInfo operator = ( ShapeInfo );
- void draw() const{ m_ShapePtr->draw(); };
- float area() const{ return m_ShapePtr->area(); };
- float doSomething(){ return m_ShapePtr->doSomething(); };
- float doThat(){ return m_ShapePtr->doThat(); };
- private:
- ShapePtr m_ShapePtr;
- int m_nID;
- };
- class ShapeContainerInfo{
- public:
- typedef std::vector<ShapeInfo> ShapeContainer;
- ShapeContainerInfo( ){ std::cout << "ShapeContainerNew Default constructor" << std::endl; };
- ShapeContainerInfo( ShapeContainerInfo && other):m_vect(std::move(other.m_vect)){ std::cout << "ShapeContainerNew Move constructor" << std::endl; };
- ShapeContainerInfo( const ShapeContainerInfo & other ):m_vect(other.m_vect){ std::cout << "ShapeContainerNew Reference constructor" << std::endl; };
- ~ShapeContainerInfo(){ std::cout << "ShapeContainerNew destructor" << std::endl; };
- const ShapeInfo & operator []( int ) const;
- //ShapeInfo & operator []( int );
- void swap(ShapeContainerInfo & );
- ShapeContainerInfo & operator = (ShapeContainerInfo );
- int size() const{ return static_cast<int>(m_vect.size()); };
- friend ShapeContainerInfo createBasicShapesInfo();
- friend ShapeContainerInfo createComplexShapesInfo();
- private:
- ShapeContainer m_vect;
- };
- class ShapeContainer{
- public:
- //typedef std::unique_ptr<Shape> ShapePtr;
- typedef std::shared_ptr<Shape> ShapePtr;
- typedef std::vector<ShapePtr> ShapePtrContainer;
- ShapeContainer(){};
- ShapeContainer( ShapeContainer && );
- ShapeContainer( const ShapeContainer & );
- ~ShapeContainer(){};
- const ShapePtr & at( int ) const;
- const ShapePtr & operator[]( int ) const;
- //ShapePtr & operator[]( int );
- ShapeContainer & operator = (ShapeContainer&&);
- int size() const{ return static_cast<int>(m_vect.size()); };
- friend ShapeContainer createBasicShapes();
- friend ShapeContainer createComplexShapes();
- friend void swap(ShapeContainer& x, ShapeContainer& y) {x.m_vect.swap(y.m_vect);}
- private:
- ShapePtrContainer m_vect;
- };
- void Rectangle::draw() const{
- std::cout << "draw rectangle at address: " << this << std::endl;
- }
- float Rectangle::area() const{
- float area = static_cast<float>(m_Height*m_Width);
- std::cout << "this is the Rectangle's area: " << area << std::endl;
- return area;
- }
- float Rectangle::doSomething(){
- std::cout << "Rectangle doing Something!" << std::endl;
- return (1.0);
- }
- float Rectangle::doThat() const {
- std::cout << "Rectangle doThat!" << std::endl;
- return (1.0);
- }
- /////////////////////////////////////////////////////////////////
- void Circle::draw() const{
- std::cout << "draw circle at address: " << this << std::endl;
- }
- float Circle::area() const{
- float area = static_cast<float>(m_Radius*2*3.1415);
- std::cout << "this is the Circle's area: " << area << std::endl;
- return area;
- }
- float Circle::doSomething(){
- std::cout << "Circle doing Something!" << std::endl;
- return (1.0);
- }
- float Circle::doThat() const{
- std::cout << "Circle doThat!" << std::endl;
- return (1.0);
- }
- /////////////////////////////////////////////////////////////////
- void ShapeInfo::swap( ShapeInfo & elem ){
- using std::swap;
- swap(m_ShapePtr, elem.m_ShapePtr);
- swap(m_nID, elem.m_nID);
- }
- ShapeInfo ShapeInfo::operator = ( ShapeInfo other ){
- other.swap(*this);
- return (*this);
- }
- /////////////////////////////////////////////////////////////////
- /*
- inline const ShapeInfo & ShapeContainerNew::at( int index ) const{
- return m_vect[index];
- }*/
- void ShapeContainerInfo::swap( ShapeContainerInfo & other ){
- using std::swap;
- swap(m_vect, other.m_vect);
- }
- ShapeContainerInfo & ShapeContainerInfo::operator = (ShapeContainerInfo other ){
- other.swap(*this);
- return (*this);
- }
- ShapeContainerInfo createBasicShapesInfo(){
- ShapeContainerInfo cont;
- cont.m_vect.push_back( ShapeInfo( ShapeInfo::ShapePtr(new Circle()), 0) );
- cont.m_vect.push_back( ShapeInfo( ShapeInfo::ShapePtr(new Rectangle()), 1) );
- return cont;
- }
- ShapeContainerInfo createComplexShapesInfo(){
- ShapeContainerInfo cont;
- cont.m_vect.push_back( ShapeInfo( ShapeInfo::ShapePtr(new Rectangle()), 3) );
- return cont;
- }
- const ShapeInfo & ShapeContainerInfo::operator []( int index ) const{
- std::cout << "const ShapeContainerInfo::operator [] const" << std::endl;
- return m_vect[index];
- }
- /*
- ShapeInfo & ShapeContainerInfo::operator []( int index ){
- std::cout << "const ShapeContainerInfo::operator [] const" << std::endl;
- return m_vect[index];
- }*/
- /////////////////////////////////////////////////////////////////
- ShapeContainer::ShapeContainer(ShapeContainer && other)
- :m_vect(std::move(other.m_vect))
- {
- }
- ShapeContainer & ShapeContainer::operator = (ShapeContainer&& other){
- m_vect = std::move(other.m_vect);
- return (*this);
- }
- ShapeContainer::ShapeContainer( const ShapeContainer & other ){
- for(int i=0; i<(int)other.m_vect.size(); i++){
- m_vect.push_back( ShapePtr( other.m_vect[i] ? other.m_vect[i]->clone() : nullptr ) );
- }
- /*
- auto begin = other.m_vect.begin();
- auto end = other.m_vect.end();
- std::transform(begin, end, std::back_inserter(m_vect), [](ShapePtr const& p)
- { return p->clone(); });
- */
- /*
- for(auto&& p: other.m_vect) {
- m_vect.push_back(p->clone());
- }*/
- }
- ShapeContainer createBasicShapes(){
- ShapeContainer cont;
- cont.m_vect.push_back(ShapeContainer::ShapePtr(new Circle()));
- cont.m_vect.push_back(ShapeContainer::ShapePtr(new Rectangle()));
- return cont;
- }
- ShapeContainer createComplexShapes(){
- ShapeContainer cont;
- cont.m_vect.push_back(ShapeContainer::ShapePtr(new Rectangle()));
- return cont;
- }
- const ShapeContainer::ShapePtr & ShapeContainer::at( int index ) const{
- std::cout << "const ShapeContainer::at(index) const" << std::endl;
- return m_vect[index];
- }
- const ShapeContainer::ShapePtr & ShapeContainer::operator[]( int index ) const{
- std::cout << "const ShapeContainer::operator[] const" << std::endl;
- return m_vect[index];
- }
- int main(int argc, char ** argv){
- ShapeContainer container1 = createBasicShapes();
- for (int i =0; i < container1.size(); i++){
- float tmp = container1[i]->doSomething();
- }
- ShapeContainerInfo container2 = createBasicShapesInfo();
- for (int i =0; i < container2.size(); i++){
- std::cout << i << std::endl;
- float tmp = container2[i].doSomething();
- //float tmp = container2.m_vect[i].doSomething();
- }
- return (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement