Skip to content

Commit

Permalink
refactor: correct implementation of OwnerStorageEntity and OwnerPtr
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jan 22, 2025
1 parent 721e51f commit a68fd25
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 31 deletions.
19 changes: 8 additions & 11 deletions src/bedrock/entity/gamerefs_entity/owner_storage_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,23 @@
#include "bedrock/entity/gamerefs_entity/entity_context.h"

class OwnerStorageEntity {
public:
[[nodiscard]] EntityContext &getStackRef() const
{
if (!context_.has_value()) {
throw std::bad_optional_access();
}
return const_cast<EntityContext &>(context_.value());
}
protected:
OwnerStorageEntity() = default;
OwnerStorageEntity(const OwnerStorageEntity &) = delete;
OwnerStorageEntity &operator=(const OwnerStorageEntity &) = delete;

[[nodiscard]] bool hasValue() const
[[nodiscard]] bool _hasValue() const
{
return context_.has_value();
}

[[nodiscard]] operator bool() const noexcept
[[nodiscard]] EntityContext &_getStackRef() const
{
return hasValue();
return const_cast<EntityContext &>(context_.value());
}

private:
friend class StackResultStorageEntity;
std::optional<EntityContext> context_;
};
BEDROCK_STATIC_ASSERT_SIZE(OwnerStorageEntity, 32, 32);
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@ StackResultStorageEntity::StackResultStorageEntity(WeakStorageEntity const &weak
}
}

StackResultStorageEntity::StackResultStorageEntity(EntityContext const &context)
StackResultStorageEntity::StackResultStorageEntity(EntityContext const &entity)
{
if (context.isValid()) {
context_.emplace(context);
if (entity.isValid()) {
context_.emplace(entity);
}
}

StackResultStorageEntity::StackResultStorageEntity(OwnerStorageEntity const &ref) : context_(ref.getStackRef()) {}
StackResultStorageEntity::StackResultStorageEntity(OwnerStorageEntity const &owner_storage)
{
if (owner_storage._hasValue()) {
context_.emplace(owner_storage.context_.value());
}
}

bool StackResultStorageEntity::_hasValue() const
{
Expand Down
12 changes: 7 additions & 5 deletions src/bedrock/gamerefs/gamerefs_shareptr/owner_storage_shareptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@

template <typename T>
class OwnerStorageSharePtr {
public:
T &operator*() const
protected:
OwnerStorageSharePtr() = default;

[[nodiscard]] bool _hasValue() const
{
return *value_;
return value_ != nullptr;
}

T *operator->() const noexcept
T &_getStackRef() const
{
return value_.get();
return *value_;
}

private:
Expand Down
37 changes: 34 additions & 3 deletions src/bedrock/gamerefs/owner_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,38 @@

#include "bedrock/gamerefs/gamerefs_shareptr/gamerefs_shareptr.h"

template <typename T>
class OwnerPtr : public GameRefs<T>::OwnerStorage {
using GameRefs<T>::OwnerStorage::OwnerStorage;
template <typename Type>
class OwnerPtr : public GameRefs<Type>::OwnerStorage {
public:
using StackRef = typename GameRefs<Type>::StackRef;
using GameRefs<Type>::OwnerStorage::OwnerStorage;

explicit operator bool() const
{
return hasValue();
}

[[nodiscard]] bool hasValue() const
{
return GameRefs<Type>::OwnerStorage::_hasValue();
}

StackRef &operator*() const
{
return value();
}

StackRef *operator->() const
{
return &value();
}

StackRef &value() const
{
return GameRefs<Type>::OwnerStorage::_getStackRef();
}

WeakRef<Type> getWeakRef() const;
bool operator==(nullptr_t) const;
bool operator!=(nullptr_t) const;
};
2 changes: 1 addition & 1 deletion src/bedrock/gamerefs/stack_ref_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class StackRefResult : public GameRefs<Type>::StackResultStorage {
return hasValue();
}

bool hasValue() const // NOLINT(*-use-nodiscard)
[[nodiscard]] bool hasValue() const
{
return GameRefs<Type>::StackResultStorage::_hasValue();
}
Expand Down
10 changes: 3 additions & 7 deletions src/endstone/core/level/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,17 @@ std::string EndstoneLevel::getName() const
std::vector<Actor *> EndstoneLevel::getActors() const
{
std::vector<Actor *> result;
for (const auto &e : level_.getEntities()) {
if (!e.hasValue()) {
for (const auto &entity : level_.getEntities()) {
if (!entity.hasValue()) {
continue;
}

// TODO(check): is this the correct usage of OwnerPtr<EntityContext> ?
const auto *actor = ::Actor::tryGetFromEntity(e.getStackRef(), false);
const auto *actor = ::Actor::tryGetFromEntity(*entity, false);
if (!actor) {
continue;
}

if (&actor->getLevel() != &level_) {
continue;
}

result.push_back(&actor->getEndstoneActor());
}
return result;
Expand Down

0 comments on commit a68fd25

Please sign in to comment.