From 63f2240983ed8cf8b6a26eba49d60600420d454a Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@student.kit.edu>
Date: Thu, 27 Jun 2019 11:35:01 +0200
Subject: [PATCH] Add copy and move constructors/assignment operators.

---
 VirtualRobot/MJCF/Document.cpp | 21 +++++++++++++++++++++
 VirtualRobot/MJCF/Document.h   | 14 +++++++++++++-
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/VirtualRobot/MJCF/Document.cpp b/VirtualRobot/MJCF/Document.cpp
index 153c478a1..df62aed86 100644
--- a/VirtualRobot/MJCF/Document.cpp
+++ b/VirtualRobot/MJCF/Document.cpp
@@ -15,6 +15,27 @@ Document::Document() : document(new tinyxml2::XMLDocument())
     root.reset(new MujocoRoot(this, xml));
 }
 
+Document::Document(const Document& other)
+{
+    deepCopyFrom(other);
+}
+
+Document::Document(Document&& other) = default;
+
+Document& Document::operator=(const Document& other)
+{
+    if (&other == this)
+    {
+        return *this;
+    }
+    
+    deepCopyFrom(other);
+    return *this;
+}
+
+Document& Document::operator=(Document&& other) = default;
+
+
 void Document::loadFile(const std::string& fileName)
 {
     tinyxml2::XMLError error = document->LoadFile(fileName.c_str());
diff --git a/VirtualRobot/MJCF/Document.h b/VirtualRobot/MJCF/Document.h
index 38aef4d53..17b15d0a6 100644
--- a/VirtualRobot/MJCF/Document.h
+++ b/VirtualRobot/MJCF/Document.h
@@ -25,6 +25,18 @@ namespace mjcf
         /// Constructor.
         Document();
         
+        
+        /// Copy constructor. Performs a deep copy from `other` to `*this`.
+        Document(const Document& other);
+        /// Move constructor.
+        Document(Document&& other);
+        
+        /// Copy assignment operator. Performs a deep copy from `other` to `*this`.
+        Document& operator= (const Document& other);
+        /// Move assignment operator.
+        Document& operator= (Document&& other);
+        
+        
         /// Set the precision for float comparison.
         float getFloatCompPrecision() const;
         /// Set the precision for float comparison (used e.g. when comparing
@@ -40,7 +52,7 @@ namespace mjcf
         /// Save to an MJCF file.
         void saveFile(const std::string& fileName);
         
-        /// Make a deep copy of source to this.
+        /// Make a deep copy of source to `*this`.
         void deepCopyFrom(const Document& source);
         
         void print(std::ostream& os = std::cout) const;
-- 
GitLab