From a6a2a6281218688de7a74a9cd2a426aad3fe2da2 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 1 Feb 2015 01:38:20 +0100 Subject: More convenient tag access --- src/NBT/CompoundTag.hpp | 22 +++++++++++++++--- src/NBT/ListTag.hpp | 2 +- src/NBT/StringTag.hpp | 4 ++++ src/NBT/Tag.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++-- src/NBT/Tag.hpp | 7 ++++-- 5 files changed, 88 insertions(+), 8 deletions(-) (limited to 'src/NBT') diff --git a/src/NBT/CompoundTag.hpp b/src/NBT/CompoundTag.hpp index 99bbd94..18de1c6 100644 --- a/src/NBT/CompoundTag.hpp +++ b/src/NBT/CompoundTag.hpp @@ -39,15 +39,15 @@ class CompoundTag : public Tag { private: friend class Tag; - std::unordered_map> value; + std::unordered_map> values; CompoundTag(Buffer *buffer) { while (true) { - std::pair> v = Tag::readNamedTag(buffer); + std::pair> v = Tag::readNamedTag(buffer); if (v.second->getType() == Type::End) break; - value.insert(std::move(v)); + values.insert(std::move(v)); } } @@ -55,6 +55,22 @@ public: virtual Type getType() const { return Type::Compound; } + + const std::unordered_map> & getValues() const { + return values; + } + + const std::shared_ptr & get(const std::string &key) const { + return values.at(key); + } + + template const std::shared_ptr & get(const std::string &key, const Args &...args) const { + std::shared_ptr tag = std::dynamic_pointer_cast(get(key)); + if (!tag) + return std::shared_ptr(nullptr); + + tag->get(args...); + } }; } diff --git a/src/NBT/ListTag.hpp b/src/NBT/ListTag.hpp index 6404065..67ba619 100644 --- a/src/NBT/ListTag.hpp +++ b/src/NBT/ListTag.hpp @@ -38,7 +38,7 @@ class ListTag : public Tag { private: friend class Tag; - std::vector> value; + std::vector> value; ListTag(Buffer *buffer) { diff --git a/src/NBT/StringTag.hpp b/src/NBT/StringTag.hpp index a120183..0144081 100644 --- a/src/NBT/StringTag.hpp +++ b/src/NBT/StringTag.hpp @@ -49,6 +49,10 @@ public: virtual Type getType() const { return Type::String; } + + const std::string & getValue() const { + return value; + } }; } diff --git a/src/NBT/Tag.cpp b/src/NBT/Tag.cpp index fddfc4e..a2d57e3 100644 --- a/src/NBT/Tag.cpp +++ b/src/NBT/Tag.cpp @@ -46,7 +46,7 @@ namespace MinedMap { namespace NBT { -std::shared_ptr Tag::readTag(Type type, Buffer *buffer) { +std::shared_ptr Tag::readTag(Type type, Buffer *buffer) { switch (type) { case Type::End: return std::shared_ptr(new EndTag()); @@ -89,7 +89,7 @@ std::shared_ptr Tag::readTag(Type type, Buffer *buffer) { } } -std::pair> Tag::readNamedTag(Buffer *buffer) { +std::pair> Tag::readNamedTag(Buffer *buffer) { Type type = static_cast(buffer->get()); if (type == Type::End) return std::make_pair("", std::shared_ptr(new EndTag())); @@ -101,5 +101,62 @@ std::pair> Tag::readNamedTag(Buffer *buffer) { return std::make_pair(name, readTag(type, buffer)); } +std::ostream& operator<<(std::ostream& os, Tag::Type type) { + switch (type) { + case Tag::Type::End: + os << "End"; + break; + + case Tag::Type::Byte: + os << "Byte"; + break; + + case Tag::Type::Short: + os << "Short"; + break; + + case Tag::Type::Int: + os << "Int"; + break; + + case Tag::Type::Long: + os << "Long"; + break; + + case Tag::Type::Float: + os << "Float"; + break; + + case Tag::Type::Double: + os << "Double"; + break; + + case Tag::Type::ByteArray: + os << "ByteArray"; + break; + + case Tag::Type::String: + os << "String"; + break; + + case Tag::Type::List: + os << "List"; + break; + + case Tag::Type::Compound: + os << "Compound"; + break; + + case Tag::Type::IntArray: + os << "IntArray"; + break; + + default: + os.setstate(std::ios_base::failbit); + } + + return os; +} + } } diff --git a/src/NBT/Tag.hpp b/src/NBT/Tag.hpp index 0d25299..077c508 100644 --- a/src/NBT/Tag.hpp +++ b/src/NBT/Tag.hpp @@ -28,6 +28,7 @@ #include #include +#include #include "../Buffer.hpp" @@ -52,14 +53,16 @@ public: IntArray = 11, }; - static std::shared_ptr readTag(Type type, Buffer *buffer); + static std::shared_ptr readTag(Type type, Buffer *buffer); - static std::pair> readNamedTag(Buffer *buffer); + static std::pair> readNamedTag(Buffer *buffer); virtual Type getType() const = 0; virtual ~Tag() {} }; +std::ostream& operator<<(std::ostream& os, Tag::Type type); + } } -- cgit v1.2.3