From 49bcfa77e1252fb9e4e55d6a5f97a420f6de8fc4 Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Tue, 7 Jul 2015 17:04:39 +0200 Subject: Create copy and clone functions for value and tag --- include/crtp_tag.h | 10 ++++++++++ include/tag.h | 1 + include/value.h | 5 +++++ src/value.cpp | 5 +++++ 4 files changed, 21 insertions(+) diff --git a/include/crtp_tag.h b/include/crtp_tag.h index abd8b3b1ce..928759d6b8 100644 --- a/include/crtp_tag.h +++ b/include/crtp_tag.h @@ -37,6 +37,7 @@ namespace detail tag_type get_type() const noexcept override final; + std::unique_ptr clone() const& override final; std::unique_ptr move_clone() && override final; private: @@ -53,6 +54,15 @@ namespace detail return Sub::type; } + //TODO: Add copy constructors for tags that are missing it before this becomes useable + /*template + std::unique_ptr crtp_tag::clone() const& + { + return std::unique_ptr( + new Sub(static_cast(*this)) + ); + }*/ + template std::unique_ptr crtp_tag::move_clone() && { diff --git a/include/tag.h b/include/tag.h index 6b037b9a32..da1d5098ad 100644 --- a/include/tag.h +++ b/include/tag.h @@ -54,6 +54,7 @@ public: ///Returns the type of the tag virtual tag_type get_type() const noexcept = 0; + virtual std::unique_ptr clone() const& = 0; virtual std::unique_ptr move_clone() && = 0; friend bool operator==(const tag& lhs, const tag& rhs); diff --git a/include/value.h b/include/value.h index 7d3dfc0e92..64e79efe9c 100644 --- a/include/value.h +++ b/include/value.h @@ -128,6 +128,11 @@ public: value& operator[](const std::string& key); value& operator[](const char* key); //need this overload because of conflict with built-in operator[] + /** + * @brief Creates a copy of the value + */ + value copy() const; + std::unique_ptr& get_ptr(); const std::unique_ptr& get_ptr() const; void set_ptr(std::unique_ptr&& t); diff --git a/src/value.cpp b/src/value.cpp index bf75b8931b..d6c276471a 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -356,6 +356,11 @@ value& value::operator[](const char* key) return (*this)[std::string(key)]; } +value value::copy() const +{ + return value(tag_->clone()); +} + std::unique_ptr& value::get_ptr() { return tag_; -- cgit 0.0.5-2-1-g0f52