Improve core NBT types

This commit is contained in:
DankParrot 2020-08-02 23:08:29 -07:00
parent 5f1af9226a
commit 7a03afcad9
2 changed files with 28 additions and 1 deletions

View File

@ -82,6 +82,10 @@ namespace NBT
case TAG_LONG_ARRAY:
return m_node->payload.tag_long_array.length;
case TAG_STRING:
// cNBT does not store string length so compute here
return strlen(m_node->payload.tag_string);
case TAG_LIST:
return list_length(&(m_node->payload.tag_list->entry));
case TAG_COMPOUND:

View File

@ -11,6 +11,7 @@ struct list_head;
namespace NBT
{
// Represents a generic NBT tag of any type
class Tag
{
protected:
@ -21,10 +22,12 @@ namespace NBT
static Tag CreateListTag(nbt_node* node);
public:
// This will be "<null>" if the tag is nameless
const char* GetName() const;
// Get the number of iterable immediate children
size_t GetLength() const;
const char* ToString() const;
};
@ -42,6 +45,16 @@ namespace NBT
inline operator T() const { return GetValue(); }
};
// Standard value types
using ByteTag = DataTag<int8_t>;
using ShortTag = DataTag<int16_t>;
using IntTag = DataTag<int32_t>;
using LongTag = DataTag<int64_t>;
using FloatTag = DataTag<float>;
using DoubleTag = DataTag<double>;
// Represents a generic array, used for StringTag,
// ByteArrayTag, IntArrayTag, and LongArrayTag
template <typename T>
class ArrayTag : public DataTag<T*>
{
@ -50,12 +63,22 @@ namespace NBT
const T* GetValue() const;
// Methods for range-based for loops
// TODO: check if this works for IntArrayTag and LongArrayTag
const T* begin() { return GetValue(); }
const T* end() { return &(GetValue()[this->GetLength()]); }
const T* end() { return GetValue() + Tag::GetLength(); }
};
// A string is an array of char. Its length is
// not stored by cNBT, so Tag::GetLength uses strlen
using StringTag = ArrayTag<char>;
// Standard array types
using ByteArrayTag = ArrayTag<int8_t>;
using IntArrayTag = ArrayTag<int32_t>;
using LongArrayTag = ArrayTag<int64_t>;
// Represents a list of nameless tags of the same type
template <typename T>
class ListTag : public Tag
{