summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorljfa-ag <ljfa-ag@web.de>2015-07-12 18:19:38 +0200
committerljfa-ag <ljfa-ag@web.de>2015-07-12 18:19:38 +0200
commit1ce4f17facba4dfe62cf52c83b4d59e51b659c31 (patch)
tree87ef369de95c589b0e3b5bfc22a502ede83edf07
parenta6e071d35535c6097458d12ca268bd73c582dcfc (diff)
downloadProject-Tick-1ce4f17facba4dfe62cf52c83b4d59e51b659c31.tar.gz
Project-Tick-1ce4f17facba4dfe62cf52c83b4d59e51b659c31.zip
Implement tag_list
-rw-r--r--include/tag_list.h14
-rw-r--r--src/tag_list.cpp99
2 files changed, 112 insertions, 1 deletions
diff --git a/include/tag_list.h b/include/tag_list.h
index d0b80ebb01..fe5a6818fa 100644
--- a/include/tag_list.h
+++ b/include/tag_list.h
@@ -21,6 +21,8 @@
#define TAG_LIST_H_INCLUDED
#include "crtp_tag.h"
+#include "value.h"
+#include <typeinfo>
#include <vector>
namespace nbt
@@ -98,7 +100,7 @@ public:
///Returns the number of tags in the list
size_t size() const;
- ///Erases all tags from the list
+ ///Erases all tags from the list. Preserves the content type.
void clear();
//Iterators
@@ -117,6 +119,16 @@ private:
tag_type el_type_;
};
+template<class T, class... Args>
+void tag_list::emplace_back(Args&&... args)
+{
+ if(el_type_ == tag_type::Null) //set content type if undetermined
+ el_type_ = T::type;
+ else if(el_type_ != T::type)
+ throw std::bad_cast();
+ tags.emplace_back(T(std::forward<Args>(args)...));
+}
+
}
#endif // TAG_LIST_H_INCLUDED
diff --git a/src/tag_list.cpp b/src/tag_list.cpp
new file mode 100644
index 0000000000..2b66889e56
--- /dev/null
+++ b/src/tag_list.cpp
@@ -0,0 +1,99 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+#include "tag_list.h"
+
+namespace nbt
+{
+
+tag_list::tag_list():
+ tag_list(tag_type::Null)
+{}
+
+tag_list::tag_list(tag_type type):
+ el_type_(type)
+{}
+
+value& tag_list::at(size_t i)
+{
+ return tags.at(i);
+}
+
+const value& tag_list::at(size_t i) const
+{
+ return tags.at(i);
+}
+
+value& tag_list::operator[](size_t i)
+{
+ return tags[i];
+}
+
+const value& tag_list::operator[](size_t i) const
+{
+ return tags[i];
+}
+
+void tag_list::set(size_t i, value&& val)
+{
+ if(val.get_type() != el_type_)
+ throw std::bad_cast();
+ tags.at(i) = std::move(val);
+}
+
+void tag_list::push_back(value&& val)
+{
+ if(!val) //don't allow null values
+ throw std::bad_cast();
+ if(el_type_ == tag_type::Null) //set content type if undetermined
+ el_type_ = val.get_type();
+ else if(el_type_ != val.get_type())
+ throw std::bad_cast();
+ tags.push_back(std::move(val));
+}
+
+void tag_list::pop_back()
+{
+ tags.pop_back();
+}
+
+tag_type tag_list::el_type() const
+{
+ return el_type_;
+}
+
+size_t tag_list::size() const
+{
+ return tags.size();
+}
+
+void tag_list::clear()
+{
+ tags.clear();
+}
+
+auto tag_list::begin() -> iterator { return tags.begin(); }
+auto tag_list::end() -> iterator { return tags.end(); }
+auto tag_list::begin() const -> const_iterator { return tags.begin(); }
+auto tag_list::end() const -> const_iterator { return tags.end(); }
+auto tag_list::cbegin() const -> const_iterator { return tags.cbegin(); }
+auto tag_list::cend() const -> const_iterator { return tags.cend(); }
+
+}
+