From b42899cde7571e94b71a8ca5312768a12e169dac Mon Sep 17 00:00:00 2001 From: zontreck Date: Wed, 31 Jul 2024 00:34:47 -0700 Subject: [PATCH] Adds casting helpers --- src/nbt/Tag.cpp | 186 ++++++++++++++++++++++++++++++++++++++++++++++++ src/nbt/Tag.h | 36 ++++++++-- 2 files changed, 216 insertions(+), 6 deletions(-) diff --git a/src/nbt/Tag.cpp b/src/nbt/Tag.cpp index b6a99a8..73532dd 100644 --- a/src/nbt/Tag.cpp +++ b/src/nbt/Tag.cpp @@ -283,4 +283,190 @@ namespace nbt return ret; } + ByteTag Tag::asByte() const + { + // Use dynamic_cast to safely cast to Tag + const ByteTag *TagPtr = dynamic_cast(this); + + if (TagPtr != nullptr) + { + // If the cast was successful, return the Tag object + return *TagPtr; + } + else + { + // Handle the case where the cast fails; return a default Tag + return ByteTag(0); // You should handle this appropriately based on your design + } + } + + ByteArrayTag Tag::asByteArray() const + { + + // Use dynamic_cast to safely cast to Tag + const ByteArrayTag *TagPtr = dynamic_cast(this); + + if (TagPtr != nullptr) + { + // If the cast was successful, return the Tag object + return *TagPtr; + } + else + { + // Handle the case where the cast fails; return a default Tag + return ByteArrayTag(); // You should handle this appropriately based on your design + } + } + + CompoundTag Tag::asCompound() const + { + + // Use dynamic_cast to safely cast to Tag + const CompoundTag *TagPtr = dynamic_cast(this); + + if (TagPtr != nullptr) + { + // If the cast was successful, return the Tag object + return *TagPtr; + } + else + { + // Handle the case where the cast fails; return a default Tag + return CompoundTag(); // You should handle this appropriately based on your design + } + } + + DoubleTag Tag::asDouble() const + { + // Use dynamic_cast to safely cast to Tag + const DoubleTag *TagPtr = dynamic_cast(this); + + if (TagPtr != nullptr) + { + // If the cast was successful, return the Tag object + return *TagPtr; + } + else + { + // Handle the case where the cast fails; return a default Tag + return DoubleTag(); // You should handle this appropriately based on your design + } + } + FloatTag Tag::asFloat() const + { + + // Use dynamic_cast to safely cast to Tag + const FloatTag *TagPtr = dynamic_cast(this); + + if (TagPtr != nullptr) + { + // If the cast was successful, return the Tag object + return *TagPtr; + } + else + { + // Handle the case where the cast fails; return a default Tag + return FloatTag(); // You should handle this appropriately based on your design + } + } + IntArrayTag Tag::asIntArray() const + { + + // Use dynamic_cast to safely cast to Tag + const IntArrayTag *TagPtr = dynamic_cast(this); + + if (TagPtr != nullptr) + { + // If the cast was successful, return the Tag object + return *TagPtr; + } + else + { + // Handle the case where the cast fails; return a default Tag + return IntArrayTag(); // You should handle this appropriately based on your design + } + } + IntTag Tag::asInt() const + { + // Use dynamic_cast to safely cast to Tag + const IntTag *TagPtr = dynamic_cast(this); + + if (TagPtr != nullptr) + { + // If the cast was successful, return the Tag object + return *TagPtr; + } + else + { + // Handle the case where the cast fails; return a default Tag + return IntTag(); // You should handle this appropriately based on your design + } + } + LongArrayTag Tag::asLongArray() const { + + // Use dynamic_cast to safely cast to Tag + const LongArrayTag *TagPtr = dynamic_cast(this); + + if (TagPtr != nullptr) + { + // If the cast was successful, return the Tag object + return *TagPtr; + } + else + { + // Handle the case where the cast fails; return a default Tag + return LongArrayTag(); // You should handle this appropriately based on your design + } + } + LongTag Tag::asLong() const { + + // Use dynamic_cast to safely cast to Tag + const LongTag *TagPtr = dynamic_cast(this); + + if (TagPtr != nullptr) + { + // If the cast was successful, return the Tag object + return *TagPtr; + } + else + { + // Handle the case where the cast fails; return a default Tag + return LongTag(); // You should handle this appropriately based on your design + } + } + ShortTag Tag::asShort() const { + + // Use dynamic_cast to safely cast to Tag + const ShortTag *TagPtr = dynamic_cast(this); + + if (TagPtr != nullptr) + { + // If the cast was successful, return the Tag object + return *TagPtr; + } + else + { + // Handle the case where the cast fails; return a default Tag + return ShortTag(); // You should handle this appropriately based on your design + } + } + + ListTag Tag::asList() const { + + // Use dynamic_cast to safely cast to Tag + const ListTag *TagPtr = dynamic_cast(this); + + if (TagPtr != nullptr) + { + // If the cast was successful, return the Tag object + return *TagPtr; + } + else + { + // Handle the case where the cast fails; return a default Tag + return ListTag(); // You should handle this appropriately based on your design + } + + } + } \ No newline at end of file diff --git a/src/nbt/Tag.h b/src/nbt/Tag.h index 79bacf5..623ba04 100644 --- a/src/nbt/Tag.h +++ b/src/nbt/Tag.h @@ -5,6 +5,18 @@ #include #include #include +#include "EndTag.h" +#include "ByteTag.h" +#include "ByteArrayTag.h" +#include "DoubleTag.h" +#include "FloatTag.h" +#include "CompoundTag.h" +#include "IntArrayTag.h" +#include "IntTag.h" +#include "ListTag.h" +#include "LongArrayTag.h" +#include "LongTag.h" +#include "ShortTag.h" #include "../utils/ByteLayer.h" #include "../utils/StringBuilder.h" #include "../utils/StringReader.h" @@ -52,7 +64,7 @@ namespace nbt static TagType getStringifiedTagType(StringReader &str); - void updateParent(Tag* tag) + void updateParent(Tag *tag) { if (tag == nullptr) { @@ -66,7 +78,7 @@ namespace nbt } } - Tag* getParent() const + Tag *getParent() const { return _parentTag; } @@ -106,19 +118,31 @@ namespace nbt return getType() == other.getType() && getKey() == other.getKey(); } - static Tag* readNamedTag(ByteLayer &data); + static Tag *readNamedTag(ByteLayer &data); static void writeNamedTag(Tag &tag, ByteLayer &data); - static Tag* readStringifiedNamedTag(StringReader &string); + static Tag *readStringifiedNamedTag(StringReader &string); static void writeStringifiedNamedTag(Tag &tag, StringBuilder &builder, int indents); - static Tag* makeTagOfType(TagType type); + static Tag *makeTagOfType(TagType type); static std::string getCanonicalName(TagType type); bool shouldQuoteName(); static bool shouldQuote(const string &value); + ByteTag asByte() const; + ByteArrayTag asByteArray() const; + CompoundTag asCompound() const; + DoubleTag asDouble() const; + FloatTag asFloat() const; + IntArrayTag asIntArray() const; + IntTag asInt() const; + LongArrayTag asLongArray() const; + LongTag asLong() const; + ShortTag asShort() const; + ListTag asList() const; + protected: - Tag* _parentTag; + Tag *_parentTag; TagType _parentTagType = TagType::End; std::string _key; };