summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorljfa-ag <ljfa-ag@web.de>2015-06-27 22:28:07 +0200
committerljfa-ag <ljfa-ag@web.de>2015-06-27 22:28:07 +0200
commit34ac833956f32e3041919123fe41077eedd5e632 (patch)
treeb3c517770fc7b256ef638dc14a7a34b9b46bb962 /include
parentb600712fad86f186730ff4f9e6a52dde959187ce (diff)
downloadProject-Tick-34ac833956f32e3041919123fe41077eedd5e632.tar.gz
Project-Tick-34ac833956f32e3041919123fe41077eedd5e632.zip
Create nbt_value and further refine tag_compound
Diffstat (limited to 'include')
-rw-r--r--include/nbt_value.h107
-rw-r--r--include/tag_compound.h59
2 files changed, 118 insertions, 48 deletions
diff --git a/include/nbt_value.h b/include/nbt_value.h
new file mode 100644
index 0000000000..5af322b4d2
--- /dev/null
+++ b/include/nbt_value.h
@@ -0,0 +1,107 @@
+/*
+ * 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/>.
+ */
+#ifndef TAG_REF_PROXY_H_INCLUDED
+#define TAG_REF_PROXY_H_INCLUDED
+
+#include "tag.h"
+#include <memory>
+#include <string>
+
+namespace nbt
+{
+
+//Forward declarations
+class tag_compound;
+class tag_list;
+
+/**
+ * @brief Represents a reference to a tag of fixed type
+ *
+ * This can contain a tag of arbitrary type, but once the tag's type
+ * is set it is immutable.
+ * It can be casted to or assigned values, but if the types do not match it
+ * will throw an exception.
+ * It can also refer to an uninitialized value (e.g. when using tag_compound::operator[]
+ * with a non-existant key).
+ */
+class nbt_value
+{
+public:
+ //Movable but not (implicitly) copyable
+ nbt_value& operator=(nbt_value&&) = default;
+ nbt_value& operator=(const nbt_value&) = delete;
+
+ //nbt_value& operator=(std::unique_ptr<tag>&& ptr);
+
+ //Assignment
+ /**
+ * @brief Assigns the given value to the tag if the type matches
+ * @throw std::bad_cast if the value is not convertible to the tag type
+ * via a widening conversion
+ */
+ nbt_value& operator=(int8_t val);
+ nbt_value& operator=(int16_t val);
+ nbt_value& operator=(int32_t val);
+ nbt_value& operator=(int64_t val);
+ nbt_value& operator=(float val);
+ nbt_value& operator=(double val);
+ nbt_value& operator=(const std::string& str);
+ nbt_value& operator=(tag_compound&& comp);
+ nbt_value& operator=(tag_list&& list);
+
+ //Conversion to tag
+ operator tag&();
+ operator const tag&() const;
+
+ //Conversions to primitives and string
+ /**
+ * @brief Casts the value if the type matches
+ * @throw std::bad_cast if the tag type is not convertible to the desired
+ * type via a widening conversion
+ */
+ explicit operator int8_t() const;
+ explicit operator int16_t() const;
+ explicit operator int32_t() const;
+ explicit operator int64_t() const;
+ explicit operator float() const;
+ explicit operator double() const;
+ explicit operator const std::string&() const;
+
+ /**
+ * @brief In case of a tag_compound, accesses a tag by key
+ * @throw std::bad_cast if the tag type is not tag_compound
+ * @sa tag_compound::operator[]
+ */
+ nbt_value& operator[](const std::string& key);
+ const nbt_value& operator[](const std::string& key) const;
+
+ ///@sa tag::get_type
+ tag_type get_type() const;
+
+ friend bool operator==(const nbt_value& lhs, const nbt_value& rhs);
+ friend bool operator!=(const nbt_value& lhs, const nbt_value& rhs);
+
+private:
+ std::unique_ptr<tag> value;
+};
+
+}
+
+#endif // TAG_REF_PROXY_H_INCLUDED
diff --git a/include/tag_compound.h b/include/tag_compound.h
index 2333713dc5..8fdc2f6617 100644
--- a/include/tag_compound.h
+++ b/include/tag_compound.h
@@ -21,6 +21,7 @@
#define TAG_COMPOUND_H_INCLUDED
#include "tag.h"
+#include "nbt_value.h"
#include <map>
#include <memory>
#include <string>
@@ -28,10 +29,6 @@
namespace nbt
{
-//TODO: Create actual proxy class
-typedef tag& tag_ref_proxy;
-typedef const tag& const_tag_ref_proxy;
-
///Tag that contains multiple unordered named tags of arbitrary types
class tag_compound : public tag
{
@@ -46,61 +43,27 @@ public:
///Constructs an empty compound
tag_compound() {}
+ //TODO: Make a separate class similar to and convertible to nbt_value for initializing tag values
+ //tag_compound(std::initializer_list<std::pair<std::string, nbt_value&&>> init);
+
/**
* @brief Accesses a tag by key with bounds checking
*
- * Returns a reference to the tag with the specified key, or throws an
+ * Returns a nbt_value to the tag with the specified key, or throws an
* exception if it does not exist.
* @throw std::out_of_range if given key does not exist
*/
- tag& at(const std::string& key);
- const tag& at(const std::string& key) const;
+ nbt_value& at(const std::string& key);
+ const nbt_value& at(const std::string& key) const;
/**
* @brief Accesses a tag by key
*
- * Returns a proxy value that can be converted to @ref tag&.
- */
- tag_ref_proxy operator[](const std::string& key);
- const_tag_ref_proxy operator[](const std::string& key) const;
-
- /**
- * @brief Inserts a tag into the compound
- *
- * If the given key does not already exist, moves the pointed tag
- * into the compound.
- * @return true if the tag was inserted
- */
- bool insert(const std::string& key, std::unique_ptr<tag>&& ptr);
-
- /**
- * @brief Inserts or assigns a tag
- *
- * If the given key already exists, assigns the pointed tag to it.
- * Otherwise, it is inserted under the given key.
- * @return true if the key did not exist
- */
- bool put(const std::string& key, std::unique_ptr<tag>&& ptr);
-
- /**
- * @brief Constructs and inserts a tag into the compound
- *
- * If the given key does not exist, constructs a new tag of type @c T
- * with the given args and inserts it into the compound.
- * @return true if the tag was inserted
- */
- template<class T, class... Args>
- bool emplace(const std::string& key, Args&&... args);
-
- /**
- * @brief Constructs and assigns or inserts a tag into the compound
- *
- * Constructs a new tag of type @c T with the given args and inserts
- * or assigns it to the given key.
- * @return true if the key did not already exist.
+ * If the key exists, returns a nbt_value to the corresponding tag.
+ * Else, a new uninitalized entry is created under this key.
*/
- template<class T, class... Args>
- bool emplace_put(const std::string& key, Args&&... args);
+ nbt_value& operator[](const std::string& key);
+ const nbt_value& operator[](const std::string& key) const;
/**
* @brief Erases a tag from the compound