diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2015-02-01 01:38:20 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2015-02-01 01:38:20 +0100 |
commit | a6a2a6281218688de7a74a9cd2a426aad3fe2da2 (patch) | |
tree | a8a40ab0978ebeff31321ae6d094a7ea605ee553 /src/NBT | |
parent | cbe64618821a0ae9f1901ce1a4a3893d939fdc32 (diff) | |
download | MinedMap-a6a2a6281218688de7a74a9cd2a426aad3fe2da2.tar MinedMap-a6a2a6281218688de7a74a9cd2a426aad3fe2da2.zip |
More convenient tag access
Diffstat (limited to 'src/NBT')
-rw-r--r-- | src/NBT/CompoundTag.hpp | 22 | ||||
-rw-r--r-- | src/NBT/ListTag.hpp | 2 | ||||
-rw-r--r-- | src/NBT/StringTag.hpp | 4 | ||||
-rw-r--r-- | src/NBT/Tag.cpp | 61 | ||||
-rw-r--r-- | src/NBT/Tag.hpp | 7 |
5 files changed, 88 insertions, 8 deletions
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<std::string, std::shared_ptr<Tag>> value; + std::unordered_map<std::string, std::shared_ptr<const Tag>> values; CompoundTag(Buffer *buffer) { while (true) { - std::pair<std::string, std::shared_ptr<Tag>> v = Tag::readNamedTag(buffer); + std::pair<std::string, std::shared_ptr<const Tag>> 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<std::string, std::shared_ptr<const Tag>> & getValues() const { + return values; + } + + const std::shared_ptr<const Tag> & get(const std::string &key) const { + return values.at(key); + } + + template<typename... Args> const std::shared_ptr<const Tag> & get(const std::string &key, const Args &...args) const { + std::shared_ptr<const CompoundTag> tag = std::dynamic_pointer_cast<const CompoundTag>(get(key)); + if (!tag) + return std::shared_ptr<Tag>(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<std::shared_ptr<Tag>> value; + std::vector<std::shared_ptr<const Tag>> 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> Tag::readTag(Type type, Buffer *buffer) { +std::shared_ptr<const Tag> Tag::readTag(Type type, Buffer *buffer) { switch (type) { case Type::End: return std::shared_ptr<EndTag>(new EndTag()); @@ -89,7 +89,7 @@ std::shared_ptr<Tag> Tag::readTag(Type type, Buffer *buffer) { } } -std::pair<std::string, std::shared_ptr<Tag>> Tag::readNamedTag(Buffer *buffer) { +std::pair<std::string, std::shared_ptr<const Tag>> Tag::readNamedTag(Buffer *buffer) { Type type = static_cast<Type>(buffer->get()); if (type == Type::End) return std::make_pair("", std::shared_ptr<EndTag>(new EndTag())); @@ -101,5 +101,62 @@ std::pair<std::string, std::shared_ptr<Tag>> 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 <cstdint> #include <memory> +#include <ostream> #include "../Buffer.hpp" @@ -52,14 +53,16 @@ public: IntArray = 11, }; - static std::shared_ptr<Tag> readTag(Type type, Buffer *buffer); + static std::shared_ptr<const Tag> readTag(Type type, Buffer *buffer); - static std::pair<std::string, std::shared_ptr<Tag>> readNamedTag(Buffer *buffer); + static std::pair<std::string, std::shared_ptr<const Tag>> readNamedTag(Buffer *buffer); virtual Type getType() const = 0; virtual ~Tag() {} }; +std::ostream& operator<<(std::ostream& os, Tag::Type type); + } } |