Skip to content

Commit

Permalink
feat: add more put methods in CompoundTag
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jun 25, 2024
1 parent 2ac48b2 commit 63fb02a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
10 changes: 7 additions & 3 deletions include/bedrock/nbt/compound_tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ class CompoundTag : public Tag {
[[nodiscard]] std::unique_ptr<Tag> copy() const override;
[[nodiscard]] std::uint64_t hash() const override;

void deepCopy(CompoundTag const &other) const;
void deepCopy(const CompoundTag &other);
[[nodiscard]] bool contains(std::string_view key) const;
[[nodiscard]] bool contains(std::string_view key, Tag::Type type) const;
[[nodiscard]] const Tag *get(std::string_view key) const;
std::uint8_t &putByte(std::string name, std::uint8_t value);
Tag &put(std::string name, Tag &&tag);
Tag *put(std::string name, std::unique_ptr<Tag> tag);
void putBoolean(std::string name, bool value);
float &putFloat(std::string name, float value);
std::uint8_t &putByte(std::string name, std::uint8_t value);
std::int16_t &putShort(std::string name, std::int16_t value);
std::int32_t &putInt(std::string name, std::int32_t value);
std::int64_t &putInt64(std::string name, std::int64_t value);
float &putFloat(std::string name, float value);
double &putDouble(std::string name, double value);
std::string &putString(std::string name, std::string value);
CompoundTag &putCompound(std::string name, CompoundTag value);

Expand Down
1 change: 1 addition & 0 deletions include/bedrock/nbt/double_tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class DoubleTag : public Tag {
}

private:
friend class CompoundTag;
double data_;
};
BEDROCK_STATIC_ASSERT_SIZE(DoubleTag, 16, 16);
1 change: 1 addition & 0 deletions include/bedrock/nbt/short_tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ShortTag : public Tag {
}

private:
friend class CompoundTag;
std::int16_t data_;
};
BEDROCK_STATIC_ASSERT_SIZE(ShortTag, 16, 16);
47 changes: 37 additions & 10 deletions src/endstone_runtime/bedrock/nbt/compound_tag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ bool CompoundTag::equals(const Tag &other) const
std::unique_ptr<Tag> CompoundTag::copy() const
{
auto new_tag = std::make_unique<CompoundTag>();
deepCopy(*new_tag);
new_tag->deepCopy(*this);
return new_tag;
}

Expand All @@ -73,10 +73,12 @@ std::uint64_t CompoundTag::hash() const
return seed;
}

void CompoundTag::deepCopy(const CompoundTag &other) const
void CompoundTag::deepCopy(const CompoundTag &other)
{
// TODO(nbt): fixme
throw std::runtime_error("Not implemented");
tags_.clear();
for (const auto &[key, value] : other.tags_) {
tags_[key].emplace(std::move(*value.get()->copy()));
}
}

bool CompoundTag::contains(std::string_view key) const
Expand All @@ -101,21 +103,34 @@ const Tag *CompoundTag::get(std::string_view key) const
return nullptr;
}

std::uint8_t &CompoundTag::putByte(std::string name, std::uint8_t value)
Tag &CompoundTag::put(std::string name, Tag &&tag)
{
auto &tag = tags_[name].emplace(ByteTag(value));
return static_cast<ByteTag &>(tag).data_;
return tags_[name].emplace(std::forward<Tag>(tag));
}

Tag *CompoundTag::put(std::string name, std::unique_ptr<Tag> tag)
{
if (!tag) {
return nullptr;
}
return &tags_[name].emplace(std::move(*tag));
}

void CompoundTag::putBoolean(std::string name, bool value)
{
tags_[name].emplace(ByteTag(value ? 1 : 0));
}

float &CompoundTag::putFloat(std::string name, float value)
std::uint8_t &CompoundTag::putByte(std::string name, std::uint8_t value)
{
auto &tag = tags_[name].emplace(FloatTag(value));
return static_cast<FloatTag &>(tag).data_;
auto &tag = tags_[name].emplace(ByteTag(value));
return static_cast<ByteTag &>(tag).data_;
}

std::int16_t &CompoundTag::putShort(std::string name, std::int16_t value)
{
auto &tag = tags_[name].emplace(ShortTag(value));
return static_cast<ShortTag &>(tag).data_;
}

std::int32_t &CompoundTag::putInt(std::string name, std::int32_t value)
Expand All @@ -130,6 +145,18 @@ std::int64_t &CompoundTag::putInt64(std::string name, std::int64_t value)
return static_cast<Int64Tag &>(tag).data_;
}

float &CompoundTag::putFloat(std::string name, float value)
{
auto &tag = tags_[name].emplace(FloatTag(value));
return static_cast<FloatTag &>(tag).data_;
}

double &CompoundTag::putDouble(std::string name, double value)
{
auto &tag = tags_[name].emplace(DoubleTag(value));
return static_cast<DoubleTag &>(tag).data_;
}

std::string &CompoundTag::putString(std::string name, std::string value)
{
auto &tag = tags_[name].emplace(StringTag(std::move(value)));
Expand Down

0 comments on commit 63fb02a

Please sign in to comment.