1 Star 1 Fork 0

俞寅达 / nicenet

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
CopyablePtr.hpp 1.73 KB
一键复制 编辑 原始数据 按行查看 历史
俞寅达 提交于 2021-11-14 19:57 . MongoAsync
#ifndef ____NICE__COPYABLEPTR____
#define ____NICE__COPYABLEPTR____
#include <memory>
namespace nicehero {
/*
A copyable unique_ptr
*/
template <class T>
class CopyablePtr {
public:
/** If value can be default-constructed, why not?
Then we don't have to move it in */
CopyablePtr() = default;
/// Move a value in.
explicit CopyablePtr(T&& t) = delete;
explicit CopyablePtr(T* t) : value(t) {}
/// copy is move
CopyablePtr(const CopyablePtr& other) : value(std::move(other.value)) {}
/// move is also move
CopyablePtr(CopyablePtr&& other) : value(std::move(other.value)) {}
const T& operator*() const {
return (*value);
}
T& operator*() {
return (*value);
}
T* get() {
return value.get();
}
const T* operator->() const {
return value.get();
}
T* operator->() {
return value.get();
}
/// move the value out (sugar for std::move(*CopyablePtr))
T&& move() {
return std::move(value);
}
explicit operator bool() const noexcept {
if (!value) return false;
return true;
}
// If you want these you're probably doing it wrong, though they'd be
// easy enough to implement
CopyablePtr& operator=(CopyablePtr const&) = delete;
CopyablePtr& operator=(CopyablePtr&& other) {
value.release();
value = std::move(other.value);
return *this;
}
private:
mutable std::unique_ptr<T> value;
};
/// Make a CopyablePtr from the argument. Because the name "makeCopyablePtr"
/// is already quite transparent in its intent, this will work for lvalues as
/// if you had wrapped them in std::move.
template <class T, class... _Types>
CopyablePtr<T> make_copyable(_Types&&... _Args) {
return CopyablePtr<T>(new T(std::forward<_Types>(_Args)...));
}
} // namespace folly
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/nicehero/nicenet.git
git@gitee.com:nicehero/nicenet.git
nicehero
nicenet
nicenet
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891