Handle unique_ptr

Also just use plain using statements, derp
This commit is contained in:
Steven Dee (Jōshin) 2024-08-28 11:55:33 -07:00
parent f6fc17d2e5
commit 10f1d06415
No known key found for this signature in database
2 changed files with 18 additions and 25 deletions

View file

@ -102,8 +102,12 @@ class shared_pointer : public shared_ref
public: public:
static shared_pointer* make(T* const p, D d) static shared_pointer* make(T* const p, D d)
{ {
auto p2 = unique_ptr<T, D>(p, move(d)); return make(unique_ptr<T, D>(p, move(d)));
return new shared_pointer(p2.release(), move(p2.get_deleter())); }
static shared_pointer* make(unique_ptr<T, D> p)
{
return new shared_pointer(p.release(), move(p.get_deleter()));
} }
private: private:
@ -235,15 +239,12 @@ class shared_ptr
rc->keep_shared(); rc->keep_shared();
} }
// TODO(mrdomino): blocked on ctl::ref
#if 0
template<typename U, typename D> template<typename U, typename D>
requires is_convertible_v<U, T> requires is_convertible_v<U, T>
shared_ptr(unique_ptr<U, D>&& r) shared_ptr(unique_ptr<U, D>&& r)
: p(r.p), rc(__::shared_pointer<U, D>::make(r.release(), r.get_deleter())) : p(r.p), rc(__::shared_pointer<U, D>::make(move(r)))
{ {
} }
#endif
~shared_ptr() ~shared_ptr()
{ {

View file

@ -21,22 +21,16 @@
#include "libc/mem/leaks.h" #include "libc/mem/leaks.h"
// #include <memory> // #include <memory>
// #include <vector>
// #define ctl std // #define ctl std
template<typename T> using ctl::bad_weak_ptr;
using shared_ptr = ctl::shared_ptr<T>; using ctl::make_shared;
using ctl::move;
template<typename T> using ctl::shared_ptr;
using weak_ptr = ctl::weak_ptr<T>; using ctl::unique_ptr;
using ctl::vector;
template<typename T, typename... Args> using ctl::weak_ptr;
shared_ptr<T>
make_shared(Args&&... args)
{
return ctl::make_shared<T, Args...>(ctl::forward<Args>(args)...);
}
using bad_weak_ptr = ctl::bad_weak_ptr;
#undef ctl #undef ctl
@ -149,7 +143,7 @@ main()
{ {
// You can take a shared pointer to a subobject, and it will free the // You can take a shared pointer to a subobject, and it will free the
// base object. // base object.
shared_ptr<ctl::vector<int>> x(new ctl::vector<int>); shared_ptr<vector<int>> x(new vector<int>);
x->push_back(5); x->push_back(5);
shared_ptr<int> y(x, &x->at(0)); shared_ptr<int> y(x, &x->at(0));
x.reset(); x.reset();
@ -157,19 +151,17 @@ main()
return 9; return 9;
} }
#if 0
{ {
g = 0; g = 0;
// You can create a shared_ptr from a unique_ptr. // You can create a shared_ptr from a unique_ptr.
ctl::unique_ptr x(&a, CallG()); unique_ptr<int, CallG> x(&a, CallG());
shared_ptr<int> y(ctl::move(x)); shared_ptr<int> y(move(x));
if (x) if (x)
return 10; return 10;
y.reset(); y.reset();
if (g != 1) if (g != 1)
return 11; return 11;
} }
#endif
{ {
g = 0; g = 0;