Inheritance Is The Base Class of Evil

链接

#include "pch.h"

#include <cassert>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

struct my_struct_t
{
};

void draw(const my_struct_t& x, ostream& out, size_t pos)
{
    out << string(pos, ' ') << "my_struct_t" << endl;
}

template <typename T>
void draw(const T& x, ostream& out, size_t pos)
{
    out << string(pos, ' ') << x << endl;
}

class object_t
{
public:
    template <typename T>
    object_t(T x) : self_(std::make_shared<model<T>>(std::move(x)))
    {
        cout << "ctor" << endl;
    }

    //object_t(const object_t& x) : self_(x.self_->copy_())
    //{
    //  cout << "copy" << endl;
    //}

    //object_t(object_t&&) noexcept = default;

    //object_t& operator=(const object_t& x)
    //{
    //  object_t tmp(x);
    //  *this = std::move(tmp);
    //  return *this;
    //}

    //object_t& operator=(object_t&&) noexcept = default;

    friend void draw(const object_t& x, ostream& out, size_t pos)
    {
        x.self_->draw_(out, pos);
    }

private:

    struct concept_t
    {
        virtual ~concept_t() = default;
        //  virtual concept_t* copy_() const = 0;
        virtual void draw_(ostream&, size_t) const = 0;
    };

    template <typename T>
    struct model : concept_t
    {
        model(T x) : data_(std::move(x))
        {
        }

        //concept_t* copy_() const override { return new model(*this); }

        void draw_(ostream& out, size_t pos) const override
        {
            draw(data_, out, pos);
        }

        T data_;
    };

    shared_ptr<const concept_t> self_;
};

using document_t = vector<object_t>;

void draw(const document_t& x, ostream& out, size_t pos)
{
    out << string(pos, ' ') << "<document>" << endl;
    for (auto& e : x)
        draw(e, out, pos + 2);
    out << string(pos, ' ') << "</document>" << endl;
}

using history_t = vector<document_t>;

void commit(history_t& x)
{
    assert(x.size());
    x.push_back(x.back());
}

void undo(history_t& x)
{
    assert(x.size());
    x.pop_back();
}

document_t& current(history_t& x)
{
    assert(x.size());
    return x.back();
}

void main()
{
    //document_t doc;

    //doc.emplace_back(0);
    //doc.emplace_back(1);
    //doc.emplace_back(doc);
    //doc.emplace_back(2);
    //doc.emplace_back(3);
    //doc.emplace_back(my_struct_t());

    //draw(doc, cout, 0);


    history_t h(1);

    current(h).emplace_back(0);
    current(h).emplace_back("hello");

    draw(current(h), cout, 0);

    cout << "-------------------------------------------" << endl;

    commit(h);

    current(h).emplace_back(current(h));
    current(h).emplace_back(my_struct_t());
    current(h)[1] = string("World");

    draw(current(h), cout, 0);

    cout << "-------------------------------------------" << endl;

    undo(h);

    draw(current(h), cout, 0);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值