SharedPtr: adopt_shared_from_owned
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
831711ab7e
commit
df8f9c6cf6
@ -2,6 +2,8 @@
|
||||
#include <luna/Alloc.h>
|
||||
#include <luna/Result.h>
|
||||
|
||||
template <typename T> class SharedPtr;
|
||||
|
||||
template <typename T> class OwnedPtr
|
||||
{
|
||||
public:
|
||||
@ -38,6 +40,8 @@ template <typename T> class OwnedPtr
|
||||
return *m_ptr;
|
||||
}
|
||||
|
||||
template <typename Type> friend Result<SharedPtr<Type>> adopt_shared_from_owned(OwnedPtr<Type>&&);
|
||||
|
||||
private:
|
||||
T* m_ptr;
|
||||
};
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <luna/Alloc.h>
|
||||
#include <luna/Atomic.h>
|
||||
#include <luna/OwnedPtr.h>
|
||||
#include <luna/Result.h>
|
||||
#include <luna/ScopeGuard.h>
|
||||
|
||||
@ -119,3 +120,17 @@ template <typename T> Result<SharedPtr<T>> adopt_shared_if_nonnull(T* ptr)
|
||||
else
|
||||
return err(ENOMEM);
|
||||
}
|
||||
|
||||
template <typename T> Result<SharedPtr<T>> adopt_shared_from_owned(OwnedPtr<T>&& other)
|
||||
{
|
||||
T* ptr = other.m_ptr;
|
||||
other.m_ptr = nullptr;
|
||||
|
||||
auto guard = make_scope_guard([&] { delete ptr; });
|
||||
|
||||
SharedPtr<T> shared_ptr = TRY(adopt_shared(ptr));
|
||||
|
||||
guard.deactivate();
|
||||
|
||||
return shared_ptr;
|
||||
}
|
Loading…
Reference in New Issue
Block a user