2019-04-19 20:21:17 +00:00
|
|
|
#include <Common/COW.h>
|
2017-12-11 02:59:56 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
2019-04-19 20:21:17 +00:00
|
|
|
class IColumn : public COW<IColumn>
|
2017-12-11 02:59:56 +00:00
|
|
|
{
|
|
|
|
private:
|
2019-04-19 20:21:17 +00:00
|
|
|
friend class COW<IColumn>;
|
2017-12-16 04:29:34 +00:00
|
|
|
virtual MutablePtr clone() const = 0;
|
2017-12-13 01:27:53 +00:00
|
|
|
|
|
|
|
public:
|
2019-01-04 13:54:39 +00:00
|
|
|
IColumn() = default;
|
|
|
|
IColumn(const IColumn &) = default;
|
|
|
|
virtual ~IColumn() = default;
|
2017-12-13 01:27:53 +00:00
|
|
|
|
|
|
|
virtual int get() const = 0;
|
|
|
|
virtual void set(int value) = 0;
|
2017-12-14 03:56:56 +00:00
|
|
|
|
|
|
|
virtual MutablePtr test() const = 0;
|
2017-12-13 01:27:53 +00:00
|
|
|
};
|
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
using ColumnPtr = IColumn::Ptr;
|
|
|
|
using MutableColumnPtr = IColumn::MutablePtr;
|
|
|
|
|
2019-04-19 20:21:17 +00:00
|
|
|
class ConcreteColumn : public COWHelper<IColumn, ConcreteColumn>
|
2017-12-13 01:27:53 +00:00
|
|
|
{
|
|
|
|
private:
|
2019-04-19 20:21:17 +00:00
|
|
|
friend class COWHelper<IColumn, ConcreteColumn>;
|
2017-12-11 02:59:56 +00:00
|
|
|
|
|
|
|
int data;
|
2020-03-18 03:27:32 +00:00
|
|
|
explicit ConcreteColumn(int data_) : data(data_) {}
|
2017-12-13 01:27:53 +00:00
|
|
|
ConcreteColumn(const ConcreteColumn &) = default;
|
2017-12-11 02:59:56 +00:00
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
MutableColumnPtr test() const override
|
|
|
|
{
|
|
|
|
MutableColumnPtr res = create(123);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-12-11 02:59:56 +00:00
|
|
|
public:
|
2017-12-13 01:27:53 +00:00
|
|
|
int get() const override { return data; }
|
|
|
|
void set(int value) override { data = value; }
|
2017-12-11 02:59:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int main(int, char **)
|
|
|
|
{
|
2017-12-13 01:27:53 +00:00
|
|
|
ColumnPtr x = ConcreteColumn::create(1);
|
2017-12-16 04:29:34 +00:00
|
|
|
ColumnPtr y = x;//x->test();
|
2017-12-11 02:59:56 +00:00
|
|
|
|
|
|
|
std::cerr << "values: " << x->get() << ", " << y->get() << "\n";
|
|
|
|
std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n";
|
2017-12-14 03:56:56 +00:00
|
|
|
std::cerr << "addresses: " << x.get() << ", " << y.get() << "\n";
|
2017-12-11 02:59:56 +00:00
|
|
|
|
|
|
|
{
|
2019-03-24 01:42:58 +00:00
|
|
|
MutableColumnPtr mut = std::move(*y).mutate();
|
2017-12-16 04:29:34 +00:00
|
|
|
mut->set(2);
|
2017-12-11 02:59:56 +00:00
|
|
|
|
2017-12-16 04:29:34 +00:00
|
|
|
std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << ", " << mut->use_count() << "\n";
|
|
|
|
std::cerr << "addresses: " << x.get() << ", " << y.get() << ", " << mut.get() << "\n";
|
|
|
|
y = std::move(mut);
|
2017-12-11 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "values: " << x->get() << ", " << y->get() << "\n";
|
|
|
|
std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n";
|
2017-12-14 03:56:56 +00:00
|
|
|
std::cerr << "addresses: " << x.get() << ", " << y.get() << "\n";
|
2017-12-11 02:59:56 +00:00
|
|
|
|
2017-12-13 01:27:53 +00:00
|
|
|
x = ConcreteColumn::create(0);
|
2017-12-11 02:59:56 +00:00
|
|
|
|
|
|
|
std::cerr << "values: " << x->get() << ", " << y->get() << "\n";
|
|
|
|
std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n";
|
2017-12-14 03:56:56 +00:00
|
|
|
std::cerr << "addresses: " << x.get() << ", " << y.get() << "\n";
|
2017-12-11 02:59:56 +00:00
|
|
|
|
|
|
|
{
|
2019-03-24 01:42:58 +00:00
|
|
|
MutableColumnPtr mut = std::move(*y).mutate();
|
2017-12-16 04:29:34 +00:00
|
|
|
mut->set(3);
|
2017-12-11 02:59:56 +00:00
|
|
|
|
2017-12-16 04:29:34 +00:00
|
|
|
std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << ", " << mut->use_count() << "\n";
|
|
|
|
std::cerr << "addresses: " << x.get() << ", " << y.get() << ", " << mut.get() << "\n";
|
|
|
|
y = std::move(mut);
|
2017-12-11 02:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "values: " << x->get() << ", " << y->get() << "\n";
|
|
|
|
std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n";
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|