Adds casting helpers

This commit is contained in:
zontreck 2024-07-31 00:34:47 -07:00
parent 422f428fce
commit b42899cde7
2 changed files with 216 additions and 6 deletions

View file

@ -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<const ByteTag *>(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<const ByteArrayTag *>(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<const CompoundTag *>(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<const DoubleTag *>(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<const FloatTag *>(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<const IntArrayTag *>(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<const IntTag *>(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<const LongArrayTag *>(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<const LongTag *>(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<const ShortTag *>(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<const ListTag *>(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
}
}
}

View file

@ -5,6 +5,18 @@
#include <vector>
#include <memory>
#include <iostream>
#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;
};