futriix/src/cowptr.h
John Sully 5449859ede Perf fixes for expire snapshots
Former-commit-id: da805e4442815c89e85ca7b9fc855dd11ef2a6c8
2019-12-17 17:39:04 -05:00

56 lines
1.0 KiB
C++

#pragma once
template <class T>
class CowPtr
{
public:
typedef std::shared_ptr<T> RefPtr;
private:
mutable RefPtr m_sp;
void detach()
{
if( !( m_sp == nullptr || m_sp.unique() ) ) {
m_sp = std::make_shared<T>(*m_sp);
}
}
public:
CowPtr() = default;
CowPtr(const RefPtr& refptr)
: m_sp(refptr)
{
}
bool operator==(std::nullptr_t) const
{
return m_sp == nullptr;
}
bool operator!=(std::nullptr_t) const
{
return m_sp != nullptr;
}
const T& operator*() const
{
return *m_sp;
}
T& operator*()
{
detach();
return *m_sp;
}
const T* operator->() const
{
return m_sp.operator->();
}
T* operator->()
{
detach();
return m_sp.operator->();
}
};