diff --git a/src-test/server/TestNbt.cpp b/src-test/server/TestNbt.cpp index a423bc4a6d..73cd9d4fe3 100644 --- a/src-test/server/TestNbt.cpp +++ b/src-test/server/TestNbt.cpp @@ -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"; diff --git a/src/mc/nbt/CompoundTagVariant.h b/src/mc/nbt/CompoundTagVariant.h index cdaa4fa7ab..9044edf4a7 100644 --- a/src/mc/nbt/CompoundTagVariant.h +++ b/src/mc/nbt/CompoundTagVariant.h @@ -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 { return std::make_unique>(val); }, mTagStorage ); } - [[nodiscard]] operator UniqueTagPtr() const { return toUnique(); } [[nodiscard]] UniqueTagPtr toUnique() && { return std::visit( @@ -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()); } } } @@ -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 T> [[nodiscard]] inline T& UniqueTagPtr::get() const { if (hold()) { diff --git a/src/mc/nbt/Tag.h b/src/mc/nbt/Tag.h index 91a199e80f..64ea5fea7f 100644 --- a/src/mc/nbt/Tag.h +++ b/src/mc/nbt/Tag.h @@ -77,8 +77,6 @@ class Tag { [[nodiscard]] bool operator==(Tag const& other) const { return equals(other); } - [[nodiscard]] operator std::unique_ptr() const { return copy(); } - LLNDAPI std::string toSnbt(SnbtFormat snbtFormat = SnbtFormat::PrettyFilePrint, uchar indent = 4) const noexcept; public: diff --git a/src/mc/nbt/UniqueTagPtr.h b/src/mc/nbt/UniqueTagPtr.h index f30e36e0fd..65b6b15205 100644 --- a/src/mc/nbt/UniqueTagPtr.h +++ b/src/mc/nbt/UniqueTagPtr.h @@ -17,6 +17,7 @@ class StringTag; class ListTag; class CompoundTag; class IntArrayTag; +class CompoundTagVariant; class UniqueTagPtr { Tag* ptr{}; @@ -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&& 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&& ptr) noexcept : ptr(ptr.release()) {} - - LL_CONSTEXPR23 UniqueTagPtr& operator=(UniqueTagPtr&& r) noexcept { + LL_CONSTEXPR23 UniqueTagPtr& operator=(std::unique_ptr&& r) noexcept { reset(r.release()); return *this; } - LL_CONSTEXPR23 UniqueTagPtr& operator=(std::unique_ptr&& 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(); diff --git a/src/mc/nbt/detail/SnbtParseImpl.cpp b/src/mc/nbt/detail/SnbtParseImpl.cpp index 2feaf7d303..7aab33abd5 100644 --- a/src/mc/nbt/detail/SnbtParseImpl.cpp +++ b/src/mc/nbt/detail/SnbtParseImpl.cpp @@ -526,7 +526,7 @@ Expected 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());