diff options
| -rw-r--r-- | include/value.h | 15 | ||||
| -rw-r--r-- | src/tag_compound.cpp | 2 | ||||
| -rw-r--r-- | src/value.cpp | 24 |
3 files changed, 30 insertions, 11 deletions
diff --git a/include/value.h b/include/value.h index 491a92b2a0..a4ccf22873 100644 --- a/include/value.h +++ b/include/value.h @@ -34,13 +34,14 @@ class tag_list; /** * @brief Contains an NBT value of fixed type * - * A wrapper class that contains a dynamically allocated tag of a fixed type. - * Casting or assigning incompatible types will throw a exceptions. + * A convenience wrapper around @c std::unique_ptr<tag>, contains a tag of + * fixed type. */ class value { public: explicit value() {} + explicit value(std::unique_ptr<tag>&& t); //Movable but not (implicitly) copyable value(const value&) = delete; @@ -48,9 +49,6 @@ public: value& operator=(const value&) = delete; value& operator=(value&&) = default; - explicit value(std::unique_ptr<tag>&& t); - value& operator=(std::unique_ptr<tag>&& t); - /** * @brief Assigns the given value to the tag if the type matches * @throw std::bad_cast if the type of @c t is not the same as the type @@ -90,6 +88,9 @@ public: operator double() const; operator const std::string&() const; + ///Returns true if the contained tag is not @c nullptr + explicit operator bool() const; + /** * @brief In case of a tag_compound, accesses a tag by key with bounds checking * @throw std::bad_cast if the tag type is not tag_compound @@ -107,6 +108,10 @@ public: value& operator[](const std::string& key); const value& operator[](const std::string& key) const; + std::unique_ptr<tag>& get_ptr(); + const std::unique_ptr<tag>& get_ptr() const; + void set_ptr(std::unique_ptr<tag>&& t); + ///@sa tag::get_type tag_type get_type() const; diff --git a/src/tag_compound.cpp b/src/tag_compound.cpp index 6bc83301e2..94dc2dd6f1 100644 --- a/src/tag_compound.cpp +++ b/src/tag_compound.cpp @@ -42,7 +42,7 @@ bool tag_compound::put(const std::string& key, std::unique_ptr<tag>&& t) auto it = tags.find(key); if(it != tags.end()) { - it->second = std::move(t); + it->second.set_ptr(std::move(t)); return false; } else diff --git a/src/value.cpp b/src/value.cpp index a5f8e3c894..f062e08a09 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -28,16 +28,30 @@ value::value(std::unique_ptr<tag>&& t): tag_(std::move(t)) {} -value& value::operator=(std::unique_ptr<tag>&& t) +value& value::operator=(tag&& t) { - tag_ = std::move(t); + tag_->assign(std::move(t)); return *this; } -value& value::operator=(tag&& t) +value::operator bool() const { - tag_->assign(std::move(t)); - return *this; + return tag_ != nullptr; +} + +std::unique_ptr<tag>& value::get_ptr() +{ + return tag_; +} + +const std::unique_ptr<tag>& value::get_ptr() const +{ + return tag_; +} + +void value::set_ptr(std::unique_ptr<tag>&& t) +{ + tag_ = std::move(t); } tag_type value::get_type() const |
