guanaqo develop
Utilities for scientific software
Loading...
Searching...
No Matches
copyable-unique_ptr.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @ingroup memory
5/// Clone-on-copy unique_ptr wrapper.
6
7#include <memory>
8
9namespace guanaqo {
10
11/// `std::unique_ptr` is move-only, this class wraps such a pointer and provides
12/// copy operations as well.
13/// @ingroup memory
14template <class T>
16 copyable_unique_ptr(std::unique_ptr<T> ptr) : ptr{std::move(ptr)} {}
19 : ptr{o.ptr ? std::make_unique<T>(*o.ptr) : nullptr} {}
21 this->ptr = o.ptr ? std::make_unique<T>(*o.ptr) : nullptr;
22 return *this;
23 }
25 copyable_unique_ptr &operator=(copyable_unique_ptr &&) noexcept = default;
26
27 operator std::unique_ptr<T> &() & { return ptr; }
28 operator const std::unique_ptr<T> &() const & { return ptr; }
29 operator std::unique_ptr<T> &&() && { return std::move(ptr); }
30
31 std::unique_ptr<T> &operator->() { return ptr; }
32 const std::unique_ptr<T> &operator->() const { return ptr; }
33 auto &operator*() { return *ptr; }
34 auto &operator*() const { return *ptr; }
35
36 std::unique_ptr<T> ptr;
37};
38
39} // namespace guanaqo
copyable_unique_ptr(const copyable_unique_ptr &o)
copyable_unique_ptr(copyable_unique_ptr &&) noexcept=default
std::unique_ptr< T > & operator->()
const std::unique_ptr< T > & operator->() const
copyable_unique_ptr(std::unique_ptr< T > ptr)
copyable_unique_ptr & operator=(const copyable_unique_ptr &o)