Skip to content

Commit

Permalink
Make base::weak_ptr(nullptr_t) simpler.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Apr 4, 2024
1 parent 8d4b2ef commit beef65f
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions base/weak_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class has_weak_ptr;
namespace details {

struct alive_tracker {
explicit alive_tracker(const has_weak_ptr *value) : value(value) {
explicit alive_tracker(const has_weak_ptr *value) noexcept
: value(value) {
}

std::atomic<int> counter = 1;
Expand Down Expand Up @@ -64,15 +65,15 @@ class has_weak_ptr {
}
}

friend inline void invalidate_weak_ptrs(has_weak_ptr *object) {
friend inline void invalidate_weak_ptrs(has_weak_ptr *object) noexcept {
if (auto alive = object ? object->_alive.load() : nullptr) {
if (object->_alive.compare_exchange_strong(alive, nullptr)) {
alive->value.store(nullptr);
details::decrement(alive);
}
}
}
friend inline int weak_ptrs_count(has_weak_ptr *object) {
friend inline int weak_ptrs_count(has_weak_ptr *object) noexcept {
if (const auto alive = object ? object->_alive.load() : nullptr) {
return alive->counter.load();
}
Expand All @@ -83,7 +84,7 @@ class has_weak_ptr {
template <typename Child>
friend class weak_ptr;

details::alive_tracker *incrementAliveTracker() const {
details::alive_tracker *incrementAliveTracker() const noexcept {
auto current = _alive.load();
if (!current) {
auto alive = std::make_unique<details::alive_tracker>(this);
Expand All @@ -103,19 +104,21 @@ template <typename T>
class weak_ptr {
public:
weak_ptr() = default;
weak_ptr(T *value)
weak_ptr(std::nullptr_t) noexcept {
}
weak_ptr(T *value) noexcept
: _alive(value ? value->incrementAliveTracker() : nullptr) {
}
weak_ptr(gsl::not_null<T*> value)
weak_ptr(gsl::not_null<T*> value) noexcept
: _alive(value->incrementAliveTracker()) {
}
weak_ptr(const std::unique_ptr<T> &value)
weak_ptr(const std::unique_ptr<T> &value) noexcept
: weak_ptr(value.get()) {
}
weak_ptr(const std::shared_ptr<T> &value)
weak_ptr(const std::shared_ptr<T> &value) noexcept
: weak_ptr(value.get()) {
}
weak_ptr(const std::weak_ptr<T> &value)
weak_ptr(const std::weak_ptr<T> &value) noexcept
: weak_ptr(value.lock().get()) {
}
weak_ptr(const weak_ptr &other) noexcept
Expand All @@ -139,23 +142,23 @@ class weak_ptr {
: _alive(std::exchange(other._alive, nullptr)) {
}

weak_ptr &operator=(T *value) {
weak_ptr &operator=(T *value) noexcept {
reset(value);
return *this;
}
weak_ptr &operator=(gsl::not_null<T*> value) {
weak_ptr &operator=(gsl::not_null<T*> value) noexcept {
reset(value.get());
return *this;
}
weak_ptr &operator=(const std::unique_ptr<T> &value) {
weak_ptr &operator=(const std::unique_ptr<T> &value) noexcept {
reset(value.get());
return *this;
}
weak_ptr &operator=(const std::shared_ptr<T> &value) {
weak_ptr &operator=(const std::shared_ptr<T> &value) noexcept {
reset(value.get());
return *this;
}
weak_ptr &operator=(const std::weak_ptr<T> &value) {
weak_ptr &operator=(const std::weak_ptr<T> &value) noexcept {
reset(value.lock().get());
return *this;
}
Expand Down Expand Up @@ -200,10 +203,10 @@ class weak_ptr {
destroy();
}

[[nodiscard]] bool null() const {
[[nodiscard]] bool null() const noexcept {
return !_alive;
}
[[nodiscard]] bool empty() const {
[[nodiscard]] bool empty() const noexcept {
return !_alive || !_alive->value;
}
[[nodiscard]] T *get() const noexcept {
Expand All @@ -228,7 +231,7 @@ class weak_ptr {
weak_ptr,
weak_ptr) noexcept = default;

void reset(T *value = nullptr) {
void reset(T *value = nullptr) noexcept {
if ((!value && _alive) || (get() != value)) {
destroy();
_alive = value ? value->incrementAliveTracker() : nullptr;
Expand Down

0 comments on commit beef65f

Please sign in to comment.