From bd8ac94ccb071ffb4cc16b3fb514e789ca1f866d Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Sun, 12 Jul 2015 12:29:25 +0200 Subject: Create tag_list --- include/libnbt.h | 1 + include/tag_list.h | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 include/tag_list.h (limited to 'include') diff --git a/include/libnbt.h b/include/libnbt.h index 9d873254f6..965fa309ca 100644 --- a/include/libnbt.h +++ b/include/libnbt.h @@ -23,5 +23,6 @@ #include "tag_primitive.h" #include "tag_string.h" #include "tag_compound.h" +#include "tag_list.h" #endif // LIBNBT_H_INCLUDED diff --git a/include/tag_list.h b/include/tag_list.h new file mode 100644 index 0000000000..afc04c0616 --- /dev/null +++ b/include/tag_list.h @@ -0,0 +1,103 @@ +/* + * libnbt++ - A library for the Minecraft Named Binary Tag format. + * Copyright (C) 2013, 2015 ljfa-ag + * + * This file is part of libnbt++. + * + * libnbt++ is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libnbt++ is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libnbt++. If not, see . + */ +#ifndef TAG_LIST_H_INCLUDED +#define TAG_LIST_H_INCLUDED + +#include "crtp_tag.h" +#include + +namespace nbt +{ + +///Tag that contains multiple unnamed tags of the same type +class tag_list : public detail::crtp_tag +{ +public: + //Iterator types + typedef std::vector::iterator iterator; + typedef std::vector::const_iterator const_iterator; + + ///The type of the tag + static constexpr tag_type type = tag_type::List; + + ///Constructs an empty list + tag_list() {} + + /** + * @brief Accesses a tag by index with bounds checking + * + * Returns a value to the tag at the specified index, or throws an + * exception if it is out of range. + * @throw std::out_of_range if given key does not exist + */ + value& at(size_t i); + const value& at(size_t i) const; + + /** + * @brief Accesses a tag by index + * + * Returns a value to the tag at the specified index. No bounds checking + * is performed. + */ + value& operator[](size_t i); + const value& operator[](size_t i) const; + + /** + * @brief Appends the tag to the end of the list + * @throw std::bad_cast if the type of the tag does not match the list's + * content type + */ + void push_back(value&& val); + + /** + * @brief Constructs and appends a tag to the end of the list + * @throw std::bad_cast if the type of the tag does not match the list's + * content type + */ + template + void emplace_back(Args&&... args); + + ///Removes the last element of the list + void pop_back(); + + ///Returns the number of tags in the list + size_t size() const; + + ///Erases all tags from the list + void clear(); + + //Iterators + iterator begin(); + iterator end(); + const_iterator begin() const; + const_iterator end() const; + const_iterator cbegin() const; + const_iterator cend() const; + + friend bool operator==(const tag_list& lhs, const tag_list& rhs); + friend bool operator!=(const tag_list& lhs, const tag_list& rhs); + +private: + std::vector tags; +}; + +} + +#endif // TAG_LIST_H_INCLUDED -- cgit 0.0.5-2-1-g0f52