Skip to content

Commit

Permalink
Add retain/release functionality to shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl committed Mar 17, 2024
1 parent 8b2a68b commit 1417e7a
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions source/numem/mem/ptr.d
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private {
/**
Allocates a new shared pointer.
*/
@trusted
shared_ptr!T shared_new(T, Args...)(Args args) nothrow @nogc {
static if (is(T == class)) {
T item = nogc_new!T(args);
Expand All @@ -87,6 +88,7 @@ shared_ptr!T shared_new(T, Args...)(Args args) nothrow @nogc {
/**
Allocates a new unique pointer.
*/
@trusted
unique_ptr!T unique_new(T, Args...)(Args args) nothrow @nogc {
static if (is(T == class)) {
T item = nogc_new!T(args);
Expand Down Expand Up @@ -141,6 +143,7 @@ public:
Creates a unique_ptr reference from a existing reference
*/
@system
static unique_ptr!T fromPtr(T* ptr) @system {
return unique_ptr!T(ptr);
}
Expand All @@ -151,6 +154,7 @@ public:
Creates a unique_ptr reference from a existing reference
*/
@system
static unique_ptr!T fromPtr(T ptr) @system {
return unique_ptr!T(cast(T*)ptr);
}
Expand All @@ -162,6 +166,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T getAtomic() {
return rc ? cast(T)atomicLoad(rc.ref_) : null;
}
Expand All @@ -171,6 +176,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T get() {
return rc ? cast(T)rc.ref_ : null;
}
Expand All @@ -180,6 +186,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T opCast() {
return rc ? cast(T)rc.ref_ : null;
}
Expand All @@ -189,6 +196,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T* getAtomic() {
return rc ? atomicLoad(rc.ref_) : null;
}
Expand All @@ -198,6 +206,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T* get() {
return rc ? rc.ref_ : null;
}
Expand All @@ -207,6 +216,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T* opCast() {
return rc ? rc.ref_ : null;
}
Expand All @@ -218,13 +228,15 @@ public:
weak refs will return null if the ref count reaches 0 of their parent shared pointer.
*/
@trusted
weak_ptr!T borrow() {
return weak_ptr!T(rc);
}

/**
Moves the unique pointer to the specified other pointer
*/
@trusted
void moveTo(ref unique_ptr!T other) {

// First destruct the target unique_ptr if neccessary.
Expand All @@ -239,12 +251,35 @@ public:
/**
Resets the unique_ptr, emptying its contents.
*/
@trusted
void reset() {
if (rc) {
rc.free();
rc = null;
}
}

/**
Gets the amount of strong references to the object
*/
@trusted
size_t getRefCount() {
if (rc) {
return atomicLoad(rc.strongRefs);
}
return 0;
}

/**
Gets the amount of weak references to the object
*/
@trusted
size_t getBorrowCount() {
if (rc) {
return atomicLoad(rc.weakRefs);
}
return 0;
}
}

/**
Expand Down Expand Up @@ -286,6 +321,7 @@ public:
Creates a shared_ptr reference from a existing reference
*/
@system
static shared_ptr!T fromPtr(T* ptr) @system {
return shared_ptr!T(ptr);
}
Expand All @@ -296,6 +332,7 @@ public:
Creates a shared_ptr reference from a existing reference
*/
@system
static shared_ptr!T fromPtr(T ptr) @system {
return shared_ptr!T(cast(T*)ptr);
}
Expand All @@ -307,6 +344,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T getAtomic() {
return rc ? cast(T)atomicLoad(rc.ref_) : null;
}
Expand All @@ -316,6 +354,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T get() {
return rc ? cast(T)rc.ref_ : null;
}
Expand All @@ -325,6 +364,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T opCast() {
return rc ? cast(T)rc.ref_ : null;
}
Expand All @@ -334,6 +374,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T* getAtomic() {
return rc ? atomicLoad(rc.ref_) : null;
}
Expand All @@ -343,6 +384,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T* get() {
return rc ? rc.ref_ : null;
}
Expand All @@ -352,6 +394,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T* opCast() {
return rc ? rc.ref_ : null;
}
Expand All @@ -363,26 +406,71 @@ public:
weak refs will return null if the ref count reaches 0 of their parent shared pointer.
*/
@trusted
weak_ptr!T borrow() {
return weak_ptr!T(rc);
}

/**
Makes a strong copy of the shared pointer.
*/
@trusted
shared_ptr!T copy() {
return shared_ptr!T(this);
}

/**
Resets the shared_ptr, emptying its contents.
*/
@trusted
void reset() {
if (rc) {
rc.subRef!false;
rc = null;
}
}

/**
Releases a reference
*/
@system
void release() {
if (rc) {
rc.subRef!false();
}
}

/**
Adds a reference
*/
@system
void retain() {
if (rc) {
rc.addRef!false();
}
}

/**
Gets the amount of strong references to the object
*/
@trusted
size_t getRefCount() {
if (rc) {
return atomicLoad(rc.strongRefs);
}
return 0;
}

/**
Gets the amount of weak references to the object
*/
@trusted
size_t getBorrowCount() {
if (rc) {
return atomicLoad(rc.weakRefs);
}
return 0;
}
}

/**
Expand Down Expand Up @@ -413,6 +501,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T getAtomic() {
return rc ? cast(T)atomicLoad(rc.ref_) : null;
}
Expand All @@ -422,6 +511,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T get() {
return rc ? cast(T)rc.ref_ : null;
}
Expand All @@ -431,6 +521,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T opCast() {
return rc ? cast(T)rc.ref_ : null;
}
Expand All @@ -440,6 +531,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T* getAtomic() {
return rc ? atomicLoad(rc.ref_) : null;
}
Expand All @@ -449,6 +541,7 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T* get() {
return rc ? rc.ref_ : null;
}
Expand All @@ -458,10 +551,33 @@ public:
Returns null if the item is no longer valid.
*/
@trusted
T* opCast() {
return rc ? rc.ref_ : null;
}
}

/**
Gets the amount of strong references to the object
*/
@trusted
size_t getRefCount() {
if (rc) {
return atomicLoad(rc.strongRefs);
}
return 0;
}

/**
Gets the amount of weak references to the object
*/
@trusted
size_t getBorrowCount() {
if (rc) {
return atomicLoad(rc.weakRefs);
}
return 0;
}
}


Expand Down

0 comments on commit 1417e7a

Please sign in to comment.