Attempt to make compositions with COWPtr more convenient

This commit is contained in:
Alexey Milovidov 2019-03-24 05:21:01 +03:00
parent 2dca389f20
commit c76a4e9dd2
3 changed files with 23 additions and 12 deletions

View File

@ -272,7 +272,7 @@ public:
MutablePtr mutate() const &&
{
MutablePtr res = std::move(*this).template COWPtr<IColumn>::mutate();
MutablePtr res = shallowMutate();
res->forEachSubcolumn([](Ptr & subcolumn) { subcolumn = std::move(*subcolumn).mutate(); });
return res;
}

View File

@ -175,7 +175,8 @@ public:
Ptr getPtr() const { return static_cast<Ptr>(derived()); }
MutablePtr getPtr() { return static_cast<MutablePtr>(derived()); }
MutablePtr mutate() const &&
protected:
MutablePtr shallowMutate() const
{
if (this->use_count() > 1)
return derived()->clone();
@ -183,6 +184,12 @@ public:
return assumeMutable();
}
public:
MutablePtr mutate() const &&
{
return shallowMutate();
}
MutablePtr assumeMutable() const
{
return const_cast<COWPtr*>(this)->getPtr();
@ -281,4 +288,7 @@ public:
static MutablePtr create(std::initializer_list<T> && arg) { return create(std::forward<std::initializer_list<T>>(arg)); }
typename Base::MutablePtr clone() const override { return typename Base::MutablePtr(new Derived(*derived())); }
protected:
MutablePtr shallowMutate() const { return MutablePtr(static_cast<Derived *>(Base::shallowMutate().get())); }
};

View File

@ -6,7 +6,9 @@ class IColumn : public COWPtr<IColumn>
{
private:
friend class COWPtr<IColumn>;
virtual MutablePtr clone() const = 0;
virtual MutablePtr deepMutate() const { return shallowMutate(); }
public:
IColumn() = default;
@ -16,7 +18,7 @@ public:
virtual int get() const = 0;
virtual void set(int value) = 0;
virtual MutablePtr mutate() const && { return std::move(*this).template COWPtr<IColumn>::mutate(); }
MutablePtr mutate() const && { return deepMutate(); }
};
using ColumnPtr = IColumn::Ptr;
@ -39,7 +41,6 @@ public:
class ColumnComposition : public COWPtrHelper<IColumn, ColumnComposition>
{
private:
using Base = COWPtrHelper<IColumn, ColumnComposition>;
friend class COWPtrHelper<IColumn, ColumnComposition>;
ConcreteColumn::WrappedPtr wrapped;
@ -47,17 +48,17 @@ private:
ColumnComposition(int data) : wrapped(ConcreteColumn::create(data)) {}
ColumnComposition(const ColumnComposition &) = default;
IColumn::MutablePtr deepMutate() const override
{
std::cerr << "Mutating\n";
auto res = shallowMutate();
res->wrapped = std::move(*wrapped).mutate();
return res;
}
public:
int get() const override { return wrapped->get(); }
void set(int value) override { wrapped->set(value); }
IColumn::MutablePtr mutate() const && override
{
std::cerr << "Mutating\n";
auto res = std::move(*this).Base::mutate();
static_cast<ColumnComposition *>(res.get())->wrapped = std::move(*wrapped).mutate();
return res;
}
};