IVD/contrib/rustutils/easyuniquepointer.h

43 lines
1.1 KiB
C
Raw Normal View History

//This file is part of the IVD project and is licensed under the terms of the LGPL-3.0-only
#pragma once
#include <memory>
namespace RustUtils
{
//I know it's frowned upon to inherit from std classes but fucking byte me.
//This is just for the trivial case where I want a deep copy and nothing fancy.
template<typename T>
class DeepCopyableUnique : public std::unique_ptr<T>
{
public:
typedef std::unique_ptr<T> Parent;
DeepCopyableUnique() noexcept {}
DeepCopyableUnique(const DeepCopyableUnique& other) noexcept:
Parent(other.get() ? std::make_unique<T>(*other.get())
: nullptr)
{}
DeepCopyableUnique(const Parent&& other) noexcept:
Parent(other)
{}
DeepCopyableUnique& operator=(const DeepCopyableUnique& other) noexcept
{
if(other.get())
Parent::operator=(std::make_unique<T>(*other.get()));
else Parent::reset();
return *this;
}
DeepCopyableUnique& makeCopy(const T& other)
{
Parent::operator=(std::make_unique<T>(other));
return *this;
}
};
}//RustUtils