Skip to content

Commit

Permalink
refactor: refactoring SharedPtr
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Jan 18, 2024
1 parent 3175162 commit 93c543b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
22 changes: 11 additions & 11 deletions src/mc/common/wrapper/SharedPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ template <typename T>
class SharedPtr {
public:
template <typename... Args>
static SharedPtr<T> makeShared(Args&&... args) {
[[nodiscard]] static SharedPtr<T> make(Args&&... args) {
return SharedPtr<T>(new T(std::forward<Args>(args)...));
}

SharedPtr() noexcept : counter(nullptr) {} // NOLINT
SharedPtr(std::nullptr_t) noexcept : counter(nullptr) {} // NOLINT
[[nodiscard]] SharedPtr() noexcept : counter(nullptr) {} // NOLINT
[[nodiscard]] SharedPtr(std::nullptr_t) noexcept : counter(nullptr) {} // NOLINT

explicit SharedPtr(T* p) : counter(new SharedCounter<T>(p)) {}
[[nodiscard]] explicit SharedPtr(T* p) : counter(new SharedCounter<T>(p)) {}

template <class Y>
explicit SharedPtr(SharedPtr<Y> const& other)
[[nodiscard]] explicit SharedPtr(SharedPtr<Y> const& other)
requires(std::convertible_to<Y*, T*>)
{
counter = (SharedCounter<T>*)other.counter;
Expand All @@ -30,15 +30,15 @@ class SharedPtr {
}

template <class Y>
explicit SharedPtr(SharedPtr<Y>&& other)
[[nodiscard]] explicit SharedPtr(SharedPtr<Y>&& other)
requires(std::convertible_to<Y*, T*>)
{
counter = (SharedCounter<T>*)other.counter;
other.counter = nullptr;
}

template <class Y>
explicit SharedPtr(WeakPtr<Y> const& other)
[[nodiscard]] explicit SharedPtr(WeakPtr<Y> const& other)
requires(std::convertible_to<Y*, T*>)
{
counter = (SharedCounter<T>*)other.counter;
Expand Down Expand Up @@ -88,13 +88,13 @@ class SharedPtr {
return *this;
}

T* get() const { return counter ? counter->get() : nullptr; }
[[nodiscard]] T* get() const { return counter ? counter->get() : nullptr; }

T* operator->() const { return get(); }
[[nodiscard]] T* operator->() const { return get(); }

T& operator*() const { return *get(); }
[[nodiscard]] T& operator*() const { return *get(); }

explicit operator bool() const { return get() != nullptr; }
[[nodiscard]] explicit operator bool() const { return get() != nullptr; }

[[nodiscard]] int use_count() const { return counter ? counter->getShareCount() : 0; }

Expand Down
20 changes: 10 additions & 10 deletions src/mc/common/wrapper/WeakPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class SharedPtr;
template <typename T>
class WeakPtr {
public:
WeakPtr() noexcept : counter(nullptr) {} // NOLINT
WeakPtr(std::nullptr_t) noexcept : counter(nullptr) {} // NOLINT
[[nodiscard]] WeakPtr() noexcept : counter(nullptr) {} // NOLINT
[[nodiscard]] WeakPtr(std::nullptr_t) noexcept : counter(nullptr) {} // NOLINT

template <class Y>
explicit WeakPtr(SharedPtr<Y> const& other)
[[nodiscard]] explicit WeakPtr(SharedPtr<Y> const& other)
requires(std::convertible_to<Y*, T*>)
{
counter = (SharedCounter<T>*)other.counter;
Expand All @@ -23,7 +23,7 @@ class WeakPtr {
}

template <class Y>
explicit WeakPtr(WeakPtr<Y> const& other)
[[nodiscard]] explicit WeakPtr(WeakPtr<Y> const& other)
requires(std::convertible_to<Y*, T*>)
{
counter = (SharedCounter<T>*)other.counter;
Expand All @@ -33,7 +33,7 @@ class WeakPtr {
}

template <class Y>
explicit WeakPtr(WeakPtr<Y>&& other)
[[nodiscard]] explicit WeakPtr(WeakPtr<Y>&& other)
requires(std::convertible_to<Y*, T*>)
{
counter = (SharedCounter<T>*)other.counter;
Expand Down Expand Up @@ -87,15 +87,15 @@ class WeakPtr {

[[nodiscard]] bool expired() const { return use_count() == 0; }

SharedPtr<T> lock() const { return expired() ? SharedPtr<T>() : SharedPtr<T>(*this); }
[[nodiscard]] SharedPtr<T> lock() const { return expired() ? SharedPtr<T>() : SharedPtr<T>(*this); }

T* get() const { return counter ? counter->get() : nullptr; }
[[nodiscard]] T* get() const { return counter ? counter->get() : nullptr; }

T* operator->() const { return get(); }
[[nodiscard]] T* operator->() const { return get(); }

T& operator*() const { return *get(); }
[[nodiscard]] T& operator*() const { return *get(); }

explicit operator bool() const { return get() != nullptr; }
[[nodiscard]] explicit operator bool() const { return get() != nullptr; }

SharedCounter<T>* counter;
};
2 changes: 1 addition & 1 deletion src/mc/world/level/block/registry/BlockTypeRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class BlockTypeRegistry {
// NOLINTBEGIN
// symbol:
// ?_lookupByNameImpl@BlockTypeRegistry@@CA?AULookupByNameImplReturnType@1@AEBVHashedString@@HW4LookupByNameImplResolve@1@_N@Z
MCAPI static struct BlockTypeRegistry::LookupByNameImplReturnType _lookupByNameImpl(
MCAPI static struct ::BlockTypeRegistry::LookupByNameImplReturnType _lookupByNameImpl(
class HashedString const& name,
int data,
::BlockTypeRegistry::LookupByNameImplResolve resolve,
Expand Down

0 comments on commit 93c543b

Please sign in to comment.