-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Borrowing/Owning reference types
- Loading branch information
Showing
10 changed files
with
307 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/react-native-nitro-modules/cpp/utils/BorrowingReference.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// BorrowingReference.cpp | ||
// NitroModules | ||
// | ||
// Created by Marc Rousavy on 21.06.24. | ||
// | ||
|
||
#include "BorrowingReference.hpp" | ||
#include "OwningReference.hpp" | ||
|
||
namespace margelo { | ||
|
||
template<typename T> | ||
BorrowingReference<T>::BorrowingReference(const OwningReference<T>& ref) { | ||
_value = ref._value; | ||
_isDeleted = ref._isDeleted; | ||
_strongRefCount = ref._strongRefCount; | ||
_weakRefCount = ref._weakRefCount; | ||
(*_weakRefCount)++; | ||
} | ||
|
||
template<typename T> | ||
OwningReference<T> BorrowingReference<T>::lock() { | ||
if (*_isDeleted) { | ||
// return nullptr | ||
return OwningReference<T>(); | ||
} | ||
|
||
return OwningReference(*this); | ||
} | ||
|
||
template class BorrowingReference<int>; | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
packages/react-native-nitro-modules/cpp/utils/BorrowingReference.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// | ||
// BorrowingReference.hpp | ||
// NitroModules | ||
// | ||
// Created by Marc Rousavy on 21.06.24. | ||
// | ||
|
||
#pragma once | ||
|
||
namespace margelo { | ||
|
||
// forward-declaration to avoid duplicate symbols | ||
template<typename T> | ||
class OwningReference; | ||
|
||
template<typename T> | ||
class BorrowingReference { | ||
private: | ||
explicit BorrowingReference(const OwningReference<T>& ref); | ||
|
||
public: | ||
BorrowingReference(): _value(nullptr), _isDeleted(nullptr), _strongRefCount(nullptr), _weakRefCount(nullptr) { } | ||
|
||
BorrowingReference(const BorrowingReference& ref): | ||
_value(ref._value), | ||
_isDeleted(ref._isDeleted), | ||
_strongRefCount(ref._strongRefCount), | ||
_weakRefCount(ref._weakRefCount) { | ||
// increment ref count after copy | ||
(*_strongRefCount)++; | ||
} | ||
|
||
BorrowingReference(BorrowingReference&& ref): | ||
_value(ref._value), | ||
_isDeleted(ref._isDeleted), | ||
_strongRefCount(ref._strongRefCount), | ||
_weakRefCount(ref._weakRefCount) { | ||
ref._value = nullptr; | ||
ref._isDeleted = nullptr; | ||
ref._strongRefCount = nullptr; | ||
ref._weakRefCount = nullptr; | ||
} | ||
|
||
BorrowingReference& operator=(const BorrowingReference& ref) { | ||
if (this == &ref) return *this; | ||
|
||
if (_weakRefCount != nullptr) { | ||
// destroy previous pointer | ||
(*_weakRefCount)--; | ||
maybeDestroy(); | ||
} | ||
|
||
_value = ref._value; | ||
_isDeleted = ref._isDeleted; | ||
_strongRefCount = ref._strongRefCount; | ||
_weakRefCount = ref._weakRefCount; | ||
if (_weakRefCount != nullptr) { | ||
(*_weakRefCount)++; | ||
} | ||
} | ||
|
||
~BorrowingReference() { | ||
if (_weakRefCount == nullptr) { | ||
// we are just a dangling nullptr. | ||
return; | ||
} | ||
|
||
(*_weakRefCount)--; | ||
maybeDestroy(); | ||
} | ||
|
||
/** | ||
Try to lock the borrowing reference to an owning reference, or `nullptr` if it has already been deleted. | ||
*/ | ||
OwningReference<T> lock(); | ||
|
||
private: | ||
void maybeDestroy() { | ||
if (*_strongRefCount < 0 && *_weakRefCount < 0) { | ||
// free the full memory if there are no more references at all | ||
if (!(*_isDeleted)) { | ||
delete _value; | ||
} | ||
delete _isDeleted; | ||
delete _strongRefCount; | ||
delete _weakRefCount; | ||
} | ||
} | ||
|
||
private: | ||
T* _value; | ||
bool* _isDeleted; | ||
int* _strongRefCount; | ||
int* _weakRefCount; | ||
}; | ||
|
||
} // namespace margelo |
Oops, something went wrong.