1 | #include <Common/COW.h> |
2 | #include <iostream> |
3 | |
4 | |
5 | class IColumn : public COW<IColumn> |
6 | { |
7 | private: |
8 | friend class COW<IColumn>; |
9 | virtual MutablePtr clone() const = 0; |
10 | |
11 | public: |
12 | IColumn() = default; |
13 | IColumn(const IColumn &) = default; |
14 | virtual ~IColumn() = default; |
15 | |
16 | virtual int get() const = 0; |
17 | virtual void set(int value) = 0; |
18 | |
19 | virtual MutablePtr test() const = 0; |
20 | }; |
21 | |
22 | using ColumnPtr = IColumn::Ptr; |
23 | using MutableColumnPtr = IColumn::MutablePtr; |
24 | |
25 | class ConcreteColumn : public COWHelper<IColumn, ConcreteColumn> |
26 | { |
27 | private: |
28 | friend class COWHelper<IColumn, ConcreteColumn>; |
29 | |
30 | int data; |
31 | ConcreteColumn(int data_) : data(data_) {} |
32 | ConcreteColumn(const ConcreteColumn &) = default; |
33 | |
34 | MutableColumnPtr test() const override |
35 | { |
36 | MutableColumnPtr res = create(123); |
37 | return res; |
38 | } |
39 | |
40 | public: |
41 | int get() const override { return data; } |
42 | void set(int value) override { data = value; } |
43 | }; |
44 | |
45 | |
46 | int main(int, char **) |
47 | { |
48 | ColumnPtr x = ConcreteColumn::create(1); |
49 | ColumnPtr y = x;//x->test(); |
50 | |
51 | std::cerr << "values: " << x->get() << ", " << y->get() << "\n" ; |
52 | std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n" ; |
53 | std::cerr << "addresses: " << x.get() << ", " << y.get() << "\n" ; |
54 | |
55 | { |
56 | MutableColumnPtr mut = std::move(*y).mutate(); |
57 | mut->set(2); |
58 | |
59 | std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << ", " << mut->use_count() << "\n" ; |
60 | std::cerr << "addresses: " << x.get() << ", " << y.get() << ", " << mut.get() << "\n" ; |
61 | y = std::move(mut); |
62 | } |
63 | |
64 | std::cerr << "values: " << x->get() << ", " << y->get() << "\n" ; |
65 | std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n" ; |
66 | std::cerr << "addresses: " << x.get() << ", " << y.get() << "\n" ; |
67 | |
68 | x = ConcreteColumn::create(0); |
69 | |
70 | std::cerr << "values: " << x->get() << ", " << y->get() << "\n" ; |
71 | std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n" ; |
72 | std::cerr << "addresses: " << x.get() << ", " << y.get() << "\n" ; |
73 | |
74 | { |
75 | MutableColumnPtr mut = std::move(*y).mutate(); |
76 | mut->set(3); |
77 | |
78 | std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << ", " << mut->use_count() << "\n" ; |
79 | std::cerr << "addresses: " << x.get() << ", " << y.get() << ", " << mut.get() << "\n" ; |
80 | y = std::move(mut); |
81 | } |
82 | |
83 | std::cerr << "values: " << x->get() << ", " << y->get() << "\n" ; |
84 | std::cerr << "refcounts: " << x->use_count() << ", " << y->use_count() << "\n" ; |
85 | |
86 | return 0; |
87 | } |
88 | |
89 | |