Skip to content

Commit

Permalink
feat: add more nbt operator
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Jan 8, 2025
1 parent ffc40f9 commit e776a62
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src-test/server/TestNbt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ LL_AUTO_TYPE_INSTANCE_HOOK(NbtTest, HookPriority::Normal, ServerInstance, &Serve

nbt3["hello"]["world"] = ListTag{1.0, 2.0, 3.0};

nbt3["hello"]["world"][1] = 7.0_d;
nbt3["hello"]["world"][1] = 7.0;

nbt3["hello"][", "] = "world";

Expand Down
18 changes: 15 additions & 3 deletions src/mc/nbt/CompoundTagVariant.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,12 @@ class CompoundTagVariant {
return operator[](std::string_view{index});
}

[[nodiscard]] UniqueTagPtr toUnique() const& {
[[nodiscard]] UniqueTagPtr toUniqueCopy() const& {
return std::visit(
[](auto& val) -> std::unique_ptr<Tag> { return std::make_unique<std::decay_t<decltype(val)>>(val); },
mTagStorage
);
}
[[nodiscard]] operator UniqueTagPtr() const { return toUnique(); }

[[nodiscard]] UniqueTagPtr toUnique() && {
return std::visit(
Expand Down Expand Up @@ -342,7 +341,7 @@ class CompoundTagVariant {
mType = tags.begin()->index();
reserve(tags.size());
for (auto& tag : tags) {
emplace_back(tag.toUnique());
emplace_back(tag.toUniqueCopy());
}
}
}
Expand All @@ -353,6 +352,19 @@ class CompoundTagVariant {
return r ? (l.get() == *r) : false;
}

[[nodiscard]] inline UniqueTagPtr::UniqueTagPtr(CompoundTagVariant&& r) : ptr(std::move(r).toUnique().release()) {}

[[nodiscard]] inline UniqueTagPtr::UniqueTagPtr(CompoundTagVariant const& r) : ptr(r.toUniqueCopy().release()) {}

inline UniqueTagPtr& UniqueTagPtr::operator=(CompoundTagVariant&& r) {
reset(std::move(r).toUnique().release());
return *this;
}
inline UniqueTagPtr& UniqueTagPtr::operator=(CompoundTagVariant const& r) {
reset(r.toUniqueCopy().release());
return *this;
}

template <std::derived_from<Tag> T>
[[nodiscard]] inline T& UniqueTagPtr::get() const {
if (hold<T>()) {
Expand Down
2 changes: 0 additions & 2 deletions src/mc/nbt/Tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ class Tag {

[[nodiscard]] bool operator==(Tag const& other) const { return equals(other); }

[[nodiscard]] operator std::unique_ptr<Tag>() const { return copy(); }

LLNDAPI std::string toSnbt(SnbtFormat snbtFormat = SnbtFormat::PrettyFilePrint, uchar indent = 4) const noexcept;

public:
Expand Down
30 changes: 20 additions & 10 deletions src/mc/nbt/UniqueTagPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class StringTag;
class ListTag;
class CompoundTag;
class IntArrayTag;
class CompoundTagVariant;

class UniqueTagPtr {
Tag* ptr{};
Expand All @@ -37,29 +38,38 @@ class UniqueTagPtr {
IntArrayTag>;

public:
LL_CONSTEXPR23 UniqueTagPtr() noexcept {}
[[nodiscard]] LL_CONSTEXPR23 UniqueTagPtr() noexcept {}

LL_CONSTEXPR23 UniqueTagPtr(nullptr_t) noexcept {}
[[nodiscard]] LL_CONSTEXPR23 UniqueTagPtr(nullptr_t) noexcept {}

[[nodiscard]] LL_CONSTEXPR23 explicit UniqueTagPtr(Tag* p) noexcept : ptr(p) {}

[[nodiscard]] LL_CONSTEXPR23 UniqueTagPtr(std::unique_ptr<Tag>&& ptr) noexcept : ptr(ptr.release()) {}

[[nodiscard]] LL_CONSTEXPR23 UniqueTagPtr(UniqueTagPtr&& r) noexcept : ptr(r.release()) {}

[[nodiscard]] LL_CONSTEXPR23 UniqueTagPtr(UniqueTagPtr const& r) : ptr(r ? (r->copy().release()) : nullptr) {}

[[nodiscard]] UniqueTagPtr(CompoundTagVariant&& r);
[[nodiscard]] UniqueTagPtr(CompoundTagVariant const& r);

UniqueTagPtr& operator=(CompoundTagVariant&& r);
UniqueTagPtr& operator=(CompoundTagVariant const& r);

LL_CONSTEXPR23 UniqueTagPtr& operator=(nullptr_t) noexcept {
reset();
return *this;
}
LL_CONSTEXPR23 explicit UniqueTagPtr(Tag* p) noexcept : ptr(p) {}

LL_CONSTEXPR23 UniqueTagPtr(UniqueTagPtr&& r) noexcept : ptr(r.release()) {}

LL_CONSTEXPR23 UniqueTagPtr(std::unique_ptr<Tag>&& ptr) noexcept : ptr(ptr.release()) {}

LL_CONSTEXPR23 UniqueTagPtr& operator=(UniqueTagPtr&& r) noexcept {
LL_CONSTEXPR23 UniqueTagPtr& operator=(std::unique_ptr<Tag>&& r) noexcept {
reset(r.release());
return *this;
}
LL_CONSTEXPR23 UniqueTagPtr& operator=(std::unique_ptr<Tag>&& r) noexcept {

LL_CONSTEXPR23 UniqueTagPtr& operator=(UniqueTagPtr&& r) noexcept {
reset(r.release());
return *this;
}
LL_CONSTEXPR23 UniqueTagPtr(UniqueTagPtr const& r) : ptr(r ? (r->copy().release()) : nullptr) {}

LL_CONSTEXPR23 UniqueTagPtr& operator=(UniqueTagPtr const& r) {
if (r && &r != this) ptr = r->copy().release();
Expand Down
2 changes: 1 addition & 1 deletion src/mc/nbt/detail/SnbtParseImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ Expected<CompoundTagVariant> parseList(std::string_view& s) {
if (!value) {
return forwardError(value.error());
}
res.emplace_back(value->toUnique());
res.emplace_back(std::move(*value).toUnique());

if (auto skipped = skipWhitespace(s); !skipped) {
return forwardError(skipped.error());
Expand Down

0 comments on commit e776a62

Please sign in to comment.